import { Command } from 'commander' ; import inquirer , { Inquirer } from 'inquirer' ; import { IPlugin , ICommander , IPicGo } from '../types' ; import commanders from '../plugins/commander' ; export class Commander implements ICommander { private list : { [ propName ] : IPlugin ; } ; program : Command ; inquirer : Inquirer ; private readonly ctx : IPicGo ; constructor ( ctx ) { this . list = { } ; this . program = new Command ( ) ; this . inquirer = inquirer ; this . ctx = ctx ; } init ( ) { this . program . version ( process . env . PICGO_VERSION , '-v, --version' ) . option ( '-d, --debug' , 'debug mode' , ( ) => { this . ctx . setConfig ( { debug : true } ) ; } ) . option ( '-s, --silent' , 'silent mode' , ( ) => { this . ctx . setConfig ( { silent : true } ) ; } ) . on ( 'command:*' , ( ) => { this . ctx . log . error ( `Invalid command: ${ this . program . args . join ( ' ' ) }\nSee --help for a list of available commands.` ) ; process . exit ( 1 ) ; } ) ; commanders ( this . ctx ) ; } register ( name , plugin ) { if ( ! name ) throw new TypeError ( 'name is required!' ) ; if ( typeof plugin . handle !== 'function' ) throw new TypeError ( 'plugin.handle must be a function!' ) ; if ( name in this . list ) throw new TypeError ( 'duplicate name!' ) ; this . list [ name ] = plugin ; } loadCommands ( ) { Object . keys ( this . list ) . map ( ( item ) => this . list [ item ] . handle ( this . ctx ) ) ; } get ( name ) { return this . list [ name ] ; } getList ( ) { return Object . keys ( this . list ) . map ( ( item ) => this . list [ item ] ) ; } } export default Commander ;