cli.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env node
  2. const spawn = require('cross-spawn');
  3. ('use strict');
  4. // Makes the script crash on unhandled rejections instead of silently
  5. // ignoring them. In the future, promise rejections that are not handled will
  6. // terminate the Node.js process with a non-zero exit code.
  7. process.on('unhandledRejection', (err) => {
  8. throw err;
  9. });
  10. const args = process.argv.slice(2);
  11. const scriptIndex = args.findIndex(
  12. (x) => x === 'build' || x === 'development' || x === 'analyzer' || x === 'rimraf' // TODO
  13. );
  14. const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
  15. const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
  16. if (['build', 'development', 'analyzer', 'rimraf'].includes(script)) {
  17. const result = spawn.sync(
  18. process.execPath,
  19. nodeArgs.concat(require.resolve('./scripts/' + script)).concat(args.slice(scriptIndex + 1)),
  20. { stdio: 'inherit' }
  21. );
  22. if (result.signal) {
  23. if (result.signal === 'SIGKILL') {
  24. console.log(
  25. 'The build failed because the process exited too early. ' +
  26. 'This probably means the system ran out of memory or someone called ' +
  27. '`kill -9` on the process.'
  28. );
  29. } else if (result.signal === 'SIGTERM') {
  30. console.log(
  31. 'The build failed because the process exited too early. ' +
  32. 'Someone might have called `kill` or `killall`, or the system could ' +
  33. 'be shutting down.'
  34. );
  35. }
  36. process.exit(1);
  37. }
  38. process.exit(result.status);
  39. } else {
  40. console.log('Unknown script "' + script + '".');
  41. }