216 lines
4.8 KiB
JavaScript
Raw Normal View History

2012-03-14 16:39:15 -07:00
2013-07-09 13:09:02 -07:00
/**
* Module exports.
*/
2012-03-14 16:39:15 -07:00
module.exports = exports = gyp
/**
* Module dependencies.
*/
2012-04-17 17:14:25 -07:00
var fs = require('graceful-fs')
2012-03-14 16:39:15 -07:00
, path = require('path')
, nopt = require('nopt')
2012-06-15 10:00:30 -07:00
, log = require('npmlog')
2012-03-14 16:39:15 -07:00
, child_process = require('child_process')
, EE = require('events').EventEmitter
, inherits = require('util').inherits
, commands = [
// Module build commands
'build'
, 'clean'
, 'configure'
, 'rebuild'
// Development Header File management commands
, 'install'
, 'list'
, 'remove'
]
, aliases = {
'ls': 'list'
, 'rm': 'remove'
}
// differentiate node-gyp's logs from npm's
2012-06-15 10:00:30 -07:00
log.heading = 'gyp'
2012-03-14 16:39:15 -07:00
/**
* The `gyp` function.
*/
function gyp () {
2013-07-09 13:09:02 -07:00
return new Gyp()
2012-03-14 16:39:15 -07:00
}
function Gyp () {
2012-06-15 10:00:30 -07:00
var self = this
2012-03-14 16:39:15 -07:00
this.devDir = ''
2012-03-14 16:39:15 -07:00
this.commands = {}
2012-04-17 17:14:25 -07:00
2012-03-14 16:39:15 -07:00
commands.forEach(function (command) {
2012-06-15 10:00:30 -07:00
self.commands[command] = function (argv, callback) {
log.verbose('command', command, argv)
return require('./' + command)(self, argv, callback)
2012-03-14 16:39:15 -07:00
}
})
}
inherits(Gyp, EE)
exports.Gyp = Gyp
var proto = Gyp.prototype
/**
* Export the contents of the package.json.
*/
proto.package = require('../package')
2012-04-17 17:14:25 -07:00
/**
* nopt configuration definitions
*/
2012-03-14 16:39:15 -07:00
proto.configDefs = {
2012-07-13 11:40:38 -07:00
help: Boolean // everywhere
, arch: String // 'configure'
, cafile: String // 'install'
2012-07-13 11:40:38 -07:00
, debug: Boolean // 'build'
2012-04-17 17:14:25 -07:00
, directory: String // bin
2012-07-13 11:40:38 -07:00
, make: String // 'build'
2012-03-14 16:39:15 -07:00
, msvs_version: String // 'configure'
2012-07-13 11:40:38 -07:00
, ensure: Boolean // 'install'
, solution: String // 'build' (windows only)
, proxy: String // 'install'
, devdir: String // everywhere
2012-07-13 11:40:38 -07:00
, nodedir: String // 'configure'
, loglevel: String // everywhere
2012-08-21 15:29:03 -07:00
, python: String // 'configure'
2012-10-11 14:11:38 -07:00
, 'dist-url': String // 'install'
2013-10-28 14:10:47 -07:00
, 'tarball': String // 'install'
2012-10-11 14:11:38 -07:00
, jobs: String // 'build'
2013-03-28 11:35:12 -07:00
, thin: String // 'configure'
2012-03-14 16:39:15 -07:00
}
2012-04-17 17:14:25 -07:00
/**
* nopt shorthands
*/
proto.shorthands = {
release: '--no-debug'
, C: '--directory'
2012-06-15 10:00:30 -07:00
, debug: '--debug'
2013-06-06 14:44:48 -07:00
, j: '--jobs'
2012-06-15 10:00:30 -07:00
, silly: '--loglevel=silly'
, verbose: '--loglevel=verbose'
, silent: '--loglevel=silent'
2012-04-17 17:14:25 -07:00
}
2012-06-15 10:00:30 -07:00
/**
* expose the command aliases for the bin file to use.
*/
proto.aliases = aliases
2012-04-17 17:14:25 -07:00
/**
* Parses the given argv array and sets the 'opts',
* 'argv' and 'command' properties.
*/
2012-03-14 16:39:15 -07:00
proto.parseArgv = function parseOpts (argv) {
this.opts = nopt(this.configDefs, this.shorthands, argv)
this.argv = this.opts.argv.remain.slice()
var commands = this.todo = []
// create a copy of the argv array with aliases mapped
2013-07-09 13:09:02 -07:00
argv = this.argv.map(function (arg) {
// is this an alias?
if (arg in this.aliases) {
arg = this.aliases[arg]
2012-04-17 17:14:25 -07:00
}
return arg
2012-04-17 17:14:25 -07:00
}, this)
// process the mapped args into "command" objects ("name" and "args" props)
argv.slice().forEach(function (arg) {
if (arg in this.commands) {
var args = argv.splice(0, argv.indexOf(arg))
argv.shift()
if (commands.length > 0) {
commands[commands.length - 1].args = args
}
commands.push({ name: arg, args: [] })
}
}, this)
if (commands.length > 0) {
commands[commands.length - 1].args = argv.splice(0)
}
2012-06-04 17:32:46 -07:00
// support for inheriting config env variables from npm
var npm_config_prefix = 'npm_config_'
Object.keys(process.env).forEach(function (name) {
if (name.indexOf(npm_config_prefix) !== 0) return
var val = process.env[name]
if (name === npm_config_prefix + 'loglevel') {
2012-06-15 10:00:30 -07:00
log.level = val
2012-06-04 17:32:46 -07:00
} else {
2012-08-01 19:10:42 -07:00
// add the user-defined options to the config
2012-06-04 17:32:46 -07:00
name = name.substring(npm_config_prefix.length)
// gyp@741b7f1 enters an infinite loop when it encounters
// zero-length options so ensure those don't get through.
if (name) this.opts[name] = val
2012-06-04 17:32:46 -07:00
}
}, this)
2012-06-15 10:00:30 -07:00
if (this.opts.loglevel) {
log.level = this.opts.loglevel
}
log.resume()
2012-03-14 16:39:15 -07:00
}
/**
* Spawns a child process and emits a 'spawn' event.
*/
proto.spawn = function spawn (command, args, opts) {
2013-07-09 13:09:02 -07:00
if (!opts) opts = {}
if (!opts.silent && !opts.stdio) {
opts.stdio = [ 0, 1, 2 ]
2012-03-14 16:39:15 -07:00
}
var cp = child_process.spawn(command, args, opts)
2012-06-15 10:00:30 -07:00
log.info('spawn', command)
log.info('spawn args', args)
2012-03-14 16:39:15 -07:00
return cp
}
/**
2012-08-01 19:10:42 -07:00
* Returns the usage instructions for node-gyp.
2012-03-14 16:39:15 -07:00
*/
2012-08-01 19:10:42 -07:00
proto.usage = function usage () {
2013-07-09 13:09:02 -07:00
var str = [
2012-03-14 16:39:15 -07:00
''
, ' Usage: node-gyp <command> [options]'
, ''
, ' where <command> is one of:'
, commands.map(function (c) {
return ' - ' + c + ' - ' + require('./' + c).usage
}).join('\n')
, ''
, 'node-gyp@' + this.version + ' ' + path.resolve(__dirname, '..')
2012-06-15 10:00:30 -07:00
, 'node@' + process.versions.node
2012-03-14 16:39:15 -07:00
].join('\n')
2013-07-09 13:09:02 -07:00
return str
2012-03-14 16:39:15 -07:00
}
/**
2012-08-01 19:10:42 -07:00
* Version number getter.
2012-03-14 16:39:15 -07:00
*/
Object.defineProperty(proto, 'version', {
get: function () {
return this.package.version
}
, enumerable: true
})