node-gyp.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env node
  2. 'use strict'
  3. process.title = 'node-gyp'
  4. const envPaths = require('env-paths')
  5. const gyp = require('../')
  6. const log = require('../lib/log')
  7. const os = require('os')
  8. /**
  9. * Process and execute the selected commands.
  10. */
  11. const prog = gyp()
  12. let completed = false
  13. prog.parseArgv(process.argv)
  14. prog.devDir = prog.opts.devdir
  15. const homeDir = os.homedir()
  16. if (prog.devDir) {
  17. prog.devDir = prog.devDir.replace(/^~/, homeDir)
  18. } else if (homeDir) {
  19. prog.devDir = envPaths('node-gyp', { suffix: '' }).cache
  20. } else {
  21. throw new Error(
  22. "node-gyp requires that the user's home directory is specified " +
  23. 'in either of the environmental variables HOME or USERPROFILE. ' +
  24. 'Overide with: --devdir /path/to/.node-gyp')
  25. }
  26. if (prog.todo.length === 0) {
  27. if (~process.argv.indexOf('-v') || ~process.argv.indexOf('--version')) {
  28. log.stdout('v%s', prog.version)
  29. } else {
  30. log.stdout('%s', prog.usage())
  31. }
  32. process.exit(0)
  33. }
  34. log.info('it worked if it ends with', 'ok')
  35. log.verbose('cli', process.argv)
  36. log.info('using', 'node-gyp@%s', prog.version)
  37. log.info('using', 'node@%s | %s | %s', process.versions.node, process.platform, process.arch)
  38. /**
  39. * Change dir if -C/--directory was passed.
  40. */
  41. const dir = prog.opts.directory
  42. if (dir) {
  43. const fs = require('fs')
  44. try {
  45. const stat = fs.statSync(dir)
  46. if (stat.isDirectory()) {
  47. log.info('chdir', dir)
  48. process.chdir(dir)
  49. } else {
  50. log.warn('chdir', dir + ' is not a directory')
  51. }
  52. } catch (e) {
  53. if (e.code === 'ENOENT') {
  54. log.warn('chdir', dir + ' is not a directory')
  55. } else {
  56. log.warn('chdir', 'error during chdir() "%s"', e.message)
  57. }
  58. }
  59. }
  60. async function run () {
  61. const command = prog.todo.shift()
  62. if (!command) {
  63. // done!
  64. completed = true
  65. log.info('ok')
  66. return
  67. }
  68. try {
  69. const args = await prog.commands[command.name](command.args) ?? []
  70. if (command.name === 'list') {
  71. if (args.length) {
  72. args.forEach((version) => log.stdout(version))
  73. } else {
  74. log.stdout('No node development files installed. Use `node-gyp install` to install a version.')
  75. }
  76. } else if (args.length >= 1) {
  77. log.stdout(...args.slice(1))
  78. }
  79. // now run the next command in the queue
  80. return run()
  81. } catch (err) {
  82. log.error(command.name + ' error')
  83. log.error('stack', err.stack)
  84. errorMessage()
  85. log.error('not ok')
  86. return process.exit(1)
  87. }
  88. }
  89. process.on('exit', function (code) {
  90. if (!completed && !code) {
  91. log.error('Completion callback never invoked!')
  92. issueMessage()
  93. process.exit(6)
  94. }
  95. })
  96. process.on('uncaughtException', function (err) {
  97. log.error('UNCAUGHT EXCEPTION')
  98. log.error('stack', err.stack)
  99. issueMessage()
  100. process.exit(7)
  101. })
  102. function errorMessage () {
  103. // copied from npm's lib/utils/error-handler.js
  104. const os = require('os')
  105. log.error('System', os.type() + ' ' + os.release())
  106. log.error('command', process.argv
  107. .map(JSON.stringify).join(' '))
  108. log.error('cwd', process.cwd())
  109. log.error('node -v', process.version)
  110. log.error('node-gyp -v', 'v' + prog.package.version)
  111. }
  112. function issueMessage () {
  113. errorMessage()
  114. log.error('', ['Node-gyp failed to build your package.',
  115. 'Try to update npm and/or node-gyp and if it does not help file an issue with the package author.'
  116. ].join('\n'))
  117. }
  118. // start running the given commands!
  119. run()