main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. /**
  3. * Set the title.
  4. */
  5. process.title = 'node-pre-gyp';
  6. const node_pre_gyp = require('../');
  7. const log = require('npmlog');
  8. /**
  9. * Process and execute the selected commands.
  10. */
  11. const prog = new node_pre_gyp.Run({ argv: process.argv });
  12. let completed = false;
  13. if (prog.todo.length === 0) {
  14. if (~process.argv.indexOf('-v') || ~process.argv.indexOf('--version')) {
  15. console.log('v%s', prog.version);
  16. process.exit(0);
  17. } else if (~process.argv.indexOf('-h') || ~process.argv.indexOf('--help')) {
  18. console.log('%s', prog.usage());
  19. process.exit(0);
  20. }
  21. console.log('%s', prog.usage());
  22. process.exit(1);
  23. }
  24. // if --no-color is passed
  25. if (prog.opts && Object.hasOwnProperty.call(prog, 'color') && !prog.opts.color) {
  26. log.disableColor();
  27. }
  28. log.info('it worked if it ends with', 'ok');
  29. log.verbose('cli', process.argv);
  30. log.info('using', process.title + '@%s', prog.version);
  31. log.info('using', 'node@%s | %s | %s', process.versions.node, process.platform, process.arch);
  32. /**
  33. * Change dir if -C/--directory was passed.
  34. */
  35. const dir = prog.opts.directory;
  36. if (dir) {
  37. const fs = require('fs');
  38. try {
  39. const stat = fs.statSync(dir);
  40. if (stat.isDirectory()) {
  41. log.info('chdir', dir);
  42. process.chdir(dir);
  43. } else {
  44. log.warn('chdir', dir + ' is not a directory');
  45. }
  46. } catch (e) {
  47. if (e.code === 'ENOENT') {
  48. log.warn('chdir', dir + ' is not a directory');
  49. } else {
  50. log.warn('chdir', 'error during chdir() "%s"', e.message);
  51. }
  52. }
  53. }
  54. function run() {
  55. const command = prog.todo.shift();
  56. if (!command) {
  57. // done!
  58. completed = true;
  59. log.info('ok');
  60. return;
  61. }
  62. // set binary.host when appropriate. host determines the s3 target bucket.
  63. const target = prog.setBinaryHostProperty(command.name);
  64. if (target && ['install', 'publish', 'unpublish', 'info'].indexOf(command.name) >= 0) {
  65. log.info('using binary.host: ' + prog.package_json.binary.host);
  66. }
  67. prog.commands[command.name](command.args, function(err) {
  68. if (err) {
  69. log.error(command.name + ' error');
  70. log.error('stack', err.stack);
  71. errorMessage();
  72. log.error('not ok');
  73. console.log(err.message);
  74. return process.exit(1);
  75. }
  76. const args_array = [].slice.call(arguments, 1);
  77. if (args_array.length) {
  78. console.log.apply(console, args_array);
  79. }
  80. // now run the next command in the queue
  81. process.nextTick(run);
  82. });
  83. }
  84. process.on('exit', (code) => {
  85. if (!completed && !code) {
  86. log.error('Completion callback never invoked!');
  87. errorMessage();
  88. process.exit(6);
  89. }
  90. });
  91. process.on('uncaughtException', (err) => {
  92. log.error('UNCAUGHT EXCEPTION');
  93. log.error('stack', err.stack);
  94. errorMessage();
  95. process.exit(7);
  96. });
  97. function errorMessage() {
  98. // copied from npm's lib/util/error-handler.js
  99. const os = require('os');
  100. log.error('System', os.type() + ' ' + os.release());
  101. log.error('command', process.argv.map(JSON.stringify).join(' '));
  102. log.error('cwd', process.cwd());
  103. log.error('node -v', process.version);
  104. log.error(process.title + ' -v', 'v' + prog.package.version);
  105. }
  106. // start running the given commands!
  107. run();