UNPKG

1.93 kBJavaScriptView Raw
1#!/usr/bin/env node
2/***
3 * 模版验证
4 * 使用: feflow verify -s <source-template-path> -d <destination-project-path>
5 * 注意:如果省略 -s 参数,则从当前路径进行验证
6 * @author jiakaiguang
7 * @time 2018-05-11
8 */
9const program = require('commander')
10const path = require('path')
11const exists = require('fs').existsSync
12const inquirer = require('inquirer')
13const chalk = require('chalk')
14
15const logger = require('../lib/logger')
16const generate = require('../lib/generate')
17
18/**
19 * Usage
20 */
21program
22 // .usage('[project-name]')
23 .option('-s, --src', 'source template directory') // 源模版路径
24 .option('-d, --dist', 'destination project directory') // 工程存放路径
25
26/**
27 * Help
28 */
29program.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
37program.parse(process.argv)
38if (program.args.length < 1) {
39 return program.help()
40}
41
42const src = program.src // 源模版路径
43const dist = program.dist // 工程存放路径
44
45if(!dist) {
46 return logger.fatal('must use -d option for destination path')
47}
48
49let templatePath
50if(src) {
51 templatePath = program.args[0]
52} else {
53 templatePath = process.cwd()
54}
55
56const destPath = program.args[program.args.length-1]
57const projectName = path.basename(templatePath)
58
59console.log()
60process.on('exit', () => {
61 console.log()
62})
63
64if (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
78async 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}