搭建一个nodejs脚手架的方法步骤(2)

#!/usr/bin/env node const chalk = require('chalk') console.log('Hello, cli!') console.log(chalk.green('init创建')) const fs = require('fs') const program = require('commander') const download = require('download-git-repo') const inquirer = require('inquirer') const ora = require('ora') const symbols = require('log-symbols') program .version(require('./package').version, '-v, --version') .command('init <name>') .action((name) => { console.log(name) inquirer.prompt([ { type: 'input', name: 'author', message: '请输入你的名字' } ]).then((answers) => { console.log(answers.author) const lqProcess = ora('正在创建...') lqProcess.start() download('github:/Mr-Prune/learn/mongodb-koa', name, {clone: true}, (err) => { if (err) { lqProcess.fail() console.log(symbols.error, chalk.red(err)) } else { lqProcess.succeed() const fileName = `${name}/package.json` const meta = { name, 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(symbols.success, chalk.green('创建成gong')) } }) }) }) program.parse(process.argv)

总结

通过上面的例子只是能够搭建出一个简单的脚手架工具,其实bash还可以做很多东西,比如 npm 包优雅地处理标准输入、管理并行任务、监听文件、管道流、压缩、ssh、git等,要想了解更多,就要深入了解,这里只是打开一扇门,学海无涯。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/b48e94b41465557496c6f66e67f29ca2.html