55 */
66
77const chalk = require ( 'chalk' ) ;
8- const generator = require ( './generator ' ) ;
8+ const fs = require ( 'fs-extra ' ) ;
99const prettier = require ( 'prettier' ) ;
1010const path = require ( 'path' ) ;
11+ const op = require ( 'object-path' ) ;
1112const mod = path . dirname ( require . main . filename ) ;
1213const { error, message } = require ( `${ mod } /lib/messenger` ) ;
14+ const GENERATOR = fs . existsSync (
15+ path . normalize ( path . join ( __dirname , 'generator.js' ) ) ,
16+ )
17+ ? require ( './generator' )
18+ : require ( `${ mod } /lib/generator` ) ;
1319
1420/**
1521 * NAME String
@@ -43,6 +49,8 @@ const CANCELED = 'Reactium empty canceled!';
4349 */
4450const CONFIRM = ( { props, params } ) => {
4551 const { prompt } = props ;
52+ if ( op . get ( prompt , 'override.confirm' , false ) === true )
53+ return Promise . resolve ( true ) ;
4654
4755 return new Promise ( ( resolve , reject ) => {
4856 prompt . get (
@@ -141,6 +149,30 @@ const SCHEMA = ({ props }) => {
141149 } ;
142150} ;
143151
152+ /**
153+ * FLAGS
154+ * @description Array of flags passed from the commander options.
155+ * @since 2.0.18
156+ */
157+ const FLAGS = [ 'confirm' , 'demo' , 'font' , 'images' , 'style' , 'toolkit' ] ;
158+
159+ /**
160+ * FLAGS_TO_PARAMS Function
161+ * @description Create an object used by the prompt.override property.
162+ * @since 2.0.18
163+ */
164+ const FLAGS_TO_PARAMS = ( { opt = { } } ) =>
165+ FLAGS . reduce ( ( obj , key ) => {
166+ let val = opt [ key ] ;
167+ val = typeof val === 'function' ? undefined : val ;
168+
169+ if ( val ) {
170+ obj [ key ] = val ;
171+ }
172+
173+ return obj ;
174+ } , { } ) ;
175+
144176/**
145177 * ACTION Function
146178 * @description Function used as the commander.action() callback.
@@ -151,47 +183,37 @@ const SCHEMA = ({ props }) => {
151183 */
152184const ACTION = ( { opt, props } ) => {
153185 const { cwd, prompt } = props ;
154-
155186 const schema = SCHEMA ( { props } ) ;
156-
157- const ovr = [ 'demo' , 'font' , 'images' , 'style' , 'toolkit' ] . reduce (
158- ( obj , key ) => {
159- let val = opt [ key ] ;
160- val = typeof val === 'function' ? null : val ;
161- if ( val ) {
162- obj [ key ] = val ;
163- }
164- return obj ;
165- } ,
166- { } ,
167- ) ;
187+ const ovr = FLAGS_TO_PARAMS ( { opt } ) ;
168188
169189 prompt . override = ovr ;
170190 prompt . start ( ) ;
171- prompt . get ( schema , ( err , input ) => {
172- // Keep this conditional as the first line in this function.
173- // Why? because you will get a js error if you try to set or use anything related to the input object.
174- if ( err ) {
175- prompt . stop ( ) ;
176- error ( `${ NAME } ${ err . message } ` ) ;
177- return ;
178- }
179191
180- const params = { ...CONFORM ( { input, props } ) , ...ovr } ;
181-
182- CONFIRM ( { props, params } )
183- . then ( ( ) => {
184- console . log ( '' ) ;
185- generator ( { params, props } ) . then ( success => {
186- console . log ( '' ) ;
187- } ) ;
188- } )
189- . then ( ( ) => prompt . stop ( ) )
190- . catch ( err => {
192+ let params = { } ;
193+
194+ return new Promise ( ( resolve , reject ) => {
195+ prompt . get ( schema , ( err , input = { } ) => {
196+ if ( err ) {
191197 prompt . stop ( ) ;
192- message ( CANCELED ) ;
193- } ) ;
194- } ) ;
198+ reject ( `${ NAME } ${ err . message } ` ) ;
199+ return ;
200+ }
201+
202+ input = { ...ovr , ...input } ;
203+ params = CONFORM ( { input, props } ) ;
204+ resolve ( ) ;
205+ } ) ;
206+ } )
207+ . then ( ( ) => CONFIRM ( { props, params } ) )
208+ . then ( ( ) => GENERATOR ( { params, props } ) )
209+ . then ( ( ) => prompt . stop ( ) )
210+ . then ( results => {
211+ console . log ( '' ) ;
212+ } )
213+ . catch ( err => {
214+ prompt . stop ( ) ;
215+ message ( op . get ( err , 'message' , CANCELED ) ) ;
216+ } ) ;
195217} ;
196218
197219/**
@@ -202,24 +224,25 @@ const COMMAND = ({ program, props }) =>
202224 program
203225 . command ( NAME )
204226 . description ( DESC )
205- . action ( opt => ACTION ( { opt, props } ) )
206- . option ( '-D, --no-demo [demo] ' , 'Keep the demo site and components.' )
227+ . action ( ( commandName , opt ) => ACTION ( { opt, props } ) )
228+ . option ( '-D, --no-demo' , 'Keep the demo site and components.' )
207229 . option (
208- '-T, --no-toolkit [toolkit] ' ,
230+ '-T, --no-toolkit' ,
209231 'Keep the default toolkit elements.' ,
210232 )
211233 . option (
212- '-S, --no-style [style] ' ,
234+ '-S, --no-style' ,
213235 'Do not empty the ~/src/assets/style/style.scss file.' ,
214236 )
215237 . option (
216- '-F, --no-font [font] ' ,
238+ '-F, --no-font' ,
217239 'Do not empty the ~/src/assets/fonts directory.' ,
218240 )
219241 . option (
220- '-I, --no-images [images] ' ,
242+ '-I, --no-images' ,
221243 'Do not empty the ~/src/assets/images directory.' ,
222244 )
245+ . option ( '-y, --confirm' , 'Skip confirmation.' )
223246 . on ( '--help' , HELP ) ;
224247
225248/**
0 commit comments