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