UNPKG

accs-cli

Version:

ACCS CLI — Full-featured developer tool for scaffolding, running, building, and managing multi-language projects

150 lines (134 loc) 3.23 kB
/** * File system utilities for cross-platform operations */ import { promises as fs } from 'fs'; import { existsSync, statSync } from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { dirname } from 'path'; import fse from 'fs-extra'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export class FileUtils { /** * Check if a file or directory exists */ static exists(filePath) { return existsSync(filePath); } /** * Check if path is a directory */ static isDirectory(filePath) { try { return statSync(filePath).isDirectory(); } catch { return false; } } /** * Check if path is a file */ static isFile(filePath) { try { return statSync(filePath).isFile(); } catch { return false; } } /** * Create directory recursively */ static async createDir(dirPath) { await fs.mkdir(dirPath, { recursive: true }); } /** * Copy file or directory */ static async copy(src, dest) { await fse.copy(src, dest, { overwrite: true }); } /** * Remove file or directory */ static async remove(filePath) { await fse.remove(filePath); } /** * Read JSON file */ static async readJson(filePath) { try { const content = await fs.readFile(filePath, 'utf8'); return JSON.parse(content); } catch (error) { throw new Error(`Failed to read JSON file ${filePath}: ${error.message}`); } } static async readFile(filePath) { try { return await fs.readFile(filePath, 'utf8'); } catch (error) { throw new Error(`Failed to read file ${filePath}: ${error.message}`); } } /** * Write JSON file */ static async writeJson(filePath, data, spaces = 2) { try { await fs.writeFile(filePath, JSON.stringify(data, null, spaces), 'utf8'); } catch (error) { throw new Error(`Failed to write JSON file ${filePath}: ${error.message}`); } } /** * Get file extension */ static getExtension(filePath) { return path.extname(filePath).toLowerCase(); } /** * Get templates directory path */ static getTemplatesDir() { return path.join(__dirname, '../../templates'); } /** * Get project root directory */ static getProjectRoot() { let current = process.cwd(); while (current !== path.dirname(current)) { if (this.exists(path.join(current, 'package.json'))) { return current; } current = path.dirname(current); } return process.cwd(); } /** * Get accs-cli root directory */ static getAccsCliRoot() { let current = __dirname; while (current !== path.dirname(current)) { if (this.exists(path.join(current, 'package.json'))) { return current; } current = path.dirname(current); } return null; } /** * Resolve template variables in file content */ static resolveTemplate(content, variables) { let result = content; for (const [key, value] of Object.entries(variables)) { const regex = new RegExp(`\\{\\{${key}\\}\\}`, 'g'); result = result.replace(regex, value); } return result; } }