index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env node
  2. const spawn = require('cross-spawn');
  3. const {resolve} = require('path');
  4. const {getSWTEnv} = require('../config/env');
  5. process.on('unhandledRejection', function (err) {
  6. throw err;
  7. });
  8. const args = process.argv.slice(2);
  9. const scriptIndex = args.findIndex(x => x === 'build' || x === 'test' || x === 'dev');
  10. let script = scriptIndex === -1 ? args[0] : args[scriptIndex];
  11. const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
  12. function setEnv(name) {
  13. switch (name) {
  14. case 'dev':
  15. process.env.NODE_ENV = 'development';
  16. break;
  17. case 'e2e':
  18. process.env.NODE_ENV = 'development';
  19. process.env.IS_E2E = 'true';
  20. script = 'dev';
  21. break;
  22. case 'build':
  23. process.env.NODE_ENV = 'production';
  24. break;
  25. case 'test':
  26. process.env.NODE_ENV = 'test';
  27. break;
  28. }
  29. const env = getSWTEnv();
  30. Object.assign(process.env, env);
  31. }
  32. function runScript() {
  33. const result = spawn.sync(
  34. process.execPath,
  35. nodeArgs
  36. .concat(resolve(__dirname, `../scripts/${script}`))
  37. .concat(process.argv.slice(scriptIndex + 1)),
  38. {stdio: 'inherit'},
  39. );
  40. if (result.signal) {
  41. if (result.signal === 'SIGKILL')
  42. console.log(
  43. 'The build failed because the process exited too early. ' +
  44. 'This probably means the system ran out of memory or someone called ' +
  45. '`kill -9` on the process.',
  46. );
  47. else if (result.signal === 'SIGTERM')
  48. console.log(
  49. 'The build failed because the process exited too early. ' +
  50. 'Someone might have called `kill` or `killall`, or the system could ' +
  51. 'be shutting down.',
  52. );
  53. process.exit(1);
  54. }
  55. return result;
  56. }
  57. function start() {
  58. if (['build', 'dev', 'test', 'e2e'].includes(script)) {
  59. setEnv(script);
  60. const {status} = runScript();
  61. process.exit(status);
  62. } else console.log('Unknown script "' + script + '".');
  63. }
  64. start();