kawkab-frontend
Version:
Kawkab frontend is a frontend library for the Kawkab framework
28 lines (27 loc) • 1.16 kB
JavaScript
import fs from 'fs-extra';
import path from 'path';
import chalk from 'chalk';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export function generateFile(templateName, targetPath, replacements) {
const templatePath = path.resolve(__dirname, `../stubs/${templateName}`);
const finalTargetPath = path.resolve(process.cwd(), targetPath);
try {
if (fs.existsSync(finalTargetPath)) {
console.log(chalk.yellow(`⚠️ File already exists: ${targetPath}`));
return;
}
const template = fs.readFileSync(templatePath, 'utf-8');
let content = template;
for (const [key, value] of Object.entries(replacements)) {
content = content.replace(new RegExp(`\\[${key}\\]`, 'g'), value);
}
fs.ensureDirSync(path.dirname(finalTargetPath));
fs.writeFileSync(finalTargetPath, content);
console.log(chalk.green(`✅ Successfully created: ${targetPath}`));
}
catch (error) {
console.error(chalk.red(`❌ Error creating file ${targetPath}:`), error);
}
}