2023-03-18 21:37:34 +00:00
|
|
|
import fs from 'fs';
|
2024-02-27 16:36:27 +00:00
|
|
|
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) => {
|
2024-02-27 16:36:27 +00:00
|
|
|
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(
|
2024-02-27 17:23:25 +00:00
|
|
|
(resolve: VoidFunction, reject: PromiseRejectErrorFunction) => {
|
2024-02-27 16:36:27 +00:00
|
|
|
fs.writeFile(path, data, (err: Error | null) => {
|
2024-02-27 15:16:02 +00:00
|
|
|
if (err) {
|
2024-02-27 16:27:13 +00:00
|
|
|
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' },
|
2024-02-27 16:27:13 +00:00
|
|
|
(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
|
|
|
}
|