UNPKG

weblica

Version:

One-click HTML/CSS/JS website generator

65 lines (53 loc) 1.96 kB
import fs from 'fs-extra'; import inquirer from 'inquirer'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const configPath = path.resolve(__dirname, '..', 'config.json'); export async function ask() { 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 templateDir = path.resolve(__dirname, '..', config.templateDir); if (!(await fs.pathExists(templateDir))) { console.error(chalk.red(`❌ Template directory missing: ${templateDir}`)); process.exit(1); } const categories = (await fs.readdir(templateDir)) .filter(p => fs.statSync(path.join(templateDir, p)).isDirectory()); if (!categories.length) { console.error(chalk.red('❌ No categories found inside templates/')); process.exit(1); } const { projectName } = await inquirer.prompt({ type: 'input', name: 'projectName', message: 'Website name?', validate: input => input.trim().length > 0 || 'Name cannot be empty' }); const { category } = await inquirer.prompt({ type: 'list', name: 'category', message: 'Choose website category…', choices: categories }); const catPath = path.join(templateDir, category); const templates = (await fs.readdir(catPath)) .filter(p => fs.statSync(path.join(catPath, p)).isDirectory()); if (!templates.length) { console.error(chalk.red(`❌ No templates found in category “${category}”`)); process.exit(1); } const { template } = await inquirer.prompt({ type: 'list', name: 'template', message: 'Pick a template', choices: templates }); return { projectName: projectName.trim(), category, template }; }