import { AbstractRunner } from '../runners/abstract.runner' ; import { PackageManagerCommands } from './types' ; type SaveType = 'prod' | 'dev' ; interface Options { useFlagForInstall : boolean ; useFlagForUninstall : boolean ; } const DEFAULT_OPTIONS = { useFlagForInstall : true , useFlagForUninstall : true , } ; export abstract class AbstractPackageManager { constructor ( protected runner , protected customOptions = { } ) { this . options = { ... DEFAULT_OPTIONS , ... customOptions , } ; } private options : Options ; public abstract get cli ( ) : PackageManagerCommands ; private async manageDependencies ( { command , dependencies , saveType , exact = false , usingWorkspace = false , } ) { const dependenciesString = dependencies . join ( ' ' ) ; const args = [ ] ; args . push ( command ) ; if ( saveType === 'prod' ) { args . push ( this . cli . saveFlag ) ; } if ( saveType === 'dev' ) { args . push ( this . cli . saveDevFlag ) ; } if ( exact ) { args . push ( this . cli . exactFlag ) ; } if ( ! usingWorkspace ) { args . push ( this . cli . rootFlag ) ; } args . push ( dependenciesString ) ; args . push ( '--silent' ) ; const argsString = args . filter ( Boolean ) . join ( ' ' ) ; const collect = true ; await this . runner . run ( argsString , collect ) ; } public async install ( { dependencies , saveType , workspace , exact = false , } ) { const additionalOptions = { } ; if ( this . options . useFlagForInstall ) { additionalOptions . saveType = saveType ; } if ( workspace ) { return this . manageDependencies ( { ... additionalOptions , command : this . cli . installWorkspace ( workspace ) , dependencies , exact , usingWorkspace : true , } ) ; } return this . manageDependencies ( { ... additionalOptions , command : this . cli . install , dependencies , exact , } ) ; } public async uninstall ( { dependencies , saveType , workspace , } ) { const additionalOptions = { } ; if ( this . options . useFlagForUninstall ) { additionalOptions . saveType = saveType ; } if ( workspace ) { return this . manageDependencies ( { ... additionalOptions , command : this . cli . uninstallWorkspace ( workspace ) , dependencies , usingWorkspace : true , } ) ; } return this . manageDependencies ( { ... additionalOptions , command : this . cli . uninstall , dependencies , } ) ; } public async version ( ) { const commandArguments = '--version' ; const collect = true ; return this . runner . run ( commandArguments , collect ) as Promise < string > ; } }