@@ -21,6 +21,13 @@ Examples:
21
21
details,
22
22
} ;
23
23
24
+ // default config
25
+ api . otherCommandInit ( async ( { args } ) => {
26
+ if ( args . name ) return { } ; // default, name is empty!!!
27
+ // must be return info
28
+ return defaultInit ( args ) ;
29
+ } ) ;
30
+
24
31
// start
25
32
api . registerCommand ( 'init' , cmdOpt , args => {
26
33
const logger = api . logger ;
@@ -32,12 +39,13 @@ Examples:
32
39
return api . applyPluginHooks ( 'beforeCommandInit' , { args } ) ;
33
40
} ) ;
34
41
35
- if ( ! args . name ) {
36
- chain = chain . then ( ( ) => defaultInit ( args ) ) ;
37
- } else {
38
- // others
39
- chain = chain . then ( ( ) => api . applyPluginHooksAsync ( 'otherCommandInit' , { args } ) ) ;
40
- }
42
+ chain = chain . then ( ( ) => checkInitConfig ( args ) ) ;
43
+
44
+ // must be return info
45
+ chain = chain . then ( ( ) => api . applyPluginHooksAsync ( 'otherCommandInit' , { args } ) ) ;
46
+
47
+ // create config
48
+ chain = chain . then ( finalInfo => saveInitConfig ( args , finalInfo || { } ) ) ;
41
49
42
50
chain = chain . then ( info => {
43
51
return api . applyPluginHooks ( 'afterCommandInit' , { args, info } ) ;
@@ -50,14 +58,13 @@ Examples:
50
58
51
59
} ) ;
52
60
53
- function defaultInit ( args ) {
61
+ function checkInitConfig ( args ) {
54
62
const logger = api . logger ;
55
63
const configDir = api . configDir ;
56
- const configJsFilepath = path . resolve ( configDir , 'index.js' ) ;
57
- const configJsonFilepath = path . resolve ( configDir , 'index.json' ) ;
58
- const pkg = api . pkg ;
59
64
60
- const info = { } ;
65
+ const filename = ( args . name || 'index' ) ;
66
+ const configJsFilepath = path . resolve ( configDir , `${ filename } .js` ) ;
67
+ const configJsonFilepath = path . resolve ( configDir , `${ filename } .json` ) ;
61
68
62
69
let chain = Promise . resolve ( ) ;
63
70
@@ -73,6 +80,77 @@ Examples:
73
80
}
74
81
} ) ;
75
82
83
+ return chain ;
84
+ }
85
+
86
+ function saveInitConfig ( args , info ) {
87
+ const logger = api . logger ;
88
+ const configDir = api . configDir ;
89
+
90
+ let chain = Promise . resolve ( info ) ;
91
+
92
+ // show
93
+ chain = chain . then ( finalInfo => {
94
+ const configJson = JSON . stringify ( finalInfo , false , 4 ) ;
95
+ logger . logo ( `\n${ chalk . grey ( 'Config' ) } : ${ chalk . green ( configJson ) } ` ) ;
96
+ return finalInfo ;
97
+ } ) ;
98
+
99
+ // confirm
100
+ chain = chain . then ( finalInfo => {
101
+ return prompt . confirm ( 'Are you ok?' ) . then ( answer => {
102
+ if ( answer ) {
103
+ return Promise . resolve ( finalInfo ) ;
104
+ }
105
+ return Promise . reject ( 'Cancel !!!' ) ;
106
+ } ) ;
107
+ } ) ;
108
+
109
+ const filename = ( args . name || 'index' ) ;
110
+ const configJsFilepath = path . resolve ( configDir , `${ filename } .js` ) ;
111
+ const configJsonFilepath = path . resolve ( configDir , `${ filename } .json` ) ;
112
+
113
+ // create config
114
+ chain = chain . then ( finalInfo => {
115
+ fs . ensureDirSync ( configDir ) ;
116
+ // delete old file
117
+ fs . removeSync ( configJsFilepath ) ;
118
+ fs . removeSync ( configJsonFilepath ) ;
119
+ // writer new file
120
+ // 优先输出 js
121
+ const jsFileContent = `'use strict';
122
+
123
+ // example ${ filename } .config
124
+ module.exports = {
125
+ ${ Object . entries ( finalInfo ) . map ( ( [ key , vlaue ] ) => {
126
+ let k = '' ;
127
+ if ( typeof key === 'string' && / ^ [ A - Z a - z _ $ ] \w * $ / g. test ( key ) ) {
128
+ k = key ;
129
+ } else {
130
+ k = JSON . stringify ( key ) ;
131
+ }
132
+ return ` ${ k } : ${ JSON . stringify ( vlaue ) } ` ;
133
+ } ) . join ( ',\n' ) }
134
+ };
135
+ ` ;
136
+ // fs.writeJSONSync(configJsonFilepath, finalInfo, { encoding: 'utf8', spaces: 4 });
137
+ // logger.success('[Init]', `Fnished, Path: ${chalk.gray.underline(configJsonFilepath)}`);
138
+ fs . writeFileSync ( configJsFilepath , jsFileContent , { encoding : 'utf8' } ) ;
139
+ logger . success ( '[Init]' , `Fnished, Path: ${ chalk . gray . underline ( configJsFilepath ) } ` ) ;
140
+ return finalInfo ;
141
+ } ) ;
142
+
143
+ return chain ;
144
+ }
145
+
146
+ function defaultInit ( args ) {
147
+ const logger = api . logger ;
148
+ const pkg = api . pkg ;
149
+
150
+ const info = { } ;
151
+
152
+ let chain = Promise . resolve ( ) ;
153
+
76
154
// name
77
155
chain = chain . then ( ( ) => {
78
156
logger . info ( '[Init]' , 'Please enter config:' ) ;
@@ -134,35 +212,6 @@ Examples:
134
212
} ) ;
135
213
} ) ;
136
214
137
- // show
138
- chain = chain . then ( finalInfo => {
139
- const configJson = JSON . stringify ( finalInfo , false , 4 ) ;
140
- logger . logo ( `\n${ chalk . grey ( 'Config' ) } : ${ chalk . green ( configJson ) } ` ) ;
141
- return finalInfo ;
142
- } ) ;
143
-
144
- // confirm
145
- chain = chain . then ( finalInfo => {
146
- return prompt . confirm ( 'Are you ok?' ) . then ( answer => {
147
- if ( answer ) {
148
- return Promise . resolve ( finalInfo ) ;
149
- }
150
- return Promise . reject ( 'Cancel !!!' ) ;
151
- } ) ;
152
- } ) ;
153
-
154
- // create config
155
- chain = chain . then ( finalInfo => {
156
- fs . ensureDirSync ( configDir ) ;
157
- // delete old file
158
- fs . removeSync ( configJsFilepath ) ;
159
- fs . removeSync ( configJsonFilepath ) ;
160
- // writer new file
161
- fs . writeJSONSync ( configJsonFilepath , finalInfo , { encoding : 'utf8' , spaces : 4 } ) ;
162
- logger . success ( '[Init]' , `Fnished, Path: ${ chalk . gray . underline ( configJsonFilepath ) } ` ) ;
163
- return finalInfo ;
164
- } ) ;
165
-
166
215
return chain ;
167
216
}
168
217
} ;
0 commit comments