node-gyp.js 3.5 KB

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