UNPKG

@wilboerht/blog-template

Version:
108 lines (90 loc) • 3.38 kB
#!/usr/bin/env node import { program } from 'commander'; import prompts from 'prompts'; import fs from 'fs-extra'; import path from 'path'; import { fileURLToPath } from 'url'; import chalk from 'chalk'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const templateDir = path.join(__dirname, '../template'); program .name('create-bilingual-blog') .description('Create a new bilingual blog with VitePress') .argument('[name]', 'Blog directory name') .action(async (name) => { try { // If name is not provided, ask for it let projectName = name; if (!projectName) { const response = await prompts({ type: 'text', name: 'name', message: 'What is your blog name?', initial: 'my-blog' }); projectName = response.name; } // Ask for blog configuration const questions = [ { type: 'text', name: 'title', message: 'What is your blog title?', initial: 'My Awesome Blog' }, { type: 'text', name: 'description', message: 'Enter a brief description for your blog:', initial: 'A bilingual blog built with VitePress' }, { type: 'text', name: 'author', message: 'What is your name?' }, { type: 'text', name: 'github', message: 'What is your GitHub username?' } ]; const response = await prompts(questions); // Create project directory const targetDir = path.join(process.cwd(), projectName); console.log(chalk.blue('\nCreating your blog...')); // Copy template files await fs.copy(templateDir, targetDir, { filter: (src) => { return !src.includes('node_modules') && !src.includes('.git'); } }); // Update package.json with user inputs const pkgPath = path.join(targetDir, 'package.json'); const pkg = await fs.readJson(pkgPath); pkg.name = projectName; pkg.author.name = response.author; pkg.author.url = `https://github.com/${response.github}`; pkg.repository.url = `git+https://github.com/${response.github}/${projectName}.git`; pkg.bugs.url = `https://github.com/${response.github}/${projectName}/issues`; pkg.homepage = `https://github.com/${response.github}/${projectName}#readme`; await fs.writeJson(pkgPath, pkg, { spaces: 2 }); // Update VitePress config const configPath = path.join(targetDir, 'docs/.vitepress/config.ts'); let config = await fs.readFile(configPath, 'utf8'); config = config.replace(/title: '.*'/, `title: '${response.title}'`); config = config.replace(/description: '.*'/, `description: '${response.description}'`); await fs.writeFile(configPath, config); console.log(chalk.green('\n✨ Blog created successfully!')); console.log('\nNext steps:'); console.log(chalk.cyan(` cd ${projectName}`)); console.log(chalk.cyan(' npm install')); console.log(chalk.cyan(' npm run docs:dev')); console.log('\nHappy blogging! 🎉'); } catch (err) { console.error(chalk.red('Error creating blog:'), err); process.exit(1); } }); program.parse();