44
55import { ManagementType , Result } from "./types/deps" ;
66import { shell } from "./shell" ;
7- import { existsSync } from "fs" ;
8- import { join } from "path ";
7+
8+ export type PackageManager = "npm" | "yarn ";
99
1010export async function install ( {
1111 appDir,
1212 dependencies,
1313 devDependencies,
14+ packageManager,
1415} : {
1516 appDir : string ;
1617 dependencies : string [ ] ;
17- devDependencies ?: string [ ] ;
18- } ) {
19- return await manageDependencies ( appDir , dependencies , devDependencies ) ;
20- }
21-
22- async function manageDependencies (
23- appDir : string ,
24- dependencies : string [ ] = [ ] ,
25- devDependencies : string [ ] = [ ]
26- ) : Promise < { result : Result ; packageManager : string } > {
27- const installedDeps = [ ...dependencies , ...devDependencies ] ;
28- console . log ( `Installing ${ installedDeps . join ( ", " ) } ...` ) ;
29-
30- const packageManager = await usePackageManager ( appDir ) ;
31-
18+ devDependencies : string [ ] ;
19+ packageManager : PackageManager ;
20+ } ) : Promise < Result > {
21+ const result : Result = new Map < ManagementType , string [ ] > ( ) ;
3222 await installNpmDevPackage ( devDependencies , packageManager , appDir ) ;
33- await installNpmPackage ( dependencies , packageManager , appDir ) ;
23+ result . set ( ManagementType . Install , devDependencies ) ;
3424
35- const result : Result = new Map < ManagementType , string [ ] > ( ) ;
36- result . set ( ManagementType . Install , installedDeps ) ;
25+ await installNpmPackage ( dependencies , packageManager , appDir ) ;
26+ result . set ( ManagementType . Install , dependencies ) ;
3727
38- return { result, packageManager } ;
28+ return result ;
3929}
4030
41- async function usePackageManager ( appDir : string ) : Promise < "yarn" | "npm" > {
42- const hasYarnLockfile = existsSync ( join ( appDir , "yarn.lock" ) ) ;
43- let yarnChild ;
44- // try yarn first if there is a lockfile
45- if ( hasYarnLockfile ) {
46- yarnChild = await shell ( "yarn" , [ "--version" ] , { stdio : "pipe" } ) ;
47- if ( ! yarnChild . failed ) return "yarn" ;
31+ export async function checkPackageManager ( {
32+ cwd,
33+ packageManager,
34+ } : {
35+ cwd : string ;
36+ packageManager : PackageManager ;
37+ } ) : Promise < boolean > {
38+ try {
39+ await shell ( packageManager , [ "--version" ] , { stdio : "pipe" , cwd } ) ;
40+ return true ;
41+ } catch ( error ) {
42+ throw new Error (
43+ `Must have ${ packageManager } installed to manage dependencies. Is either in your PATH? We tried running in ${ cwd } `
44+ ) ;
4845 }
49-
50- // try npm then as the "default"
51- const npmChild = await shell ( "npm" , [ "--version" ] , { stdio : "pipe" } ) ;
52- if ( ! npmChild . failed ) return "npm" ;
53-
54- // try yarn as maybe only yarn is installed
55- if ( yarnChild && ! yarnChild . failed ) return "yarn" ;
56-
57- // if we have reached here, we can't seem to run anything
58- throw new Error (
59- `Must have npm or yarn installed to manage dependencies. Is either in your PATH? We tried running in ${ appDir } `
60- ) ;
6146}
6247
6348async function installNpmPackage (
6449 packageNames : string [ ] ,
65- packageManager : string ,
50+ packageManager : PackageManager ,
6651 appDir : string
6752) : Promise < void > {
6853 if ( packageNames . length === 0 ) return ;
54+ console . log ( `Installing ${ packageNames . join ( ", " ) } ...` ) ;
6955 if ( packageManager === "yarn" ) {
7056 await shell ( "yarn" , [ "add" , packageNames . join ( " " ) ] , {
7157 cwd : appDir ,
@@ -79,10 +65,11 @@ async function installNpmPackage(
7965
8066async function installNpmDevPackage (
8167 packageNames : string [ ] ,
82- packageManager : string ,
68+ packageManager : PackageManager ,
8369 appDir : string
8470) : Promise < void > {
8571 if ( packageNames . length === 0 ) return ;
72+ console . log ( `Installing ${ packageNames . join ( ", " ) } ...` ) ;
8673 if ( packageManager === "yarn" ) {
8774 await shell (
8875 "yarn" ,
0 commit comments