From e9d2f4d5d1508b88bb030d46c631d18c59c56e67 Mon Sep 17 00:00:00 2001 From: Hyko Date: Sun, 12 Apr 2026 16:57:21 -0400 Subject: [PATCH] refactor(cli): improve output formatting and simplify init logic Replace step-numbered logging with minimal ANSI-styled output using `step`, `done`, and `fail` helpers. Consolidate redundant README and package.json updates into a single block, remove verbose inline comments, and simplify file creation by eliminating unnecessary variable assignments. --- bin/cli.js | 97 +++++++++++++++++++++--------------------------------- 1 file changed, 37 insertions(+), 60 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index dc17eba..7eaafdc 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -8,10 +8,19 @@ import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); -const logStep = (stepNumber, message) => { - console.log(`\n📋 Étape ${stepNumber}: ${message}`); +const c = { + reset: '\x1b[0m', + dim: '\x1b[2m', + green: '\x1b[32m', + cyan: '\x1b[36m', + red: '\x1b[31m', + bold: '\x1b[1m', }; +const step = (msg) => process.stdout.write(` ${c.dim}◆${c.reset} ${msg}...\n`); +const done = (msg) => process.stdout.write(` ${c.green}✓${c.reset} ${msg}\n`); +const fail = (msg) => process.stdout.write(` ${c.red}✗${c.reset} ${msg}\n`); + // Templates zen (inline pour Ă©viter des dĂ©pendances externes) const zenTemplates = { instrumentation: `export async function register() { @@ -56,38 +65,28 @@ const zenFiles = [ async function createZenFile(filePath, content) { const fullPath = path.resolve(process.cwd(), filePath); - const dir = path.dirname(fullPath); - await fs.ensureDir(dir); + await fs.ensureDir(path.dirname(fullPath)); await fs.writeFile(fullPath, content, 'utf-8'); - console.log(` ✅ ${filePath}`); } async function initProject() { try { - console.log('\n🚀 Initialisation du projet Next.js avec @zen/core...\n'); + console.log(`\n ${c.bold}${c.cyan}zen-start${c.reset}\n`); - // Étape 1 : CrĂ©er le projet Next.js (le dossier doit ĂȘtre vide) - logStep(1, 'CrĂ©ation du projet Next.js'); + step('Creating Next.js project'); execSync('npx create-next-app@latest ./ --javascript --tailwind --app --empty', { stdio: 'inherit' }); + done('Next.js project created'); - // Étape 2 : Supprimer /app (sera remplacĂ© par le template) - logStep(2, 'Suppression du dossier /app par dĂ©faut'); if (fs.existsSync('./app')) { fs.removeSync('./app'); - console.log(' 📁 Dossier /app supprimĂ©'); } + done('Cleaned default /app directory'); - // Étape 3 : Mettre Ă  jour jsconfig.json (path aliases) - logStep(3, 'Configuration des path aliases (jsconfig.json)'); fs.writeJsonSync('./jsconfig.json', { - compilerOptions: { - paths: { '@*': ['./*'] }, - }, + compilerOptions: { paths: { '@*': ['./*'] } }, }, { spaces: 2 }); - console.log(' ⚙ jsconfig.json mis Ă  jour'); + done('Configured path aliases'); - // Étape 4 : Mettre Ă  jour next.config.mjs - logStep(4, 'Configuration de next.config.mjs'); fs.writeFileSync('./next.config.mjs', `/** @type {import('next').NextConfig} */ const nextConfig = { \tdevIndicators: false, @@ -98,69 +97,47 @@ const nextConfig = { export default nextConfig; `); - console.log(' ⚙ next.config.mjs mis Ă  jour'); + done('Configured next.config.mjs'); - // Étape 5 : Mettre Ă  jour README.md - logStep(5, 'Mise Ă  jour du README.md'); const packageJsonPath = './package.json'; if (fs.existsSync(packageJsonPath)) { const packageJson = fs.readJsonSync(packageJsonPath); - const projectName = packageJson.name || 'Mon projet'; - fs.writeFileSync('README.md', `# ${projectName} - -Un site web construit avec Next.js, Tailwind CSS et @zen/core. - -![screenshot](/.github/assets/screenshot.png)`); - console.log(' 📘 README.md mis Ă  jour'); - } - - // Étape 6 : Ajouter le script make-favicon dans package.json - logStep(6, 'Ajout du script make-favicon'); - if (fs.existsSync(packageJsonPath)) { - const packageJson = fs.readJsonSync(packageJsonPath); - if (!packageJson.scripts) packageJson.scripts = {}; + const projectName = packageJson.name || 'project'; + fs.writeFileSync('README.md', `# ${projectName}\n\nBuilt with Next.js, Tailwind CSS and @zen/core.\n\n![screenshot](/.github/assets/screenshot.png)`); + packageJson.scripts ??= {}; packageJson.scripts['make-favicon'] = 'node dev/icons/make-favicon.js'; fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 }); - console.log(' ⚙ Script make-favicon ajoutĂ©'); } + done('Updated README and package.json'); - // Étape 7 : Copier les fichiers template (app/, styles/, dev/, components/) - logStep(7, 'Copie des fichiers template'); const templatesPath = path.join(__dirname, '..', 'templates'); - if (fs.existsSync(templatesPath)) { - fs.copySync(templatesPath, './', { overwrite: true }); - console.log(' 📁 Fichiers template copiĂ©s'); - } else { - console.error(' ❌ Dossier templates introuvable dans le package zen-start'); + if (!fs.existsSync(templatesPath)) { + fail('Templates directory not found in zen-start package'); process.exit(1); } + fs.copySync(templatesPath, './', { overwrite: true }); + done('Copied template files'); - // Étape 8 : CrĂ©er les fichiers zen (auth, admin, api, instrumentation) - logStep(8, 'CrĂ©ation des fichiers @zen/core'); for (const file of zenFiles) { await createZenFile(file.path, zenTemplates[file.template]); } + done('Created @zen/core route files'); - // Étape 9 : CrĂ©er .npmrc avec le registre Gitea (APRÈS create-next-app !) - logStep(9, 'Configuration du registre npm (@zen/core)'); fs.writeFileSync('./.npmrc', '@zen:registry=https://git.hyko.cx/api/packages/zen/npm/\n'); - console.log(' ⚙ .npmrc créé'); + done('Configured npm registry'); - // Étape 10 : Installer @zen/core - logStep(10, 'Installation de @zen/core'); + step('Installing @zen/core'); execSync('npm install @zen/core', { stdio: 'inherit' }); + done('@zen/core installed'); - // Fin - console.log('\n✅ Projet zen prĂȘt !\n'); - console.log('Prochaines Ă©tapes :'); - console.log(' 1. Configurer les variables d\'environnement'); - console.log(' (voir la doc : https://git.hyko.cx/zen/core)'); - console.log(' 2. Initialiser la base de donnĂ©es :'); - console.log(' npx zen-db init'); - console.log(''); + console.log(`\n ${c.green}${c.bold}Project ready.${c.reset} Next steps:\n`); + console.log(` ${c.dim}1.${c.reset} Configure environment variables`); + console.log(` ${c.dim}https://git.hyko.cx/zen/core${c.reset}`); + console.log(` ${c.dim}2.${c.reset} Initialize the database`); + console.log(` ${c.dim}npx zen-db init${c.reset}\n`); } catch (error) { - console.error('\n❌ Une erreur est survenue :', error.message); + fail(error.message); process.exit(1); } }