整合前端团队代码规范, 以及统一整体架构
本文借鉴vue-cli思路,用到的库有:
npm init -y npm i commander download-git-repo inquirer handlebars ora chalk log-symbols -S
{ "name": "lcq-cli", "version": "1.0.0", "description": "lcq-cli", "bin": { "lcq": "index.js" }, ... }
const fs = require('fs'); const program = require('commander'); const download = require('download-git-repo'); const handlebars = require('handlebars'); const inquirer = require('inquirer'); const ora = require('ora'); const chalk = require('chalk'); const symbols = require('log-symbols'); program.version('1.0.0', '-v, --version') .command('init <name>') .action((name) => { if(!fs.existsSync(name)){ inquirer.prompt([ { name: 'description', message: '请输入项目描述' }, { name: 'author', message: '请输入作者名称' } ]).then((answers) => { const spinner = ora('download...'); spinner.start(); download('https://github.com:bear-new/latest-webpack-cli#master', name, {clone: true}, (err) => { if(err){ spinner.fail(); console.log(chalk.red(err)); }else{ spinner.succeed(); const fileName = `${name}/package.json`; const meta = { name, description: answers.description, author: answers.author } if(fs.existsSync(fileName)){ const content = fs.readFileSync(fileName).toString(); const result = handlebars.compile(content)(meta); fs.writeFileSync(fileName, result); } console.log(chalk.green('success')); } }) }) }else{ console.log(symbols.error, chalk.red('项目已存在')); } }) program.parse(process.argv);
node ./index.js init demo