index.js 1.8 KB

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