import { Command } from "@enzsft/cli" ; import chalk from "chalk" ; import { readJson , writeJson } from "fs-extra" ; import { EOL } from "os" ; import { resolve as resolvePath } from "path" ; import { exec } from "../exec" ; import { createConsoleLogger } from "../logger" ; import { devOption } from "../options/dev" ; import { includeOption } from "../options/include" ; import { filterPackages , getPackageName , getPackageVersion } from "../packages" ; import { AddCommandOptions , MonoRepo , Package } from "../types" ; param { Object [ ] } packages param { Object } monoRepo returns { Object } export const createAddCommand = ( packages , monoRepo ) : Command < AddCommandOptions > => ( { description : "Install dependencies from NPM or your local mono repo." , handler : async ( installPackageNames , options ) : Promise < void > => { const logger = createConsoleLogger ( ) ; if ( ! monoRepo ) { logger . warn ( "Unable to locate your mono repo 😰" ) ; return ; } const targetPackages = filterPackages ( packages , options . include ) ; if ( targetPackages . length === 0 ) { logger . warn ( "No packages found 😰" ) ; return ; } const localInstallPackageNames = installPackageNames . filter ( n => packages . find ( p => p . name === getPackageName ( n ) ) ) ; const npmPackages = await Promise . all ( installPackageNames . filter ( n => ! localInstallPackageNames . includes ( getPackageName ( n ) ) ) . map ( async ( n ) => { const versionSpec = await getPackageVersion ( n , packages ) ; return { __dir : "" , license : "" , name : getPackageName ( n ) , version : `${ versionSpec . modifier }${ versionSpec . version }` } ; } ) ) ; const localPackages = await Promise . all ( installPackageNames . filter ( n => localInstallPackageNames . includes ( getPackageName ( n ) ) ) . map ( async ( n ) => { const versionSpec = await getPackageVersion ( n , packages ) ; return { __dir : "" , license : "" , name : getPackageName ( n ) , version : `${ versionSpec . modifier }${ versionSpec . version }` } ; } ) ) ; const installPackages = [ ... npmPackages , ... localPackages ] ; logger . log ( `${ chalk . greenBright ( "Target packages:" ) }${ EOL }${ chalk . cyanBright ( targetPackages . map ( p => p . name ) . join ( EOL ) ) }` ) ; const dependencyKey = options . dev ? "devDependencies" : "dependencies" ; for ( const pkg of targetPackages ) { const filteredPackages = installPackages . filter ( p => p . name !== pkg . name ) ; if ( filteredPackages . length > 0 ) { const packageJsonFilePath = resolvePath ( pkg . __dir , "package.json" ) ; const packageJson = await readJson ( packageJsonFilePath ) ; if ( packageJson [ dependencyKey ] === undefined ) { packageJson [ dependencyKey ] = { } ; } for ( const installPackage of filteredPackages ) { packageJson [ dependencyKey ] [ installPackage . name ] = `${ installPackage . version }` ; } await writeJson ( packageJsonFilePath , packageJson , { spaces : 2 } ) ; } } await exec ( "yarn" , [ ] , { cwd : monoRepo . __dir } ) ; } , name : "add" , options : [ devOption , includeOption ] } ) ;