@@ -5,22 +5,21 @@ import { select } from "@inquirer/prompts";
5
5
import spawn from "@npmcli/promise-spawn" ;
6
6
import ora from "ora" ;
7
7
8
- console . log ( process ) ;
8
+ const contextIsNpmCreate = process . env . npm_command === "init" ;
9
+
10
+ // npm/10.1.0 node/v20.9.0 darwin x64 workspaces/false
11
+ // pnpm/9.1.0 npm/? node/v20.9.0 darwin x64
12
+ // yarn/1.7.0 npm/? node/v8.9.4 darwin x64
13
+ const npmUserAgent = process . env . npm_config_user_agent
14
+ ? Object . fromEntries ( process . env . npm_config_user_agent . split ( " " ) . map ( ( s ) => s . split ( "/" ) ) )
15
+ : { } ;
9
16
10
17
program
11
18
. option ( "--framework <string>" )
12
19
. option ( "--package-manager <string>" )
13
- . option ( "--skip-install" )
14
20
. argument ( "<directory>" , "path to the project's root directory" )
15
21
. action (
16
- async (
17
- dir ,
18
- {
19
- framework,
20
- packageManager,
21
- skipInstall,
22
- } : { framework ?: string ; packageManager ?: string ; skipInstall ?: true } ,
23
- ) => {
22
+ async ( dir , { framework, packageManager } : { framework ?: string ; packageManager ?: string } ) = > {
24
23
// TODO DRY up the validation and error handling, move to commander parse
25
24
if ( framework ) {
26
25
if ( ! [ "angular" , "nextjs" ] . includes ( framework ) ) {
@@ -36,21 +35,24 @@ program
36
35
] ,
37
36
} ) ;
38
37
}
39
- const cloneSpinner = ora ( "Cloning template..." ) . start ( ) ;
40
- // TODO allow different templates
41
- await downloadTemplate (
42
- `gh:FirebaseExtended/firebase-framework-tools/starters/${ framework } /basic` ,
43
- { dir, force : true } ,
44
- ) ;
45
- cloneSpinner . succeed ( ) ;
46
38
// TODO DRY up validation and error message, move to commander parse
39
+ let packageManagerVersion = "*" ;
47
40
if ( packageManager ) {
41
+ [ packageManager , packageManagerVersion = "*" ] = packageManager . split ( "@" ) ;
48
42
if ( ! [ "npm" , "yarn" , "pnpm" ] . includes ( packageManager ) ) {
49
43
console . error (
50
44
`Invalid package manager: ${ packageManager } , valid choices are npm, yarn, and pnpm` ,
51
45
) ;
52
46
process . exit ( 1 ) ;
53
47
}
48
+ } else if ( contextIsNpmCreate ) {
49
+ packageManager = "npm" ;
50
+ } else if ( npmUserAgent . yarn ) {
51
+ packageManager = "yarn ";
52
+ packageManagerVersion = npmUserAgent . yarn ;
53
+ } else if ( npmUserAgent . pnpm ) {
54
+ packageManager = "pnpm ";
55
+ packageManagerVersion = npmUserAgent . pnpm ;
54
56
} else {
55
57
packageManager = await select ( {
56
58
message : "Select a package manager" ,
@@ -62,26 +64,31 @@ program
62
64
] ,
63
65
} ) ;
64
66
}
65
- if ( packageManager !== "npm" ) {
66
- await spawn ( "corepack" , [ "enable" ] , {
67
+ const cloneSpinner = ora ( "Cloning template..." ) . start ( ) ;
68
+ // TODO allow different templates
69
+ await downloadTemplate (
70
+ `gh:FirebaseExtended/firebase-framework-tools/starters/${ framework } /basic` ,
71
+ { dir, force : true } ,
72
+ ) ;
73
+ cloneSpinner . succeed ( ) ;
74
+ if ( packageManager === "npm" ) {
75
+ console . log ( `> ${ packageManager } install` ) ;
76
+ await spawn ( packageManager , [ "install" ] , {
67
77
shell : true ,
68
78
stdio : "inherit" ,
69
79
cwd : dir ,
70
80
} ) ;
71
- await spawn ( "corepack" , [ "use" , `${ packageManager } @*` ] , {
81
+ } else {
82
+ await spawn ( "corepack" , [ "enable" ] , {
72
83
shell : true ,
73
84
stdio : "inherit" ,
74
85
cwd : dir ,
75
86
} ) ;
76
- }
77
- if ( ! skipInstall ) {
78
- const installSpinner = ora ( "Installing dependencies..." ) . start ( ) ;
79
- await spawn ( packageManager , [ "install" ] , {
87
+ await spawn ( "corepack" , [ "use" , `${ packageManager } @${ packageManagerVersion } ` ] , {
80
88
shell : true ,
81
89
stdio : "inherit" ,
82
90
cwd : dir ,
83
91
} ) ;
84
- installSpinner . succeed ( ) ;
85
92
}
86
93
} ,
87
94
) ;
0 commit comments