node-gyp.js 4.6 KB

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