node-gyp.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. 'use strict'
  2. const path = require('path')
  3. const nopt = require('nopt')
  4. const log = require('npmlog')
  5. const childProcess = require('child_process')
  6. const EE = require('events').EventEmitter
  7. const inherits = require('util').inherits
  8. const commands = [
  9. // Module build commands
  10. 'build',
  11. 'clean',
  12. 'configure',
  13. 'rebuild',
  14. // Development Header File management commands
  15. 'install',
  16. 'list',
  17. 'remove'
  18. ]
  19. const aliases = {
  20. ls: 'list',
  21. rm: 'remove'
  22. }
  23. // differentiate node-gyp's logs from npm's
  24. log.heading = 'gyp'
  25. function gyp () {
  26. return new Gyp()
  27. }
  28. function Gyp () {
  29. var self = this
  30. this.devDir = ''
  31. this.commands = {}
  32. commands.forEach(function (command) {
  33. self.commands[command] = function (argv, callback) {
  34. log.verbose('command', command, argv)
  35. return require('./' + command)(self, argv, callback)
  36. }
  37. })
  38. }
  39. inherits(Gyp, EE)
  40. exports.Gyp = Gyp
  41. var proto = Gyp.prototype
  42. /**
  43. * Export the contents of the package.json.
  44. */
  45. proto.package = require('../package.json')
  46. /**
  47. * nopt configuration definitions
  48. */
  49. proto.configDefs = {
  50. help: Boolean, // everywhere
  51. arch: String, // 'configure'
  52. cafile: String, // 'install'
  53. debug: Boolean, // 'build'
  54. directory: String, // bin
  55. make: String, // 'build'
  56. msvs_version: String, // 'configure'
  57. ensure: Boolean, // 'install'
  58. solution: String, // 'build' (windows only)
  59. proxy: String, // 'install'
  60. noproxy: String, // 'install'
  61. devdir: String, // everywhere
  62. nodedir: String, // 'configure'
  63. loglevel: String, // everywhere
  64. python: String, // 'configure'
  65. 'dist-url': String, // 'install'
  66. tarball: String, // 'install'
  67. jobs: String, // 'build'
  68. thin: String // 'configure'
  69. }
  70. /**
  71. * nopt shorthands
  72. */
  73. proto.shorthands = {
  74. release: '--no-debug',
  75. C: '--directory',
  76. debug: '--debug',
  77. j: '--jobs',
  78. silly: '--loglevel=silly',
  79. verbose: '--loglevel=verbose',
  80. silent: '--loglevel=silent'
  81. }
  82. /**
  83. * expose the command aliases for the bin file to use.
  84. */
  85. proto.aliases = aliases
  86. /**
  87. * Parses the given argv array and sets the 'opts',
  88. * 'argv' and 'command' properties.
  89. */
  90. proto.parseArgv = function parseOpts (argv) {
  91. this.opts = nopt(this.configDefs, this.shorthands, argv)
  92. this.argv = this.opts.argv.remain.slice()
  93. var commands = this.todo = []
  94. // create a copy of the argv array with aliases mapped
  95. argv = this.argv.map(function (arg) {
  96. // is this an alias?
  97. if (arg in this.aliases) {
  98. arg = this.aliases[arg]
  99. }
  100. return arg
  101. }, this)
  102. // process the mapped args into "command" objects ("name" and "args" props)
  103. argv.slice().forEach(function (arg) {
  104. if (arg in this.commands) {
  105. var args = argv.splice(0, argv.indexOf(arg))
  106. argv.shift()
  107. if (commands.length > 0) {
  108. commands[commands.length - 1].args = args
  109. }
  110. commands.push({ name: arg, args: [] })
  111. }
  112. }, this)
  113. if (commands.length > 0) {
  114. commands[commands.length - 1].args = argv.splice(0)
  115. }
  116. // support for inheriting config env variables from npm
  117. var npmConfigPrefix = 'npm_config_'
  118. Object.keys(process.env).forEach(function (name) {
  119. if (name.indexOf(npmConfigPrefix) !== 0) {
  120. return
  121. }
  122. var val = process.env[name]
  123. if (name === npmConfigPrefix + 'loglevel') {
  124. log.level = val
  125. } else {
  126. // add the user-defined options to the config
  127. name = name.substring(npmConfigPrefix.length)
  128. // gyp@741b7f1 enters an infinite loop when it encounters
  129. // zero-length options so ensure those don't get through.
  130. if (name) {
  131. this.opts[name] = val
  132. }
  133. }
  134. }, this)
  135. if (this.opts.loglevel) {
  136. log.level = this.opts.loglevel
  137. }
  138. log.resume()
  139. }
  140. /**
  141. * Spawns a child process and emits a 'spawn' event.
  142. */
  143. proto.spawn = function spawn (command, args, opts) {
  144. if (!opts) {
  145. opts = {}
  146. }
  147. if (!opts.silent && !opts.stdio) {
  148. opts.stdio = [0, 1, 2]
  149. }
  150. var cp = childProcess.spawn(command, args, opts)
  151. log.info('spawn', command)
  152. log.info('spawn args', args)
  153. return cp
  154. }
  155. /**
  156. * Returns the usage instructions for node-gyp.
  157. */
  158. proto.usage = function usage () {
  159. var str = [
  160. '',
  161. ' Usage: node-gyp <command> [options]',
  162. '',
  163. ' where <command> is one of:',
  164. commands.map(function (c) {
  165. return ' - ' + c + ' - ' + require('./' + c).usage
  166. }).join('\n'),
  167. '',
  168. 'node-gyp@' + this.version + ' ' + path.resolve(__dirname, '..'),
  169. 'node@' + process.versions.node
  170. ].join('\n')
  171. return str
  172. }
  173. /**
  174. * Version number getter.
  175. */
  176. Object.defineProperty(proto, 'version', {
  177. get: function () {
  178. return this.package.version
  179. },
  180. enumerable: true
  181. })
  182. module.exports = exports = gyp