1 | #!/usr/bin/env node
|
2 | const program = require('commander')
|
3 | const { resolve } = require('path')
|
4 | const fs = require('fs-extra')
|
5 | const log4js = require('log4js')
|
6 | const mili = require('../lib')
|
7 | const { version } = require('../package.json')
|
8 |
|
9 |
|
10 | const logger = log4js.getLogger('mili-cli')
|
11 | if (process.env.NODE_ENV === 'development') logger.level = 'debug'
|
12 | else logger.level = 'info'
|
13 |
|
14 |
|
15 | function exitWhenThrowError(func) {
|
16 | return async(...arg) => {
|
17 | try {
|
18 | await func(...arg)
|
19 | } catch (e) {
|
20 | console.error(e)
|
21 | process.exit(1)
|
22 | }
|
23 | }
|
24 | }
|
25 |
|
26 | program
|
27 | .version(version)
|
28 |
|
29 | const absolutize = val => resolve(val)
|
30 |
|
31 | program
|
32 | .command('init [repository]')
|
33 | .usage('[options] <repository>')
|
34 | .description('initialize the project')
|
35 | .option('-n --app-name [app_name]', 'Set your app name.')
|
36 | .option('--no-deps', 'Need not install dependencies', false)
|
37 | .option('--force')
|
38 | .option('-v --version [version]', 'Set the template version')
|
39 | .option('--cwd [cwd]', 'Set the current work directory', absolutize)
|
40 | .action(exitWhenThrowError(async(repository, option) => {
|
41 | if (!repository) program.help()
|
42 |
|
43 | const { appName, force = false, deps = true, cwd } = option
|
44 |
|
45 | let version
|
46 | if (typeof option.version === 'string') version = option.version
|
47 | if (cwd) fs.ensureDir(cwd)
|
48 |
|
49 | await mili.init({ cwd, name: appName, force, repository, version, noDeps: !deps })
|
50 | logger.info('initialize complete')
|
51 | }))
|
52 |
|
53 | const collect = (val, memo) => {
|
54 | memo.push(val)
|
55 | return memo
|
56 | }
|
57 |
|
58 | program
|
59 | .command('upgrade')
|
60 | .description('upgrade the template')
|
61 | .option('--force')
|
62 | .option('--no-deps', 'Need not install dependencies', false)
|
63 | .option('-r, --recursive', 'Upgrade recursive all subfolder')
|
64 | .option('--ignore [file]', 'the folder need not search', collect, [])
|
65 | .option('--cwd [cwd]', 'Set the current work directory', absolutize)
|
66 | .action(exitWhenThrowError(async option => {
|
67 | const { cwd, force = false, deps = true, recursive, ignore } = option
|
68 | if (cwd && !fs.pathExistsSync(cwd)) {
|
69 | throw new Error(`No such directory: ${cwd}`)
|
70 | }
|
71 |
|
72 | await mili.upgrade({ cwd, force, noDeps: !deps, recursive, ignore })
|
73 | logger.info('upgraded complete')
|
74 | }))
|
75 |
|
76 | program
|
77 | .command('update')
|
78 | .description('Update the project with the current version of the template')
|
79 | .option('--force')
|
80 | .option('-v --version [version]', 'Set the template version')
|
81 | .option('--no-deps', 'Need not install dependencies', false)
|
82 | .option('-r, --recursive', 'Upgrade recursive all subfolder')
|
83 | .option('--ignore [file]', 'the folder need not search', collect, [])
|
84 | .option('--cwd [cwd]', 'Set the current work directory', absolutize)
|
85 | .action(exitWhenThrowError(async option => {
|
86 | const { cwd, force = false, deps = true, recursive, ignore } = option
|
87 |
|
88 | let version
|
89 | if (typeof option.version === 'string') version = option.version
|
90 | if (cwd && !fs.pathExistsSync(cwd)) {
|
91 | throw new Error(`No such directory: ${cwd}`)
|
92 | }
|
93 |
|
94 | await mili.update({ cwd, force, version, noDeps: !deps, recursive, ignore })
|
95 | logger.info('update complete')
|
96 | }))
|
97 |
|
98 | program
|
99 | .command('clean')
|
100 | .description('Clean the cache of mili')
|
101 | .action(exitWhenThrowError(async() => {
|
102 | logger.info('begin clean')
|
103 | await mili.clean()
|
104 | logger.info('clean complete')
|
105 | }))
|
106 |
|
107 | program
|
108 | .command('outdated')
|
109 | .description('Check template is outdated')
|
110 | .action(exitWhenThrowError(async() => {
|
111 | await mili.outdated()
|
112 | }))
|
113 |
|
114 | program
|
115 | .command('check [files...]')
|
116 | .description('Check if the project file meets the template requirements')
|
117 | .option('--no-deps', 'Need not install dependencies', false)
|
118 | .option('-r, --recursive', 'Upgrade recursive all subfolder')
|
119 | .option('--ignore [file]', 'the folder need not search', collect, [])
|
120 | .option('--cwd [cwd]', 'Set the current work directory', absolutize)
|
121 | .option('-d --diff', 'Show file difference')
|
122 | .option('--fold', 'fold undifferentiated code')
|
123 | .action(exitWhenThrowError(async(files, options) => {
|
124 | const { cwd, deps = true, recursive, ignore, diff, fold } = options
|
125 | if (cwd && !fs.pathExistsSync(cwd)) {
|
126 | throw new Error(`No such directory: ${cwd}`)
|
127 | }
|
128 |
|
129 | try {
|
130 | await mili.check({ cwd, noDeps: !deps, recursive, ignore, showDiff: diff, fold, files })
|
131 | logger.info('check completed')
|
132 | } catch (e) {
|
133 | logger.error(e)
|
134 | process.exit(1)
|
135 | }
|
136 | }))
|
137 |
|
138 | // error on unknown commands
|
139 | program.on('command:*', () => {
|
140 | logger.error(`Invalid command: %s\nSee --help for a list of available commands.${program.args.join(' ')}`)
|
141 | process.exit(1)
|
142 | })
|
143 | program.on('command:*', function (operands) {
|
144 | logger.error(`error: unknown command '${operands[0]}'`);
|
145 | // const availableCommands = program.commands.map(cmd => cmd.name());
|
146 | process.exitCode = 1;
|
147 | });
|
148 |
|
149 |
|
150 | program.parse(process.argv)
|
151 |
|
152 | if (!process.argv.slice(2).length) program.help()
|