@@ -30,14 +30,36 @@ export abstract class Template extends BaseClass {
30
30
protected targetDir = process . cwd ( )
31
31
protected _debugPrefix = ''
32
32
private rootPath = ''
33
+ private subDirectory = false
33
34
34
35
constructor ( ) {
35
36
super ( )
36
-
37
+ }
38
+
39
+ // public methods
40
+ public resolveTemplate ( templateId : string ) {
41
+ const template = this . templates . find ( ( x ) => x . id === templateId )
42
+ if ( ! template ) {
43
+ this . debug (
44
+ `Template (${ this . id } ${ this . factory ? `:${ this . factory . id } ` : '' } ):` ,
45
+ `template "${ templateId } " not found`
46
+ )
47
+ } else {
48
+ this . debug (
49
+ `Template (${ this . id } ${ this . factory ? `:${ this . factory . id } ` : '' } ):` ,
50
+ `found template "${ templateId } "`
51
+ )
52
+ }
53
+
54
+ return template
37
55
}
38
56
39
57
public async run ( data : Record < string , any > , flags : any ) : Promise < any > {
40
- this . prepare ( data )
58
+ if ( data ?. subDirectory ) {
59
+ this . subDirectory = data . subDirectory
60
+ }
61
+
62
+ await this . prepare ( data )
41
63
42
64
// 1. gathering: this.data
43
65
this . debug ( `${ this . _debugPrefix } run gathering` )
@@ -73,74 +95,55 @@ export abstract class Template extends BaseClass {
73
95
}
74
96
}
75
97
76
- public resolveTemplate ( templateId : string ) {
77
- const template = this . templates . find ( x => x . id === templateId )
78
- if ( ! template ) {
79
- this . debug (
80
- `Template (${ this . id } ${ this . factory ? `:${ this . factory . id } ` : '' } ):` ,
81
- `template "${ templateId } " not found`
82
- )
83
- } else {
84
- this . debug (
85
- `Template (${ this . id } ${ this . factory ? `:${ this . factory . id } ` : '' } ):` ,
86
- `found template "${ templateId } "`
87
- )
98
+ // processes
99
+ private async prepare ( data ?: any ) {
100
+ this . _debugPrefix = `Template "${ this . id } "`
101
+
102
+ if ( data && isValidObject ( data ) ) {
103
+ this . data = data
88
104
}
89
105
90
- return template
91
- }
106
+ if ( ! this . data . factory || ! this . data ?. factory ?. path ) {
107
+ this . error ( `need path of factory` )
108
+ return this . exit ( )
109
+ }
92
110
111
+ this . rootPath = join ( this . data . factory . path , this . path )
112
+ }
93
113
protected async gathering ( flags : any ) : Promise < any > { }
94
- protected async checking ( ) : Promise < any > { }
95
- protected async writing ( ) : Promise < any > { }
96
- protected async installing ( flags : any ) : Promise < any > { }
97
- protected async ending ( ) : Promise < any > { }
98
-
99
114
private async afterGathering ( ) {
100
- const { project } = this . data
101
- this . targetDir = join ( process . cwd ( ) , ( project && project . name ) || '' )
102
- this . debug ( `${ this . _debugPrefix } rootPath: ${ this . rootPath } targetDir: ${ this . targetDir } ` )
115
+ if ( this . subDirectory ) {
116
+ this . targetDir = join ( this . targetDir , this . data ?. project ?. name || '' )
117
+ }
118
+ this . debug ( `${ this . _debugPrefix } rootPath: ${ this . rootPath } ; targetDir: ${ this . targetDir } ` )
103
119
}
120
+ protected async checking ( ) : Promise < any > { }
104
121
private async afterChecking ( ) { }
122
+ protected async writing ( ) : Promise < any > { }
105
123
private async afterWriting ( ) {
106
124
if ( this . files . copy && isValidArray ( this . files . copy ) ) {
107
125
this . debug ( `${ this . _debugPrefix } start copy` , this . files . copy )
108
126
await this . copy ( this . files . copy )
109
127
}
110
128
111
129
if ( isFunction ( this . renderer ) && this . files . render && isValidArray ( this . files . render ) ) {
112
- this . debug ( `${ this . _debugPrefix } start render` , this . files . render )
113
- await this . render ( this . files . render , this . data , this . renderOptions )
130
+ this . debug ( `${ this . _debugPrefix } start render` , this . files . render , this . files ?. renderOptions )
131
+ await this . render ( this . files . render , this . data , this . files ?. renderOptions )
114
132
}
115
133
}
134
+ protected async installing ( flags : any ) : Promise < any > { }
116
135
private async afterInstalling ( ) { }
136
+ protected async ending ( ) : Promise < any > { }
117
137
private async afterEnding ( ) { }
118
138
119
- private prepare ( data ?: any ) {
120
- this . _debugPrefix = `Template "${ this . id } "`
121
-
122
- if ( data && isValidObject ( data ) ) {
123
- this . data = data
124
- }
125
-
126
- if ( ! this . data . factory || ! this . data . factory . path ) {
127
- this . error ( `need path of factory` )
128
- return this . exit ( )
129
- }
130
-
131
- this . rootPath = join ( this . data . factory . path , this . path )
132
- }
133
-
139
+ // utils
134
140
private async copy ( fileMaps : StringOrFileMap [ ] ) {
135
141
const maps : FileMap [ ] = this . foramtFileMaps ( fileMaps )
136
142
for ( const map of maps ) {
137
143
const paths = await this . globFile ( map )
138
144
const replace = map . to . split ( '/' ) . filter ( Boolean )
139
145
for ( const p of paths ) {
140
- const rest = p
141
- . split ( '/' )
142
- . filter ( Boolean )
143
- . slice ( replace . length )
146
+ const rest = p . split ( '/' ) . filter ( Boolean ) . slice ( replace . length )
144
147
const src = join ( this . rootPath , p )
145
148
if ( ! ( await this . fs . pathExists ( src ) ) ) {
146
149
this . warn ( `${ src } not found` )
@@ -161,7 +164,7 @@ export abstract class Template extends BaseClass {
161
164
private async render (
162
165
fileMaps : StringOrFileMap [ ] ,
163
166
data : Record < string | number , any > ,
164
- options : [ ] | { }
167
+ options ? : [ ] | { }
165
168
) {
166
169
if ( ! this . renderer || ! isFunction ( this . renderer ) ) {
167
170
return
@@ -171,10 +174,7 @@ export abstract class Template extends BaseClass {
171
174
const paths = await this . globFile ( map )
172
175
const replace = map . to . split ( '/' ) . filter ( Boolean )
173
176
for ( const p of paths ) {
174
- const rest = p
175
- . split ( '/' )
176
- . filter ( Boolean )
177
- . slice ( replace . length )
177
+ const rest = p . split ( '/' ) . filter ( Boolean ) . slice ( replace . length )
178
178
const src = join ( this . rootPath , p )
179
179
const stats = await this . fs . stat ( src )
180
180
if ( stats . isFile ( ) ) {
0 commit comments