-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxfs.ts
37 lines (32 loc) · 873 Bytes
/
xfs.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
"use strict";
import * as fs from "fs";
import writeAtomic from "write-file-atomic";
export namespace xfs {
export async function readText(srcPath: string) {
return new Promise<string | undefined>((resolve, reject) => {
fs.readFile(srcPath, "utf8", (err, data) => {
if (err) {
resolve(undefined);
} else {
resolve(data.toString());
}
});
});
}
export async function readJson(srcPath: string): Promise<string | undefined> {
const data = await xfs.readText(srcPath);
return data ? JSON.parse(data) : undefined;
}
export async function writeJsonAtomic(json: any, destPath: string): Promise<undefined> {
const data = JSON.stringify(json, null, 2);
return new Promise<undefined>((resolve, reject) => {
writeAtomic(destPath, data, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
}