-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
CliFs.ts
65 lines (51 loc) 路 1.84 KB
/
CliFs.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {Injectable} from "@tsed/di";
import {PathLike, WriteFileOptions} from "fs";
import Fs from "fs-extra";
import {EnsureOptions} from "fs-extra";
import {join} from "path";
import {normalizePath} from "@tsed/normalize-path";
@Injectable()
export class CliFs {
raw = Fs;
exists(path: string) {
return this.raw.existsSync(path);
}
join(...args: string[]) {
return normalizePath(join(...args));
}
async readFile(file: string | Buffer | number, encoding?: any): Promise<string> {
return this.raw.readFile(file, encoding) as any;
}
async writeJson(file: string | Buffer | number, data: any, options?: WriteFileOptions | string): Promise<any> {
await this.raw.writeFile(file, JSON.stringify(data, null, 2), options || ({encoding: "utf8"} as any));
}
writeFileSync(path: PathLike | number, data: any, options?: WriteFileOptions) {
return this.raw.writeFileSync(path, data, options);
}
writeFile(file: string | Buffer | number, data: any, options?: WriteFileOptions | string) {
return this.raw.writeFile(file, data, options as any);
}
ensureDir(path: string, options?: EnsureOptions | number) {
return this.raw.ensureDir(path, options);
}
ensureDirSync(path: string, options?: EnsureOptions | number): void {
return this.raw.ensureDirSync(path, options);
}
findUpFile(root: string, file: string) {
return [join(root, file), join(root, "..", file), join(root, "..", "..", file), join(root, "..", "..", "..", file)].find((path) =>
this.exists(path)
);
}
async importModule(mod: string, root: string = process.cwd()) {
try {
if (process.env.NODE_ENV === "development") {
return await import(mod);
}
} catch (er) {}
const path = this.findUpFile(root, join("node_modules", mod));
if (path) {
return import(path);
}
return import(mod);
}
}