mirror of
https://github.com/idrawjs/idraw
synced 2026-05-22 17:18:24 +00:00
27 lines
818 B
TypeScript
27 lines
818 B
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
export function removeFullDir(dirPath: string) {
|
|
if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) {
|
|
fs.rmSync(dirPath, { recursive: true });
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|