import { Command } from "@enzsft/cli" ; import chalk from "chalk" ; import { readJson , writeJson } from "fs-extra" ; import { EOL } from "os" ; import { resolve } from "path" ; import { exec } from "../exec" ; import { createConsoleLogger } from "../logger" ; import { includeOption } from "../options/include" ; import { filterPackages } from "../packages" ; import { MonoRepo , Package , RemoveCommandOptions } from "../types" ; param { Object [ ] } packages param { Object } monoRepo returns { Object } export const createRemoveCommand = ( packages , monoRepo ) : Command < RemoveCommandOptions > => ( { description : "Remove dependencies from packages in your local mono repo." , handler : async ( removePackageNames , 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 ; } logger . log ( `${ chalk . greenBright ( "Target packages:" ) }${ EOL }${ chalk . cyanBright ( targetPackages . map ( p => p . name ) . join ( EOL ) ) }` ) ; for ( const pkg of targetPackages ) { const packageJsonFilePath = resolve ( pkg . __dir , "package.json" ) ; const packageJson = await readJson ( packageJsonFilePath ) ; for ( const removePackageName of removePackageNames ) { if ( packageJson . dependencies ) { delete packageJson . dependencies [ removePackageName ] ; } if ( packageJson . devDependencies ) { delete packageJson . devDependencies [ removePackageName ] ; } } await writeJson ( packageJsonFilePath , packageJson , { spaces : 2 } ) ; } await exec ( "yarn" , [ ] , { cwd : monoRepo . __dir } ) ; } , name : "remove" , options : [ includeOption ] } ) ;