@@ -23,11 +23,12 @@ const argv = mri<{
23
23
template ?: string
24
24
help ?: boolean
25
25
overwrite ?: boolean
26
+ immediate ?: boolean
26
27
rolldown ?: boolean
27
28
interactive ?: boolean
28
29
} > ( process . argv . slice ( 2 ) , {
29
- alias : { h : 'help' , t : 'template' } ,
30
- boolean : [ 'help' , 'overwrite' , 'rolldown ', 'interactive' ] ,
30
+ boolean : [ 'help' , 'overwrite' , 'immediate' , 'rolldown' , 'interactive' ] ,
31
+ alias : { h : 'help' , t : 'template ', i : 'immediate' } ,
31
32
string : [ 'template' ] ,
32
33
} )
33
34
const cwd = process . cwd ( )
@@ -41,6 +42,7 @@ When running in TTY, the CLI will start in interactive mode.
41
42
42
43
Options:
43
44
-t, --template NAME use a specific template
45
+ -i, --immediate install dependencies and start dev
44
46
--rolldown / --no-rolldown use / do not use rolldown-vite (Experimental)
45
47
--interactive / --no-interactive force interactive / non-interactive mode
46
48
@@ -344,12 +346,52 @@ const renameFiles: Record<string, string | undefined> = {
344
346
345
347
const defaultTargetDir = 'vite-project'
346
348
349
+ function run ( ...params : Parameters < typeof spawn . sync > ) {
350
+ const { status, error } = spawn . sync ( ...params )
351
+ if ( status != null && status > 0 ) {
352
+ process . exit ( status )
353
+ }
354
+
355
+ if ( error ) {
356
+ console . error ( `\n${ params . slice ( 0 , - 1 ) . join ( ' ' ) } error!` )
357
+ console . error ( error )
358
+ process . exit ( 1 )
359
+ }
360
+ }
361
+
362
+ function install ( root : string , agent : string ) {
363
+ if ( process . env . _VITE_TEST_CLI ) {
364
+ prompts . log . step (
365
+ `Installing dependencies with ${ agent } ... (skipped in test)` ,
366
+ )
367
+ return
368
+ }
369
+ prompts . log . step ( `Installing dependencies with ${ agent } ...` )
370
+ run ( agent , agent === 'yarn' ? [ ] : [ 'install' ] , {
371
+ stdio : 'inherit' ,
372
+ cwd : root ,
373
+ } )
374
+ }
375
+
376
+ function start ( root : string , agent : string ) {
377
+ if ( process . env . _VITE_TEST_CLI ) {
378
+ prompts . log . step ( 'Starting dev server... (skipped in test)' )
379
+ return
380
+ }
381
+ prompts . log . step ( 'Starting dev server...' )
382
+ run ( agent , agent === 'npm' ? [ 'run' , 'dev' ] : [ 'dev' ] , {
383
+ stdio : 'inherit' ,
384
+ cwd : root ,
385
+ } )
386
+ }
387
+
347
388
async function init ( ) {
348
389
const argTargetDir = argv . _ [ 0 ]
349
390
? formatTargetDir ( String ( argv . _ [ 0 ] ) )
350
391
: undefined
351
392
const argTemplate = argv . template
352
393
const argOverwrite = argv . overwrite
394
+ const argImmediate = argv . immediate
353
395
const argRolldown = argv . rolldown
354
396
const argInteractive = argv . interactive
355
397
@@ -499,6 +541,22 @@ async function init() {
499
541
}
500
542
}
501
543
544
+ const pkgManager = pkgInfo ? pkgInfo . name : 'npm'
545
+
546
+ // 5. Ask about immediate install and package manager
547
+ let immediate = argImmediate
548
+ if ( immediate === undefined ) {
549
+ if ( interactive ) {
550
+ const immediateResult = await prompts . confirm ( {
551
+ message : `Install with ${ pkgManager } and start now?` ,
552
+ } )
553
+ if ( prompts . isCancel ( immediateResult ) ) return cancel ( )
554
+ immediate = immediateResult
555
+ } else {
556
+ immediate = false
557
+ }
558
+ }
559
+
502
560
const root = path . join ( cwd , targetDir )
503
561
fs . mkdirSync ( root , { recursive : true } )
504
562
@@ -509,8 +567,6 @@ async function init() {
509
567
template = template . replace ( '-swc' , '' )
510
568
}
511
569
512
- const pkgManager = pkgInfo ? pkgInfo . name : 'npm'
513
-
514
570
const { customCommand } =
515
571
FRAMEWORKS . flatMap ( ( f ) => f . variants ) . find ( ( v ) => v . name === template ) ?? { }
516
572
@@ -613,25 +669,30 @@ async function init() {
613
669
setupReactSwc ( root , template . endsWith ( '-ts' ) )
614
670
}
615
671
616
- let doneMessage = ''
617
- const cdProjectName = path . relative ( cwd , root )
618
- doneMessage += `Done. Now run:\n`
619
- if ( root !== cwd ) {
620
- doneMessage += `\n cd ${
621
- cdProjectName . includes ( ' ' ) ? `"${ cdProjectName } "` : cdProjectName
622
- } `
623
- }
624
- switch ( pkgManager ) {
625
- case 'yarn' :
626
- doneMessage += '\n yarn'
627
- doneMessage += '\n yarn dev'
628
- break
629
- default :
630
- doneMessage += `\n ${ pkgManager } install`
631
- doneMessage += `\n ${ pkgManager } run dev`
632
- break
672
+ if ( immediate ) {
673
+ install ( root , pkgManager )
674
+ start ( root , pkgManager )
675
+ } else {
676
+ let doneMessage = ''
677
+ const cdProjectName = path . relative ( cwd , root )
678
+ doneMessage += `Done. Now run:\n`
679
+ if ( root !== cwd ) {
680
+ doneMessage += `\n cd ${
681
+ cdProjectName . includes ( ' ' ) ? `"${ cdProjectName } "` : cdProjectName
682
+ } `
683
+ }
684
+ switch ( pkgManager ) {
685
+ case 'yarn' :
686
+ doneMessage += '\n yarn'
687
+ doneMessage += '\n yarn dev'
688
+ break
689
+ default :
690
+ doneMessage += `\n ${ pkgManager } install`
691
+ doneMessage += `\n ${ pkgManager } run dev`
692
+ break
693
+ }
694
+ prompts . outro ( doneMessage )
633
695
}
634
- prompts . outro ( doneMessage )
635
696
}
636
697
637
698
function formatTargetDir ( targetDir : string ) {
0 commit comments