@@ -21,6 +21,13 @@ Examples:
2121 details,
2222 } ;
2323
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+
2431 // start
2532 api . registerCommand ( 'init' , cmdOpt , args => {
2633 const logger = api . logger ;
@@ -32,12 +39,13 @@ Examples:
3239 return api . applyPluginHooks ( 'beforeCommandInit' , { args } ) ;
3340 } ) ;
3441
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 || { } ) ) ;
4149
4250 chain = chain . then ( info => {
4351 return api . applyPluginHooks ( 'afterCommandInit' , { args, info } ) ;
@@ -50,14 +58,13 @@ Examples:
5058
5159 } ) ;
5260
53- function defaultInit ( args ) {
61+ function checkInitConfig ( args ) {
5462 const logger = api . logger ;
5563 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 ;
5964
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` ) ;
6168
6269 let chain = Promise . resolve ( ) ;
6370
@@ -73,6 +80,77 @@ Examples:
7380 }
7481 } ) ;
7582
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+
76154 // name
77155 chain = chain . then ( ( ) => {
78156 logger . info ( '[Init]' , 'Please enter config:' ) ;
@@ -134,35 +212,6 @@ Examples:
134212 } ) ;
135213 } ) ;
136214
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-
166215 return chain ;
167216 }
168217} ;
0 commit comments