app-scripts-worker-client.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { fork, ChildProcess } from 'child_process';
  2. import { dirname, join } from 'path';
  3. import { MessageToWorker, WorkerProcess } from './interfaces';
  4. export function runWorker(pathToAppScripts: string, debug: boolean, appEntryPoint: string, appNgModulePath: string, srcDir: string, distDir: string, tsConfig: string, ionicAngularDir: string, sassConfigPath: string, copyConfigPath: string, isDev: boolean, minifyCss: boolean, minifyJs: boolean, optimizeJs: boolean) {
  5. return new Promise((resolve, reject) => {
  6. const msgToWorker: MessageToWorker = {
  7. pathToAppScripts: pathToAppScripts,
  8. appEntryPoint: appEntryPoint,
  9. appNgModulePath: appNgModulePath,
  10. debug: debug,
  11. srcDir: srcDir,
  12. distDir: distDir,
  13. tsConfig: tsConfig,
  14. ionicAngularDir: ionicAngularDir,
  15. sassConfigPath: sassConfigPath,
  16. copyConfigPath: copyConfigPath,
  17. isDev: isDev,
  18. minifyCss: minifyCss,
  19. minifyJs: minifyJs,
  20. optimizeJs: optimizeJs
  21. };
  22. const worker = <ChildProcess>createWorker(msgToWorker);
  23. console.log(`Starting to do a ${isDev ? 'dev' : 'prod'} build test ${appEntryPoint}`);
  24. worker.on('error', (err: any) => {
  25. console.error(`worker error, entrypoint: ${appEntryPoint}, pid: ${worker.pid}, error: ${err}`);
  26. reject(err);
  27. });
  28. worker.on('exit', (code: number) => {
  29. console.log(`Finished building test ${appEntryPoint}`);
  30. if (code === 0) {
  31. resolve(code);
  32. } else {
  33. reject(new Error(`${appEntryPoint} exited with non-zero status code`));
  34. }
  35. });
  36. });
  37. }
  38. export function createWorker(msg: MessageToWorker): any {
  39. for (var i = workers.length - 1; i >= 0; i--) {
  40. if (workers[i].appEntryPoint === msg.appEntryPoint) {
  41. try {
  42. workers[i].worker.kill('SIGKILL');
  43. } catch (e) {
  44. console.log(`createWorker, kill('SIGKILL'): ${e}`);
  45. } finally {
  46. delete workers[i].worker;
  47. workers.splice(i, 1);
  48. }
  49. }
  50. }
  51. // default it to use the test/basic, or test/xyz directory
  52. const deepLinksDir = dirname(dirname(msg.appNgModulePath));
  53. try {
  54. let scriptArgs = [
  55. 'build',
  56. '--appEntryPoint', msg.appEntryPoint,
  57. '--appNgModulePath', msg.appNgModulePath,
  58. '--deepLinksDir', deepLinksDir,
  59. '--srcDir', msg.srcDir,
  60. '--wwwDir', msg.distDir,
  61. '--tsconfig', msg.tsConfig,
  62. '--readConfigJson', 'false',
  63. '--experimentalManualTreeshaking', 'false',
  64. '--experimentalPurgeDecorators', 'false',
  65. '--ionicAngularDir', msg.ionicAngularDir,
  66. '--sass', msg.sassConfigPath,
  67. '--copy', msg.copyConfigPath,
  68. '--enableLint', 'false',
  69. '--skipIonicAngularVersion', 'true'
  70. ];
  71. // TODO, use prod once we're a little more settled
  72. if (!msg.isDev) {
  73. scriptArgs.push('--aot');
  74. }
  75. if (msg.debug) {
  76. scriptArgs.push('--debug');
  77. }
  78. if (msg.minifyJs) {
  79. scriptArgs.push('--minifyJs');
  80. }
  81. if (msg.minifyCss) {
  82. scriptArgs.push('--minifyCss');
  83. }
  84. if (msg.optimizeJs) {
  85. scriptArgs.push('--optimizeJs');
  86. }
  87. const workerModule = join(process.cwd(), 'node_modules', '@ionic', 'app-scripts', 'bin', 'ionic-app-scripts.js');
  88. const worker = fork(workerModule, scriptArgs, {
  89. env: {
  90. FORCE_COLOR: true,
  91. npm_config_argv: process.env.npm_config_argv
  92. }
  93. });
  94. workers.push({
  95. appEntryPoint: msg.appEntryPoint,
  96. worker: worker
  97. });
  98. return worker;
  99. } catch (e) {
  100. throw new Error(`unable to create worker-process: ${e.msg}`);
  101. }
  102. }
  103. export const workers: WorkerProcess[] = [];