oneuptime/CommonServer/Utils/LocalFile.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-03-18 21:37:34 +00:00
import fs from 'fs';
import { PromiseRejectErrorFunction } from 'Common/Types/FunctionTypes';
2023-03-18 21:37:34 +00:00
export default class LocalFile {
2023-10-06 12:41:29 +00:00
public static async makeDirectory(path: string): Promise<void> {
2024-02-27 15:16:02 +00:00
return new Promise(
2024-02-27 15:17:39 +00:00
(resolve: Function, reject: PromiseRejectErrorFunction) => {
fs.mkdir(path, { recursive: true }, (err: Error | null) => {
2024-02-27 15:16:02 +00:00
if (err) {
return reject(err);
}
resolve();
});
}
);
2023-10-06 12:41:29 +00:00
}
public static async write(path: string, data: string): Promise<void> {
2024-02-27 15:16:02 +00:00
return new Promise(
(resolve: VoidFunction, reject: PromiseRejectErrorFunction) => {
fs.writeFile(path, data, (err: Error | null) => {
2024-02-27 15:16:02 +00:00
if (err) {
return reject(err);
2024-02-27 15:16:02 +00:00
}
resolve();
});
}
);
2023-10-06 12:41:29 +00:00
}
2023-03-18 21:37:34 +00:00
public static async read(path: string): Promise<string> {
2023-03-18 21:48:21 +00:00
return new Promise(
2024-02-27 15:16:02 +00:00
(
resolve: (data: string) => void,
2024-02-27 15:17:39 +00:00
reject: PromiseRejectErrorFunction
2024-02-27 15:16:02 +00:00
) => {
2023-03-20 18:06:50 +00:00
fs.readFile(
2023-03-18 21:48:21 +00:00
path,
{ encoding: 'utf-8' },
(err: Error | null, data: string) => {
2023-03-18 21:48:21 +00:00
if (!err) {
return resolve(data);
}
return reject(err);
}
);
}
);
2023-03-18 21:37:34 +00:00
}
2023-03-18 21:48:21 +00:00
}