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

import {ISource} from './ISource';

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

    constructor(course: string) {
    }

    getData(weekOfYear: number): 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);
            });
        });
    }
}