-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (55 loc) · 1.66 KB
/
index.js
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
#!/usr/bin/env node
// 处理用户输入的命令
const program = require('commander')
// 下载模板
const download = require('download-git-repo')
// node 文件模块
const fs = require('fs')
// 填充信息至文件
const handlebars = require('handlebars')
// 动画效果
const ora = require('ora')
// 字体加颜色
const chalk = require('chalk')
// 显示提示图标
const symbols = require('log-symbols')
// 命令行操作
var shell = require('shelljs')
var chooseType = require('./chooseType.js')
var installModules = require('./installModules.js')
// 不同项目的git地址
const gitAddress = {
Vue: 'xiao-team/xiao-cli-template#vue',
library: 'xiao-team/xiao-cli-template#library',
}
program
.version(require('../package').version)
.command('init <name>')
.action(function (name) {
chooseType().then(answers => {
const spinner = ora('模板下载中...')
spinner.start()
download(gitAddress[answers.projectType], name, err => {
if (err) {
spinner.fail()
console.log(symbols.error, 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(symbols.success, chalk.green('模板下载成功!'))
installModules(name)
}
})
})
})
program.parse(process.argv)