Skip to content

Commit c341fe7

Browse files
author
Chen Asraf
committed
Get comp name from argv
1 parent 652621f commit c341fe7

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export namespace IScaffold {
33
export interface IConfig {
44
templates: string[]
55
output: string | ((path: string) => string)
6+
locals?: any
67
}
78

89
export interface IReplacement {

scaffold.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ import { IScaffold } from './index'
44

55
class Scaffold {
66
private config: IScaffold.IConfig
7+
private locals = {} as any
8+
public scaffoldName = process.argv[2]
79
private DefaultConfig: IScaffold.IConfig = {
810
templates: [],
9-
output: path.resolve(process.cwd())
11+
output: path.resolve(process.cwd()),
12+
locals: {
13+
Name: this.scaffoldName[0].toUpperCase() + this.scaffoldName.slice(1),
14+
name: this.scaffoldName[0].toLowerCase() + this.scaffoldName.slice(1)
15+
}
1016
}
1117

1218
constructor(config: IScaffold.IConfig) {
1319
// this.config = utils.merge<IScaffold.IConfig>(this.DefaultConfig, config)
20+
if (!this.scaffoldName) {
21+
throw new Error('Must provide scaffold name')
22+
}
1423
this.config = (Object as any).assign({}, this.DefaultConfig, config)
24+
this.locals = (Object as any).assign({}, this.DefaultConfig.locals, config.locals)
1525
console.info('Config loaded:', this.config)
26+
console.info('Locals:', this.locals)
1627
}
1728

1829
private parseLocals(text: string): string {
1930
let out = text.toString()
2031
const pattern = /{{\s*(.+)\s*}}/gi
21-
return out.replace(pattern, (match: string, $1: string) => {
22-
const upperName = 'MyComponent'
23-
const lowerName = 'myComponent'
24-
25-
const replaceMap = {
26-
Name: upperName,
27-
name: lowerName,
28-
property: 'someProp'
29-
} as any
30-
31-
return replaceMap[$1]
32-
})
32+
return out.replace(pattern, (match: string, $1: string) => this.locals[$1])
3333
}
3434

3535
private getFileList(pathList: string[]): string[] {
@@ -91,7 +91,10 @@ class Scaffold {
9191
const templateDir = process.cwd() + '/examples'
9292
const scf = new Scaffold({
9393
templates: [templateDir + '/test-input/Component'],
94-
output: templateDir + '/test-output'
94+
output: templateDir + '/test-output',
95+
locals: {
96+
property: 'myProp'
97+
}
9598
})
9699

97100
scf.run()

0 commit comments

Comments
 (0)