1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const program = require('commander')
|
10 | const path = require('path')
|
11 | const exists = require('fs').existsSync
|
12 | const inquirer = require('inquirer')
|
13 | const chalk = require('chalk')
|
14 |
|
15 | const logger = require('../lib/logger')
|
16 | const generate = require('../lib/generate')
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | program
|
22 |
|
23 | .option('-s, --src', 'source template directory')
|
24 | .option('-d, --dist', 'destination project directory')
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | program.on('--help', () => {
|
30 | console.log(' Examples:')
|
31 | console.log()
|
32 | console.log(chalk.gray(' # verify a template'))
|
33 | console.log(' $ feflow verify -d /Users/fedor/Downloads/verify')
|
34 | console.log()
|
35 | })
|
36 |
|
37 | program.parse(process.argv)
|
38 | if (program.args.length < 1) {
|
39 | return program.help()
|
40 | }
|
41 |
|
42 | const src = program.src
|
43 | const dist = program.dist
|
44 |
|
45 | if(!dist) {
|
46 | return logger.fatal('must use -d option for destination path')
|
47 | }
|
48 |
|
49 | let templatePath
|
50 | if(src) {
|
51 | templatePath = program.args[0]
|
52 | } else {
|
53 | templatePath = process.cwd()
|
54 | }
|
55 |
|
56 | const destPath = program.args[program.args.length-1]
|
57 | const projectName = path.basename(templatePath)
|
58 |
|
59 | console.log()
|
60 | process.on('exit', () => {
|
61 | console.log()
|
62 | })
|
63 |
|
64 | if (exists(path.resolve(destPath, projectName))) {
|
65 | inquirer.prompt([{
|
66 | type: 'confirm',
|
67 | message: 'Destination directory exists. Continue?',
|
68 | name: 'ok'
|
69 | }]).then(answers => {
|
70 | if (answers.ok) {
|
71 | run()
|
72 | }
|
73 | }).catch(logger.fatal)
|
74 | } else {
|
75 | run()
|
76 | }
|
77 |
|
78 | async function run() {
|
79 | try {
|
80 | await generate(templatePath, destPath)
|
81 | console.log()
|
82 | logger.success('generated project "%s".', projectName)
|
83 | } catch (err) {
|
84 | logger.fatal(err)
|
85 | }
|
86 | }
|