|
1 | 1 | import SimpleScaffold from './scaffold' |
2 | 2 | import * as fs from 'fs' |
3 | 3 | import IScaffold from './index' |
| 4 | +import * as cliArgs from 'command-line-args' |
| 5 | +import * as cliUsage from 'command-line-usage' |
| 6 | +import * as path from 'path' |
4 | 7 |
|
5 | | -const args = process.argv.slice(2) |
| 8 | +type Def = cliArgs.OptionDefinition & { description?: string } |
6 | 9 |
|
7 | | -class ScaffoldCmd { |
8 | | - private config: IScaffold.IConfig |
| 10 | +function localsParser(content: string) { |
| 11 | + const [key, value] = content.split('=') |
| 12 | + return { [key]: value } |
| 13 | +} |
9 | 14 |
|
10 | | - constructor() { |
11 | | - this.config = this.getOptionsFromArgs() |
| 15 | +function filePathParser(content: string) { |
| 16 | + if (content.startsWith('/')) { |
| 17 | + return content |
12 | 18 | } |
| 19 | + return [process.cwd(), content].join(path.sep) |
| 20 | +} |
13 | 21 |
|
14 | | - private getOptionsFromArgs(): IScaffold.IConfig { |
15 | | - let skipNext = false |
16 | | - const options = {} as any |
17 | | - |
18 | | - args.forEach((arg, i) => { |
19 | | - if (skipNext) { |
20 | | - skipNext = false |
21 | | - return |
22 | | - } |
23 | | - |
24 | | - if (arg.slice(0, 2) == '--') { |
25 | | - skipNext = true |
26 | | - let value: string |
27 | | - |
28 | | - if (arg.indexOf('=') >= 0) { |
29 | | - value = arg.split('=').slice(1).join('') |
30 | | - } else if (args.length >= i + 1 && args[i + 1] && args[i + 1].slice(0, 2) !== '--') { |
31 | | - value = args[i + 1] |
32 | | - } else { |
33 | | - value = 'true' |
34 | | - } |
35 | | - |
36 | | - const argName = arg.slice(2) |
37 | | - options[argName] = this.getArgValue(argName, value, options) |
38 | | - } else { |
39 | | - if (!options.name) { |
40 | | - options.name = arg |
41 | | - } else { |
42 | | - throw new TypeError(`Invalid argument: ${arg}`) |
43 | | - } |
44 | | - } |
45 | | - }) |
| 22 | +const defs: Def[] = [ |
| 23 | + { name: 'name', alias: 'n', type: String, description: 'Component output name', defaultOption: true }, |
| 24 | + { name: 'templates', alias: 't', type: filePathParser, multiple: true }, |
| 25 | + { name: 'output', alias: 'o', type: filePathParser, multiple: true }, |
| 26 | + { name: 'locals', alias: 'l', multiple: true, type: localsParser }, |
| 27 | + { name: 'create-sub-folder', alias: 'S', type: Boolean }, |
| 28 | + { name: 'help', alias: 'h', type: Boolean, description: 'Display this help message' }, |
| 29 | +] |
46 | 30 |
|
47 | | - if (!['name', 'templates'].every(o => options[o] !== undefined)) { |
48 | | - throw new Error(`Config is missing keys: ${JSON.stringify(options)}`) |
49 | | - } |
| 31 | +const args = cliArgs(defs, { camelCase: true }) |
50 | 32 |
|
51 | | - return options |
52 | | - } |
| 33 | +const help = [ |
| 34 | + { header: 'Scaffold Generator', content: 'Generate scaffolds for your project based on file templates.' }, |
| 35 | + { header: 'Options', optionList: defs } |
| 36 | +] |
53 | 37 |
|
54 | | - private getArgValue(arg: string, value: string, options: IScaffold.IConfig) { |
55 | | - switch (arg) { |
56 | | - case 'templates': |
57 | | - return (options.templates || []).concat([value]) |
58 | | - case 'output': |
59 | | - return value |
60 | | - case 'locals': |
61 | | - const split = value.split(',') |
62 | | - const locals = options.locals || {} |
63 | | - for (const item of split) { |
64 | | - const [k, v] = item.split('=') |
65 | | - locals[k] = v |
66 | | - } |
67 | | - return locals |
68 | | - default: |
69 | | - throw TypeError(`arguments invalid for config: arg=\`${arg}\`, value=\`${value}\``) |
70 | | - } |
71 | | - } |
| 38 | +args.locals = args.locals.reduce((all: object, cur: object) => ({ ...all, ...cur }), {} as IScaffold.Config['locals']) |
| 39 | +console.info('Config:', args) |
72 | 40 |
|
73 | | - public run() { |
74 | | - const config: IScaffold.IConfig = this.config |
75 | | - console.info('Config:', config) |
76 | | - |
77 | | - const scf = new SimpleScaffold({ |
78 | | - name: config.name, |
79 | | - templates: config.templates, |
80 | | - output: config.output, |
81 | | - locals: config.locals, |
82 | | - }).run() |
83 | | - } |
| 41 | +if (args.help || !args.name) { |
| 42 | + console.log(cliUsage(help)) |
| 43 | + process.exit(0) |
84 | 44 | } |
85 | 45 |
|
86 | | -new ScaffoldCmd().run() |
| 46 | +new SimpleScaffold({ |
| 47 | + name: args.name, |
| 48 | + templates: args.templates, |
| 49 | + output: args.output, |
| 50 | + locals: args.locals, |
| 51 | + createSubfolder: args.createSubFolder, |
| 52 | +}).run() |
0 commit comments