198 lines
4.3 KiB
JavaScript
Raw Normal View History

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 () {
return new Gyp
}
function Gyp () {
2012-06-15 10:00:30 -07:00
var self = this
2012-03-14 16:39:15 -07:00
2012-04-17 17:14:25 -07:00
// set the dir where node-gyp dev files get installed
2012-06-15 10:00:30 -07:00
// TODO: make this *more* configurable?
2012-04-17 17:14:25 -07:00
// see: https://github.com/TooTallNate/node-gyp/issues/21
var homeDir = process.env.HOME || process.env.USERPROFILE
this.devDir = path.resolve(homeDir, '.node-gyp')
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'
, 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'
, nodedir: String // 'configure'
, loglevel: String // everywhere
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'
, silly: '--loglevel=silly'
, verbose: '--loglevel=verbose'
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()
2012-04-17 17:14:25 -07:00
var commands = []
this.argv.slice().forEach(function (arg) {
2012-06-15 10:00:30 -07:00
if (arg in this.commands || arg in this.aliases) {
2012-04-17 17:14:25 -07:00
this.argv.splice(this.argv.indexOf(arg), 1)
commands.push(arg)
}
}, this)
this.todo = commands
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 {
// take the config name and check if it's one that node-gyp cares about
name = name.substring(npm_config_prefix.length)
if (name in this.configDefs) {
this.opts[name] = val
}
}
}, 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) {
opts || (opts = {})
if (!opts.silent && !opts.customFds) {
opts.customFds = [ 0, 1, 2 ]
}
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-06-15 10:00:30 -07:00
* Prints the usage instructions and then exits.
2012-03-14 16:39:15 -07:00
*/
proto.usageAndExit = function usageAndExit () {
var usage = [
''
, ' Usage: node-gyp <command> [options]'
, ''
, ' where <command> is one of:'
, commands.map(function (c) {
return ' - ' + c + ' - ' + require('./' + c).usage
}).join('\n')
, ''
, ' for specific command usage and options try:'
, ' $ node-gyp <command> --help'
, ''
, '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')
console.log(usage)
process.exit(4)
}
/**
* Version number proxy.
*/
Object.defineProperty(proto, 'version', {
get: function () {
return this.package.version
}
, enumerable: true
})