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.
This commit is contained in:
2026-04-12 16:57:21 -04:00
parent fe84ec6a43
commit e9d2f4d5d1
+37 -60
View File
@@ -8,10 +8,19 @@ import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
const logStep = (stepNumber, message) => { const c = {
console.log(`\n📋 Étape ${stepNumber}: ${message}`); 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) // Templates zen (inline pour éviter des dépendances externes)
const zenTemplates = { const zenTemplates = {
instrumentation: `export async function register() { instrumentation: `export async function register() {
@@ -56,38 +65,28 @@ const zenFiles = [
async function createZenFile(filePath, content) { async function createZenFile(filePath, content) {
const fullPath = path.resolve(process.cwd(), filePath); const fullPath = path.resolve(process.cwd(), filePath);
const dir = path.dirname(fullPath); await fs.ensureDir(path.dirname(fullPath));
await fs.ensureDir(dir);
await fs.writeFile(fullPath, content, 'utf-8'); await fs.writeFile(fullPath, content, 'utf-8');
console.log(`${filePath}`);
} }
async function initProject() { async function initProject() {
try { 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) step('Creating Next.js project');
logStep(1, 'Création du projet Next.js');
execSync('npx create-next-app@latest ./ --javascript --tailwind --app --empty', { stdio: 'inherit' }); 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')) { if (fs.existsSync('./app')) {
fs.removeSync('./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', { fs.writeJsonSync('./jsconfig.json', {
compilerOptions: { compilerOptions: { paths: { '@*': ['./*'] } },
paths: { '@*': ['./*'] },
},
}, { spaces: 2 }); }, { 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} */ fs.writeFileSync('./next.config.mjs', `/** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
\tdevIndicators: false, \tdevIndicators: false,
@@ -98,69 +97,47 @@ const nextConfig = {
export default 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'; const packageJsonPath = './package.json';
if (fs.existsSync(packageJsonPath)) { if (fs.existsSync(packageJsonPath)) {
const packageJson = fs.readJsonSync(packageJsonPath); const packageJson = fs.readJsonSync(packageJsonPath);
const projectName = packageJson.name || 'Mon projet'; const projectName = packageJson.name || 'project';
fs.writeFileSync('README.md', `# ${projectName} fs.writeFileSync('README.md', `# ${projectName}\n\nBuilt with Next.js, Tailwind CSS and @zen/core.\n\n![screenshot](/.github/assets/screenshot.png)`);
packageJson.scripts ??= {};
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 = {};
packageJson.scripts['make-favicon'] = 'node dev/icons/make-favicon.js'; packageJson.scripts['make-favicon'] = 'node dev/icons/make-favicon.js';
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 }); 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'); const templatesPath = path.join(__dirname, '..', 'templates');
if (fs.existsSync(templatesPath)) { if (!fs.existsSync(templatesPath)) {
fs.copySync(templatesPath, './', { overwrite: true }); fail('Templates directory not found in zen-start package');
console.log(' 📁 Fichiers template copiés');
} else {
console.error(' ❌ Dossier templates introuvable dans le package zen-start');
process.exit(1); 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) { for (const file of zenFiles) {
await createZenFile(file.path, zenTemplates[file.template]); 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'); 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 step('Installing @zen/core');
logStep(10, 'Installation de @zen/core');
execSync('npm install @zen/core', { stdio: 'inherit' }); execSync('npm install @zen/core', { stdio: 'inherit' });
done('@zen/core installed');
// Fin console.log(`\n ${c.green}${c.bold}Project ready.${c.reset} Next steps:\n`);
console.log('\n✅ Projet zen prêt !\n'); console.log(` ${c.dim}1.${c.reset} Configure environment variables`);
console.log('Prochaines étapes :'); console.log(` ${c.dim}https://git.hyko.cx/zen/core${c.reset}`);
console.log(' 1. Configurer les variables d\'environnement'); console.log(` ${c.dim}2.${c.reset} Initialize the database`);
console.log(' (voir la doc : https://git.hyko.cx/zen/core)'); console.log(` ${c.dim}npx zen-db init${c.reset}\n`);
console.log(' 2. Initialiser la base de données :');
console.log(' npx zen-db init');
console.log('');
} catch (error) { } catch (error) {
console.error('\n❌ Une erreur est survenue :', error.message); fail(error.message);
process.exit(1); process.exit(1);
} }
} }