summaryrefslogtreecommitdiff
path: root/src/sources/FileSource.ts
blob: fd4f6f7a133b9b4ade27fce93ed49e61c62300dc (plain)
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
import {readFile} from 'fs';

import {ISource} from './ISource';

export class FileSource implements ISource {
    private _path: string;
    private _data: string = null;

    constructor(path: string) {
        this._path = path;
    }

    getData(): Promise<string> {
        if (this._data) {
            return Promise.resolve(this._data);
        }

        return new Promise<string>((resolve, reject) => {
            readFile(this._path, (err, data) => {
                if (err) return reject(err);

                this._data = data.toString();
                resolve(this._data);
            });
        });
    }
}