1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const co = require('co')
|
7 | const prompt = require('co-prompt')
|
8 | const chalk = require('chalk')
|
9 | const tplJson = require('../tpl')
|
10 | const fs = require('fs')
|
11 | const exec = require('child_process').exec
|
12 |
|
13 | module.exports = () => {
|
14 | co(function *() {
|
15 | let tplName = process.argv[3]
|
16 | let projectName = process.argv[4] || 'my-project'
|
17 | let gitUrl
|
18 | let branch
|
19 |
|
20 | if (!tplName) {
|
21 | tplName = yield prompt('Template name: ')
|
22 | } else if (!tplJson.tpl[tplName]) {
|
23 | console.log(chalk.red('\n × Template does not exit!'))
|
24 | process.exit()
|
25 | }
|
26 |
|
27 | gitUrl = tplJson.tpl[tplName].url
|
28 | branch = tplJson.tpl[tplName].branch
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 | if (projectName && fs.existsSync(projectName)) {
|
35 | console.log()
|
36 | console.log(chalk.red('projectName already existed! \n'))
|
37 | process.exit()
|
38 | }
|
39 |
|
40 |
|
41 | let cmdStr = `git clone -b ${branch} ${gitUrl} ${projectName}`
|
42 | console.log(chalk.white('\n Start generating...'))
|
43 |
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 | exec(cmdStr, (error, stdout, stderr) => {
|
50 | if (error) {
|
51 | console.log(error)
|
52 | process.exit()
|
53 | }
|
54 | console.log(chalk.green('\n √ Generation completed!'))
|
55 | console.log(`\n cd ${projectName} && rm -rf .git \n`)
|
56 | process.exit()
|
57 | })
|
58 | })
|
59 | }
|