import { fork , ChildProcess } from 'child_process' ; import { dirname , join } from 'path' ; import { MessageToWorker , WorkerProcess } from './interfaces' ; export function runWorker ( pathToAppScripts , debug , appEntryPoint , appNgModulePath , srcDir , distDir , tsConfig , ionicAngularDir , sassConfigPath , copyConfigPath , isDev , minifyCss , minifyJs , optimizeJs ) { return new Promise ( ( resolve , reject ) => { const msgToWorker = { pathToAppScripts : pathToAppScripts , appEntryPoint : appEntryPoint , appNgModulePath : appNgModulePath , debug : debug , srcDir : srcDir , distDir : distDir , tsConfig : tsConfig , ionicAngularDir : ionicAngularDir , sassConfigPath : sassConfigPath , copyConfigPath : copyConfigPath , isDev : isDev , minifyCss : minifyCss , minifyJs : minifyJs , optimizeJs : optimizeJs } ; const worker = < ChildProcess > createWorker ( msgToWorker ) ; console . log ( `Starting to do a ${ isDev ? 'dev' : 'prod' } build test ${ appEntryPoint }` ) ; worker . on ( 'error' , ( err ) => { console . error ( `worker error, entrypoint: ${ appEntryPoint }, pid: ${ worker . pid }, error: ${ err }` ) ; reject ( err ) ; } ) ; worker . on ( 'exit' , ( code ) => { console . log ( `Finished building test ${ appEntryPoint }` ) ; if ( code === 0 ) { resolve ( code ) ; } else { reject ( new Error ( `${ appEntryPoint } exited with non-zero status code` ) ) ; } } ) ; } ) ; } export function createWorker ( msg ) { for ( var i = workers . length - 1 ; i >= 0 ; i -- ) { if ( workers [ i ] . appEntryPoint === msg . appEntryPoint ) { try { workers [ i ] . worker . kill ( 'SIGKILL' ) ; } catch ( e ) { console . log ( `createWorker, kill('SIGKILL'): ${ e }` ) ; } finally { delete workers [ i ] . worker ; workers . splice ( i , 1 ) ; } } } const deepLinksDir = dirname ( dirname ( msg . appNgModulePath ) ) ; try { let scriptArgs = [ 'build' , '--appEntryPoint' , msg . appEntryPoint , '--appNgModulePath' , msg . appNgModulePath , '--deepLinksDir' , deepLinksDir , '--srcDir' , msg . srcDir , '--wwwDir' , msg . distDir , '--tsconfig' , msg . tsConfig , '--readConfigJson' , 'false' , '--experimentalManualTreeshaking' , 'false' , '--experimentalPurgeDecorators' , 'false' , '--ionicAngularDir' , msg . ionicAngularDir , '--sass' , msg . sassConfigPath , '--copy' , msg . copyConfigPath , '--enableLint' , 'false' , '--skipIonicAngularVersion' , 'true' ] ; if ( ! msg . isDev ) { scriptArgs . push ( '--aot' ) ; } if ( msg . debug ) { scriptArgs . push ( '--debug' ) ; } if ( msg . minifyJs ) { scriptArgs . push ( '--minifyJs' ) ; } if ( msg . minifyCss ) { scriptArgs . push ( '--minifyCss' ) ; } if ( msg . optimizeJs ) { scriptArgs . push ( '--optimizeJs' ) ; } const workerModule = join ( process . cwd ( ) , 'node_modules' , '@ionic' , 'app-scripts' , 'bin' , 'ionic-app-scripts.js' ) ; const worker = fork ( workerModule , scriptArgs , { env : { FORCE_COLOR : true , npm_config_argv : process . env . npm_config_argv } } ) ; workers . push ( { appEntryPoint : msg . appEntryPoint , worker : worker } ) ; return worker ; } catch ( e ) { throw new Error ( `unable to create worker-process: ${ e . msg }` ) ; } } export const workers = [ ] ;