index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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
  65. console.log('Unknown script "' + script + '".');
  66. }
  67. start();