1 | import * as fs from 'fs-extra';
|
2 | import * as path from 'path';
|
3 |
|
4 | export class FileEngine {
|
5 | private static instance: FileEngine;
|
6 | private constructor() {}
|
7 | public static getInstance() {
|
8 | if (!FileEngine.instance) {
|
9 | FileEngine.instance = new FileEngine();
|
10 | }
|
11 | return FileEngine.instance;
|
12 | }
|
13 |
|
14 | public get(filepath: string): Promise<string> {
|
15 | return new Promise((resolve, reject) => {
|
16 | fs.readFile(path.resolve(filepath), 'utf8', (err, data) => {
|
17 | if (err) {
|
18 | reject('Error during ' + filepath + ' read');
|
19 | } else {
|
20 | resolve(data);
|
21 | }
|
22 | });
|
23 | });
|
24 | }
|
25 |
|
26 | public write(filepath: string, contents: string): Promise<void> {
|
27 | return new Promise((resolve, reject) => {
|
28 | fs.outputFile(path.resolve(filepath), contents, err => {
|
29 | if (err) {
|
30 | reject(err);
|
31 | } else {
|
32 | resolve();
|
33 | }
|
34 | });
|
35 | });
|
36 | }
|
37 |
|
38 | public writeSync(filepath: string, contents: string): void {
|
39 | fs.outputFileSync(filepath, contents);
|
40 | }
|
41 |
|
42 | public getSync(filepath: string): string {
|
43 | return fs.readFileSync(path.resolve(filepath), 'utf8');
|
44 | }
|
45 |
|
46 | |
47 |
|
48 |
|
49 | public existsSync(file: string): boolean {
|
50 | return fs.existsSync(file);
|
51 | }
|
52 | }
|
53 |
|
54 | export default FileEngine.getInstance();
|