import { promisify } from 'util' ; export class PluginManager { npm : any ; npmReady ? : Promise < void > ; async ensureLoaded ( ) { if ( ! this . npmReady ) { this . npmReady = new Promise ( resolve => { const npm = require ( 'npm' ) ; npm . load ( err => { if ( err ) { console . error ( err ) ; return ; } npm . config . set ( 'global' , false ) ; this . npm = npm ; resolve ( ) ; } ) ; } ) ; } return this . npmReady ; } async install ( path , name , version ) { await this . ensureLoaded ( ) ; this . npm . prefix = path ; return promisify ( this . npm . commands . install ) ( [ `${ name }@${ version }` ] ) ; } async uninstall ( path , name ) { await this . ensureLoaded ( ) ; this . npm . prefix = path ; return promisify ( this . npm . commands . remove ) ( [ name ] ) ; } } export const pluginManager = new PluginManager ( ) ;