-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.ts
30 lines (26 loc) · 775 Bytes
/
files.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
import fs from 'fs';
export const writeFile = async (path: string, data: string) => {
return new Promise((accept, rej) => {
fs.writeFile(path, data, (err) => {
if (!err)
accept();
else rej(err)
});
});
}
export const writeJsonFile = async (path: string, data: any) => {
await writeFile(path, JSON.stringify(data));
}
export const readFile = async (path: string): Promise<string> => {
return new Promise((accept, rej) => {
fs.readFile(path, (err, data) => {
if (!err)
accept(data.toString());
else rej(err);
})
});
}
export const readJsonFile = async (path: string) => {
const text = await readFile(path);
return JSON.parse(text);
}