mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
This commit refactors the CodeRepositoryUtil and LocalFile classes to include a new method, sanitizeFilePath, which removes double slashes from file paths. This change ensures that file paths are properly formatted and improves the reliability and maintainability of the code.
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { PromiseRejectErrorFunction } from 'Common/Types/FunctionTypes';
|
|
import fs from 'fs';
|
|
|
|
export default class LocalFile {
|
|
public static sanitizeFilePath(filePath: string): string {
|
|
// remove double slashes
|
|
return filePath.replace(/\/\//g, '/');
|
|
}
|
|
|
|
public static async makeDirectory(path: string): Promise<void> {
|
|
return new Promise(
|
|
(resolve: VoidFunction, reject: PromiseRejectErrorFunction) => {
|
|
fs.mkdir(path, { recursive: true }, (err: Error | null) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
resolve();
|
|
});
|
|
}
|
|
);
|
|
}
|
|
|
|
public static async write(path: string, data: string): Promise<void> {
|
|
return new Promise(
|
|
(resolve: VoidFunction, reject: PromiseRejectErrorFunction) => {
|
|
fs.writeFile(path, data, (err: Error | null) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
resolve();
|
|
});
|
|
}
|
|
);
|
|
}
|
|
|
|
public static async read(path: string): Promise<string> {
|
|
return new Promise(
|
|
(
|
|
resolve: (data: string) => void,
|
|
reject: PromiseRejectErrorFunction
|
|
) => {
|
|
fs.readFile(
|
|
path,
|
|
{ encoding: 'utf-8' },
|
|
(err: Error | null, data: string) => {
|
|
if (!err) {
|
|
return resolve(data);
|
|
}
|
|
return reject(err);
|
|
}
|
|
);
|
|
}
|
|
);
|
|
}
|
|
}
|