Skip to content

Commit d6195c6

Browse files
author
Chen Asraf
committed
added 'createSubFolder' option, cleaned up CMD file
1 parent b14e3d2 commit d6195c6

17 files changed

Lines changed: 2243 additions & 964 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ typings/
5757
# dotenv environment variables file
5858
.env
5959

60-
examples/test-output
60+
examples/test-output/**/*
61+
!examples/test-output/.gitkeep

cmd.ts

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,52 @@
11
import SimpleScaffold from './scaffold'
22
import * as fs from 'fs'
33
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'
47

5-
const args = process.argv.slice(2)
8+
type Def = cliArgs.OptionDefinition & { description?: string }
69

7-
class ScaffoldCmd {
8-
private config: IScaffold.IConfig
10+
function localsParser(content: string) {
11+
const [key, value] = content.split('=')
12+
return { [key]: value }
13+
}
914

10-
constructor() {
11-
this.config = this.getOptionsFromArgs()
15+
function filePathParser(content: string) {
16+
if (content.startsWith('/')) {
17+
return content
1218
}
19+
return [process.cwd(), content].join(path.sep)
20+
}
1321

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+
]
4630

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 })
5032

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+
]
5337

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)
7240

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)
8444
}
8545

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

Comments
 (0)