@@ -39,6 +39,8 @@ const {
3939 checkPath,
4040 processInstallSnippetReplacements,
4141 promptChoice,
42+ promptChoiceWithEdit,
43+ editScriptInEditor,
4244 colors,
4345} = require ( "./utils" ) ;
4446
@@ -356,16 +358,42 @@ const displayScriptList = (installScripts, log) => {
356358
357359 log . log ( ` ${ continueOption } . Continue to regular installation` ) ;
358360 log . log ( ` ${ exitOption } . Exit installation` ) ;
361+ log . log ( ` e. Edit selected script before running` ) ;
359362} ;
360363
361364const handleSingleScript = async ( script , log ) => {
362365 displayScriptPreview ( script , log ) ;
363366
364- const shouldRun = await confirm (
365- "Run install script (y) or continue to regular installation (n)?" ,
366- ) ;
367+ while ( true ) {
368+ const readline = require ( 'readline' ) ;
369+ const rli = readline . createInterface ( {
370+ input : process . stdin ,
371+ output : process . stdout ,
372+ } ) ;
373+
374+ const choice = await new Promise ( ( resolve ) => {
375+ rli . question ( `${ colors . fg . yellow } Run install script (y/n) or edit (e)?${ colors . reset } ` , ( ans ) => {
376+ rli . close ( ) ;
377+ resolve ( ans . trim ( ) . toLowerCase ( ) ) ;
378+ } ) ;
379+ } ) ;
367380
368- return shouldRun ? script : null ;
381+ if ( choice === 'e' ) {
382+ const editedScript = await editScriptInEditor ( script ) ;
383+ displayScriptPreview ( editedScript , log ) ;
384+
385+ const shouldRun = await confirm (
386+ "Run this edited install script (y) or continue to regular installation (n)?" ,
387+ ) ;
388+
389+ if ( shouldRun ) return editedScript ;
390+ return null ;
391+ } else if ( choice === 'y' || choice === 'yes' ) {
392+ return script ;
393+ } else if ( choice === 'n' || choice === 'no' ) {
394+ return null ;
395+ }
396+ }
369397} ;
370398
371399const handleMultipleScripts = async ( installScripts , log ) => {
@@ -375,25 +403,53 @@ const handleMultipleScripts = async (installScripts, log) => {
375403 while ( true ) {
376404 displayScriptList ( installScripts , log ) ;
377405
378- const choice = await promptChoice (
379- `Preview install script (1-${ EXIT_OPTION } ): ` ,
380- EXIT_OPTION ,
381- ) ;
406+ const readline = require ( 'readline' ) ;
407+ const rli = readline . createInterface ( {
408+ input : process . stdin ,
409+ output : process . stdout ,
410+ } ) ;
382411
383- if ( choice <= installScripts . length ) {
384- const script = installScripts [ choice - 1 ] ;
385- displayScriptPreview ( script , log ) ;
412+ const choice = await new Promise ( ( resolve ) => {
413+ rli . question ( `${ colors . fg . yellow } Preview install script (1-${ EXIT_OPTION } ) or 'e' to edit:${ colors . reset } ` , ( ans ) => {
414+ rli . close ( ) ;
415+ resolve ( ans . trim ( ) . toLowerCase ( ) ) ;
416+ } ) ;
417+ } ) ;
386418
419+ if ( choice === 'e' ) {
420+ const scriptChoice = await promptChoice (
421+ `Which script would you like to edit? (1-${ installScripts . length } ): ` ,
422+ installScripts . length ,
423+ ) ;
424+ const scriptToEdit = installScripts [ scriptChoice - 1 ] ;
425+ const editedScript = await editScriptInEditor ( scriptToEdit ) ;
426+ displayScriptPreview ( editedScript , log ) ;
427+
387428 const shouldRun = await confirm (
388- "Run this install script (y) or choose a different one (n)?" ,
429+ "Run this edited install script (y) or choose a different one (n)?" ,
389430 ) ;
390431
391- if ( shouldRun ) return script ;
432+ if ( shouldRun ) return editedScript ;
392433 // Continue loop if user chooses 'n'
393- } else if ( choice === CONTINUE_OPTION ) {
394- return null ; // Continue to regular installation
395- } else if ( choice === EXIT_OPTION ) {
396- throw new Error ( "Installation aborted by user" ) ;
434+ } else {
435+ const numericChoice = parseInt ( choice ) ;
436+ if ( numericChoice <= installScripts . length ) {
437+ const script = installScripts [ numericChoice - 1 ] ;
438+ displayScriptPreview ( script , log ) ;
439+
440+ const shouldRun = await confirm (
441+ "Run this install script (y) or choose a different one (n)?" ,
442+ ) ;
443+
444+ if ( shouldRun ) return script ;
445+ // Continue loop if user chooses 'n'
446+ } else if ( numericChoice === CONTINUE_OPTION ) {
447+ return null ; // Continue to regular installation
448+ } else if ( numericChoice === EXIT_OPTION ) {
449+ throw new Error ( "Installation aborted by user" ) ;
450+ } else {
451+ console . log ( `${ colors . fg . red } Invalid choice. Please try again.${ colors . reset } ` ) ;
452+ }
397453 }
398454 }
399455} ;
0 commit comments