1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| var program = require('commander') var ora = require('ora') var inquirer = require('inquirer') var download = require('download-git-repo') var fs = require('fs')
program .command('init [projectName]') .description('init project') .action(function (projectName, opts) { var loading = ora('fetching template......') inquirer .prompt([ { type: 'input', name: 'projectName', message: '项目名称', default: projectName || 'template' }, { type: 'input', name: 'author', message: '作者' }, { type: 'input', name: 'version', message: '版本', default: '0.1.0' } ]) .then(function (answers) { var repository = 'Yx1aoq1/vue-template' var project = answers.projectName loading.start() download(repository, project, function (err) { if (err) { console.log(err) return } var path = process.cwd() + '\/' + project edit(answers, path) console.log(path) loading.succeed() }) }) })
function edit (info, path) { fs.readFile(path + '/package.json', function (err, data) { if (err) { console.log(err) return } var data = JSON.parse(data) data.name = info.projectName data.author = info.author data.version = info.version var replaceData = JSON.stringify(data, null, 4) fs.writeFile(path + '/package.json', replaceData, function (err) { if (err) { console.log(err) } }) }) }
|