import { Interfaces } from './Interfaces' ; import { Command } from 'commander' ; import { readdirSync } from 'fs' ; import * as path from 'path' ; export function getClassName ( name ) { return `${ name [ 0 ] . toUpperCase ( ) }${ name . slice ( 1 , name . length ) }` ; } export interface IContext { [ key ] : Interfaces . IBuilderModule ; } export function loadModules ( env , context ) { let ls = readdirSync ( path . join ( __dirname , 'modules' ) ) ; for ( const i in ls ) { try { let mod = require ( path . join ( __dirname , 'modules' , ls [ i ] ) ) ; if ( ls [ i ] . slice ( ls [ i ] . length - 3 ) !== '.js' && ls [ i ] . slice ( ls [ i ] . length - 3 ) !== '.ts' ) { continue ; } let moduleClassName = getClassName ( ls [ i ] . slice ( 0 , ls [ i ] . length - 3 ) ) ; let mod_instance = new mod [ moduleClassName ] ( ) ; if ( env . builder . debug ) { console . log ( `loading '${ mod_instance . constructor . name }' module ...` ) ; } let newContext = { commander : new Command ( ) } ; let returnCode = mod_instance . installModule ( env , newContext ) ; if ( returnCode !== 0 ) { console . warn ( `'${ mod_instance . constructor . name }' module exit with code: ${ returnCode }` ) ; } else { context [ mod_instance . constructor . name ] = mod_instance ; context [ mod_instance . constructor . name ] . _ = newContext ; } } catch ( error ) { console . log ( 'failed to load modules' , error . message ) ; process . exit ( ) ; } } return context ; }