forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandUtils.ts
43 lines (36 loc) · 1.18 KB
/
CommandUtils.ts
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
41
42
43
import * as fs from "fs";
import * as path from "path";
import mkdirp from "mkdirp";
/**
* Command line utils functions.
*/
export class CommandUtils {
/**
* Creates directories recursively.
*/
static createDirectories(directory: string) {
return mkdirp(directory);
}
/**
* Creates a file with the given content in the given path.
*/
static async createFile(filePath: string, content: string, override: boolean = true): Promise<void> {
await CommandUtils.createDirectories(path.dirname(filePath));
return new Promise<void>((ok, fail) => {
if (override === false && fs.existsSync(filePath))
return ok();
fs.writeFile(filePath, content, err => err ? fail(err) : ok());
});
}
/**
* Reads everything from a given file and returns its content as a string.
*/
static async readFile(filePath: string): Promise<string> {
return new Promise<string>((ok, fail) => {
fs.readFile(filePath, (err, data) => err ? fail(err) : ok(data.toString()));
});
}
static async fileExists(filePath: string) {
return fs.existsSync(filePath);
}
}