-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (112 loc) · 3.07 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const program = require('commander')
const _ = require('lodash')
const pkg = require('./package.json')
const version = pkg.version
const { redux, component, feature, config, scaffold } = require('./src')
program
.version(version)
program
.name('node-react')
.usage('[command] [options]')
program
.command('redux')
.option('-a, --add ', 'add redux ...')
.option('-r, --remove ', 'remove redux ...')
.description('redux toolkit ...')
.action( () => {
if (program.args[0].add) {
redux.add()
}
else if (program.args[0].remove) {
redux.remove()
}
else {
var opts = _.last(program.args).options
var desc = _.last(program.args)._description
help('redux [options]', desc, opts)
}
})
program
.command('component')
.option('-a, --add ', 'add component ...')
.option('-r, --remove ', 'remove component ...')
.description('component toolkit ...')
.action( () => {
if (program.args[0].add) {
component.add()
}
else if (program.args[0].remove) {
component.remove()
}
else {
var opts = _.last(program.args).options
var desc = _.last(program.args)._description
help('component [options]', desc, opts)
}
})
program
.command('feature')
.option('-a, --add ', 'add feature ...')
.option('-r, --remove ', 'remove feature ...')
.description('feature toolkit ...')
.action( () => {
if (program.args[0].add) {
feature.add()
}
else if (program.args[0].remove) {
feature.remove()
}
else {
var opts = _.last(program.args).options
var desc = _.last(program.args)._description
help('feature [options]', desc, opts)
}
})
program
.command('config')
.description('set config')
.action( () => {
config.set()
})
program
.command('scaffold')
.option('-u, --update', 'update scaffold ...')
.option('-i, --install', 'install scaffold ...')
.description('scaffold toolkit ...')
.action( () => {
if (program.args[0].update) {
scaffold.update()
}
else if (program.args[0].install) {
scaffold.install()
}
else {
var opts = _.last(program.args).options
var desc = _.last(program.args)._description
help('scaffold [options]', desc, opts)
}
})
// Parse and fallback to help if no args
if (_.isEmpty(program.parse(process.argv).args) && process.argv.length === 2) {
program.help()
}
function outOption(flags, len, description) {
var str = flags
for (var i = flags.length; i < len; i++) {
str += ' '
}
return ` ${str} ${description}`
}
function help(usage, description, options) {
console.log(`\n` +
` Usage: ${usage}\n\n` +
` ${description}\n\n` +
` Options:\n`)
const flags = _.map(options, 'flags').sort( (a, b) => b.length - a.length )
const maxlength = options.length > 0 ? flags[0].length : '-h, --help'.length
//console.log(outOption('-h, --help2', maxlength, 'output usage information'))
options.map( opt => {
console.log(outOption(opt.flags, maxlength, opt.description))
})
console.log('\r')
}