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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. 'force-process-config': Boolean // 'configure'
  70. }
  71. /**
  72. * nopt shorthands
  73. */
  74. proto.shorthands = {
  75. release: '--no-debug',
  76. C: '--directory',
  77. debug: '--debug',
  78. j: '--jobs',
  79. silly: '--loglevel=silly',
  80. verbose: '--loglevel=verbose',
  81. silent: '--loglevel=silent'
  82. }
  83. /**
  84. * expose the command aliases for the bin file to use.
  85. */
  86. proto.aliases = aliases
  87. /**
  88. * Parses the given argv array and sets the 'opts',
  89. * 'argv' and 'command' properties.
  90. */
  91. proto.parseArgv = function parseOpts (argv) {
  92. this.opts = nopt(this.configDefs, this.shorthands, argv)
  93. this.argv = this.opts.argv.remain.slice()
  94. var commands = this.todo = []
  95. // create a copy of the argv array with aliases mapped
  96. argv = this.argv.map(function (arg) {
  97. // is this an alias?
  98. if (arg in this.aliases) {
  99. arg = this.aliases[arg]
  100. }
  101. return arg
  102. }, this)
  103. // process the mapped args into "command" objects ("name" and "args" props)
  104. argv.slice().forEach(function (arg) {
  105. if (arg in this.commands) {
  106. var args = argv.splice(0, argv.indexOf(arg))
  107. argv.shift()
  108. if (commands.length > 0) {
  109. commands[commands.length - 1].args = args
  110. }
  111. commands.push({ name: arg, args: [] })
  112. }
  113. }, this)
  114. if (commands.length > 0) {
  115. commands[commands.length - 1].args = argv.splice(0)
  116. }
  117. // support for inheriting config env variables from npm
  118. var npmConfigPrefix = 'npm_config_'
  119. Object.keys(process.env).forEach(function (name) {
  120. if (name.indexOf(npmConfigPrefix) !== 0) {
  121. return
  122. }
  123. var val = process.env[name]
  124. if (name === npmConfigPrefix + 'loglevel') {
  125. log.level = val
  126. } else {
  127. // add the user-defined options to the config
  128. name = name.substring(npmConfigPrefix.length)
  129. // gyp@741b7f1 enters an infinite loop when it encounters
  130. // zero-length options so ensure those don't get through.
  131. if (name) {
  132. // convert names like force_process_config to force-process-config
  133. if (name.includes('_')) {
  134. name = name.replace(/_/g, '-')
  135. }
  136. this.opts[name] = val
  137. }
  138. }
  139. }, this)
  140. if (this.opts.loglevel) {
  141. log.level = this.opts.loglevel
  142. }
  143. log.resume()
  144. }
  145. /**
  146. * Spawns a child process and emits a 'spawn' event.
  147. */
  148. proto.spawn = function spawn (command, args, opts) {
  149. if (!opts) {
  150. opts = {}
  151. }
  152. if (!opts.silent && !opts.stdio) {
  153. opts.stdio = [0, 1, 2]
  154. }
  155. var cp = childProcess.spawn(command, args, opts)
  156. log.info('spawn', command)
  157. log.info('spawn args', args)
  158. return cp
  159. }
  160. /**
  161. * Returns the usage instructions for node-gyp.
  162. */
  163. proto.usage = function usage () {
  164. var str = [
  165. '',
  166. ' Usage: node-gyp <command> [options]',
  167. '',
  168. ' where <command> is one of:',
  169. commands.map(function (c) {
  170. return ' - ' + c + ' - ' + require('./' + c).usage
  171. }).join('\n'),
  172. '',
  173. 'node-gyp@' + this.version + ' ' + path.resolve(__dirname, '..'),
  174. 'node@' + process.versions.node
  175. ].join('\n')
  176. return str
  177. }
  178. /**
  179. * Version number getter.
  180. */
  181. Object.defineProperty(proto, 'version', {
  182. get: function () {
  183. return this.package.version
  184. },
  185. enumerable: true
  186. })
  187. module.exports = exports = gyp