leshi-ui
Version:
Modern CLI for building and managing React Native UI components with copy-paste simplicity, custom theming, and open source design system support
66 lines (65 loc) • 1.94 kB
JavaScript
import * as fs from 'fs/promises';
import * as fsExtra from 'fs-extra';
import * as path from 'path';
export class FileUtils {
static async exists(filePath) {
return fsExtra.pathExists(filePath);
}
static async ensureDir(dirPath) {
await fsExtra.ensureDir(dirPath);
}
static async copy(src, dest, overwrite = false) {
if (!overwrite && await this.exists(dest)) {
throw new Error(`File already exists: ${dest}`);
}
await this.ensureDir(path.dirname(dest));
await fsExtra.copy(src, dest, { overwrite });
}
static async readFile(filePath) {
return fs.readFile(filePath, 'utf-8');
}
static async writeFile(filePath, content) {
await this.ensureDir(path.dirname(filePath));
await fs.writeFile(filePath, content, 'utf-8');
}
static async readJson(filePath) {
const content = await fs.readFile(filePath, 'utf-8');
return JSON.parse(content);
}
static async writeJson(filePath, data) {
await this.ensureDir(path.dirname(filePath));
await fs.writeFile(filePath, JSON.stringify(data, null, 2), 'utf-8');
}
static async isDirectory(dirPath) {
try {
const stats = await fs.stat(dirPath);
return stats.isDirectory();
}
catch {
return false;
}
}
static async listFiles(dirPath) {
try {
return await fs.readdir(dirPath);
}
catch {
return [];
}
}
static getRelativePath(from, to) {
return path.relative(from, to);
}
static join(...paths) {
return path.join(...paths);
}
static dirname(filePath) {
return path.dirname(filePath);
}
static basename(filePath, ext) {
return path.basename(filePath, ext);
}
static resolve(...paths) {
return path.resolve(...paths);
}
}