1
1
import { existsSync } from 'fs' ;
2
+ import { env } from './env' ;
2
3
3
- type PackageManager = 'npm' | 'pnpm' | 'yarn' ;
4
+ const supportedPackageManagers = [ 'npm' , 'pnpm' , 'yarn' ] as const ;
5
+ type PackageManager = ( typeof supportedPackageManagers ) [ number ] ;
6
+
7
+ const installCommands : Record < PackageManager , string > = {
8
+ npm : 'npm ci' ,
9
+ pnpm : 'pnpm install --frozen-lockfile' ,
10
+ yarn : 'yarn install --frozen-lockfile' ,
11
+ } ;
12
+ const debugFlags : Record < PackageManager , string > = {
13
+ npm : '--verbose' ,
14
+ pnpm : '--verbose' ,
15
+ yarn : '--verbose' ,
16
+ } ;
4
17
5
18
export function detectPackageManager ( directoryPath : string ) : PackageManager {
6
19
if ( existsSync ( `${ directoryPath } /yarn.lock` ) ) {
@@ -14,13 +27,15 @@ export function detectPackageManager(directoryPath: string): PackageManager {
14
27
return `npm` ;
15
28
}
16
29
17
- export function getInstallCommand ( packageManager : PackageManager ) {
18
- switch ( packageManager ) {
19
- case 'npm' :
20
- return 'npm ci' ;
21
- case 'pnpm' :
22
- return 'pnpm install --frozen-lockfile' ;
23
- case 'yarn' :
24
- return 'yarn install --frozen-lockfile' ;
30
+ export function getInstallCommand ( packageManager : PackageManager ) : string {
31
+ const installCommand = installCommands [ packageManager ] ;
32
+ if ( ! installCommand ) {
33
+ const packageManagers = supportedPackageManagers . join ( ', ' ) ;
34
+ throw new Error (
35
+ `Unsupported package manager: "${ packageManager } ". Options are: ${ packageManagers } ` ,
36
+ ) ;
25
37
}
38
+
39
+ const debugFlag = env . debug ? ` ${ debugFlags [ packageManager ] } ` : '' ;
40
+ return `${ installCommand } ${ debugFlag } ` ;
26
41
}
0 commit comments