UNPKG

weblica

Version:

One-click HTML/CSS/JS website generator

59 lines (51 loc) 1.99 kB
import fs from 'fs-extra'; import path from 'node:path'; import chalk from 'chalk'; import { fileURLToPath } from 'node:url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const configPath = path.resolve(__dirname, '..', 'config.json'); export async function generate({ projectName, category, template }) { let config; try { config = JSON.parse(await fs.readFile(configPath, 'utf8')); } catch (err) { console.error(chalk.red('❌ Failed to load config.json'), err.message); process.exit(1); } const templateRoot = path.resolve(__dirname, '..', config.templateDir); const src = path.join(templateRoot, category, template); const dest = path.resolve(process.cwd(), projectName); if (!(await fs.pathExists(src))) { console.error(chalk.red(`❌ Template not found: ${src}`)); process.exit(1); } if (await fs.pathExists(dest)) { console.error(chalk.red(`❌ Folder “${projectName}” already exists.`)); process.exit(1); } console.log(chalk.blue('📦 Copying template…')); try { await fs.copy(src, dest); } catch (err) { console.error(chalk.red('❌ Copy failed'), err.message); process.exit(1); } // Replace {{WEBSITE_NAME}} everywhere try { const files = await fs.readdir(dest, { recursive: true }); for (const file of files) { const full = path.join(dest, file); if ((await fs.stat(full)).isFile()) { let content = await fs.readFile(full, 'utf8'); content = content.replace(/{{WEBSITE_NAME}}/g, projectName); await fs.writeFile(full, content, 'utf8'); } } } catch (err) { console.error(chalk.red('❌ Failed to inject site name'), err.message); process.exit(1); } console.log(chalk.green(`✅ Created “${projectName}”`)); console.log(chalk.gray(`cd ${projectName}`)); console.log(chalk.gray('Serve with any static server (e.g. npx serve .)')); }