index.js 1.9 KB

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