# all files / files/ File.js

100% Statements 83/83
100% Branches 28/28
100% Functions 17/17
100% Lines 11/11
22 statements, 1 function, 18 branches Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40

    1

    1
    78
    78

    78

    78

    1

    2

    2

    1

     'use strict';

    // Utilities:
    import Promise from 'bluebird';
    const fs = Promise.promisifyAll(require('graceful-fs'));
    import { sep } from 'path';

    export default class File {
        constructor (path, directory) {
            this.directory = directory;
            this.path = path;

            let [fileName] = this.path.split(sep).reverse();
            let [name] = fileName.split('.');
            this.name = name;

            this.directory.addFile(this);
        }

        read () {
            return fs.readFileAsync(this.path)
            .then(content => this.content = content.toString());
        }

        save () {
            return fs.writeFileAsync(this.path, this.content)
            .then(() => this.directory.addFile(this));
        }

        delete () {
            return fs.unlinkAsync(this.path)
            .then(() => this.directory.removeFile(this));
        }

        toJSON () {
            let { ast, content, path, name, isPending, tokens } = this;
            return { ast, content, path, name, isPending, tokens };
        }
    }