import * as fs from './fs' ; import * as path from './path' ; import { Config , NPMPackageConfig } from '../types' ; import { logInfo } from '../helpers' ; const kModuleFileExtension = '.js' ; export interface Module { preLink ? : ( config : Config ) => void ; postLink ? : ( config : Config ) => void ; } export interface ModuleList { [ k ] : Module ; } const androidModules = fetchModules ( path . resolve ( __dirname , 'modules' , 'android' ) ) ; const iosModules = fetchModules ( path . resolve ( __dirname , 'modules' , 'ios' ) ) ; function fetchModules ( directory ) { const initialModules = { } ; return fs . readdirSync ( directory ) . filter ( ( filename ) => kModuleFileExtension === path . extname ( filename ) ) . reduce ( ( map , filename ) => { const name = path . basename ( filename , kModuleFileExtension ) ; const src = path . resolve ( directory , filename ) ; map [ name ] = require ( src ) ; return map ; } , initialModules ) ; } param { string [ ] } dependencies param { ModuleList } modules returns { string [ ] } function getUsedModules ( dependencies , modules ) { return dependencies . map ( dependency => { const [ scopeOrModule , module ] = dependency . split ( '/' ) ; return modules [ module || scopeOrModule ] ; } ) . filter ( Boolean ) ; } param { object } packageJSON param { object } configuration param { string } stage export function android ( packageJSON , configuration , stage ) { logInfo ( `Running module scripts for android for init stage ${ stage }` ) ; getUsedModules ( Object . keys ( packageJSON . dependencies || { } ) , androidModules ) . forEach ( exported => { const stageFn = exported [ stage ] ; return stageFn && typeof stageFn === 'function' && stageFn ( configuration ) ; } ) ; } param { object } packageJSON param { object } configuration param { string } stage export function ios ( packageJSON , configuration , stage ) { logInfo ( `Running module scripts for ios for init stage ${ stage }` ) ; getUsedModules ( Object . keys ( packageJSON . dependencies || { } ) , iosModules ) . forEach ( exported => { const stageFn = exported [ stage ] ; return stageFn && typeof stageFn === 'function' && stageFn ( configuration ) ; } ) ; }