idraw/scripts/util/file.ts

28 lines
818 B
TypeScript
Raw Permalink Normal View History

2022-12-31 15:21:57 +00:00
import fs from 'fs';
import path from 'path';
2021-06-04 06:43:42 +00:00
export function removeFullDir(dirPath: string) {
2023-02-12 03:39:24 +00:00
if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) {
fs.rmSync(dirPath, { recursive: true });
2021-06-04 06:43:42 +00:00
}
}
export function projectRootPath(...args: string[]) {
const pathList = Array.from(args);
const baseDir = path.join(__dirname, '..', '..');
return path.join(baseDir, ...pathList);
}
export function readJSONFile(...args: string[]) {
const filePath = projectRootPath(...args);
const jsonStr = fs.readFileSync(filePath, { encoding: 'utf8' });
const json = JSON.parse(jsonStr);
return json;
}
export function writeJSONFile(filePath: string, json: any) {
const fullPath = projectRootPath(filePath);
const jsonStr = JSON.stringify(json, null, 2);
fs.writeFileSync(fullPath, jsonStr);
}