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:
+37
-60
@@ -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.
|
||||
|
||||
`);
|
||||
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`);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user