You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

node-gyp.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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('npmlog')
  7. const os = require('os')
  8. /**
  9. * Process and execute the selected commands.
  10. */
  11. const prog = gyp()
  12. var completed = false
  13. prog.parseArgv(process.argv)
  14. prog.devDir = prog.opts.devdir
  15. var 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. console.log('v%s', prog.version)
  29. } else {
  30. console.log('%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. var dir = prog.opts.directory
  42. if (dir) {
  43. var fs = require('fs')
  44. try {
  45. var 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. function run () {
  61. var command = prog.todo.shift()
  62. if (!command) {
  63. // done!
  64. completed = true
  65. log.info('ok')
  66. return
  67. }
  68. prog.commands[command.name](command.args, function (err) {
  69. if (err) {
  70. log.error(command.name + ' error')
  71. log.error('stack', err.stack)
  72. errorMessage()
  73. log.error('not ok')
  74. return process.exit(1)
  75. }
  76. if (command.name === 'list') {
  77. var versions = arguments[1]
  78. if (versions.length > 0) {
  79. versions.forEach(function (version) {
  80. console.log(version)
  81. })
  82. } else {
  83. console.log('No node development files installed. Use `node-gyp install` to install a version.')
  84. }
  85. } else if (arguments.length >= 2) {
  86. console.log.apply(console, [].slice.call(arguments, 1))
  87. }
  88. // now run the next command in the queue
  89. process.nextTick(run)
  90. })
  91. }
  92. process.on('exit', function (code) {
  93. if (!completed && !code) {
  94. log.error('Completion callback never invoked!')
  95. issueMessage()
  96. process.exit(6)
  97. }
  98. })
  99. process.on('uncaughtException', function (err) {
  100. log.error('UNCAUGHT EXCEPTION')
  101. log.error('stack', err.stack)
  102. issueMessage()
  103. process.exit(7)
  104. })
  105. function errorMessage () {
  106. // copied from npm's lib/utils/error-handler.js
  107. var os = require('os')
  108. log.error('System', os.type() + ' ' + os.release())
  109. log.error('command', process.argv
  110. .map(JSON.stringify).join(' '))
  111. log.error('cwd', process.cwd())
  112. log.error('node -v', process.version)
  113. log.error('node-gyp -v', 'v' + prog.package.version)
  114. }
  115. function issueMessage () {
  116. errorMessage()
  117. log.error('', ['Node-gyp failed to build your package.',
  118. 'Try to update npm and/or node-gyp and if it does not help file an issue with the package author.'
  119. ].join('\n'))
  120. }
  121. // start running the given commands!
  122. run()