[6a3a178] | 1 | #!/usr/bin/env node
|
---|
| 2 |
|
---|
| 3 | 'use strict'
|
---|
| 4 |
|
---|
| 5 | process.title = 'node-gyp'
|
---|
| 6 |
|
---|
| 7 | const envPaths = require('env-paths')
|
---|
| 8 | const gyp = require('../')
|
---|
| 9 | const log = require('npmlog')
|
---|
| 10 | const os = require('os')
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Process and execute the selected commands.
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | const prog = gyp()
|
---|
| 17 | var completed = false
|
---|
| 18 | prog.parseArgv(process.argv)
|
---|
| 19 | prog.devDir = prog.opts.devdir
|
---|
| 20 |
|
---|
| 21 | var homeDir = os.homedir()
|
---|
| 22 | if (prog.devDir) {
|
---|
| 23 | prog.devDir = prog.devDir.replace(/^~/, homeDir)
|
---|
| 24 | } else if (homeDir) {
|
---|
| 25 | prog.devDir = envPaths('node-gyp', { suffix: '' }).cache
|
---|
| 26 | } else {
|
---|
| 27 | throw new Error(
|
---|
| 28 | "node-gyp requires that the user's home directory is specified " +
|
---|
| 29 | 'in either of the environmental variables HOME or USERPROFILE. ' +
|
---|
| 30 | 'Overide with: --devdir /path/to/.node-gyp')
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | if (prog.todo.length === 0) {
|
---|
| 34 | if (~process.argv.indexOf('-v') || ~process.argv.indexOf('--version')) {
|
---|
| 35 | console.log('v%s', prog.version)
|
---|
| 36 | } else {
|
---|
| 37 | console.log('%s', prog.usage())
|
---|
| 38 | }
|
---|
| 39 | process.exit(0)
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | log.info('it worked if it ends with', 'ok')
|
---|
| 43 | log.verbose('cli', process.argv)
|
---|
| 44 | log.info('using', 'node-gyp@%s', prog.version)
|
---|
| 45 | log.info('using', 'node@%s | %s | %s', process.versions.node, process.platform, process.arch)
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Change dir if -C/--directory was passed.
|
---|
| 49 | */
|
---|
| 50 |
|
---|
| 51 | var dir = prog.opts.directory
|
---|
| 52 | if (dir) {
|
---|
| 53 | var fs = require('fs')
|
---|
| 54 | try {
|
---|
| 55 | var stat = fs.statSync(dir)
|
---|
| 56 | if (stat.isDirectory()) {
|
---|
| 57 | log.info('chdir', dir)
|
---|
| 58 | process.chdir(dir)
|
---|
| 59 | } else {
|
---|
| 60 | log.warn('chdir', dir + ' is not a directory')
|
---|
| 61 | }
|
---|
| 62 | } catch (e) {
|
---|
| 63 | if (e.code === 'ENOENT') {
|
---|
| 64 | log.warn('chdir', dir + ' is not a directory')
|
---|
| 65 | } else {
|
---|
| 66 | log.warn('chdir', 'error during chdir() "%s"', e.message)
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | function run () {
|
---|
| 72 | var command = prog.todo.shift()
|
---|
| 73 | if (!command) {
|
---|
| 74 | // done!
|
---|
| 75 | completed = true
|
---|
| 76 | log.info('ok')
|
---|
| 77 | return
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | prog.commands[command.name](command.args, function (err) {
|
---|
| 81 | if (err) {
|
---|
| 82 | log.error(command.name + ' error')
|
---|
| 83 | log.error('stack', err.stack)
|
---|
| 84 | errorMessage()
|
---|
| 85 | log.error('not ok')
|
---|
| 86 | return process.exit(1)
|
---|
| 87 | }
|
---|
| 88 | if (command.name === 'list') {
|
---|
| 89 | var versions = arguments[1]
|
---|
| 90 | if (versions.length > 0) {
|
---|
| 91 | versions.forEach(function (version) {
|
---|
| 92 | console.log(version)
|
---|
| 93 | })
|
---|
| 94 | } else {
|
---|
| 95 | console.log('No node development files installed. Use `node-gyp install` to install a version.')
|
---|
| 96 | }
|
---|
| 97 | } else if (arguments.length >= 2) {
|
---|
| 98 | console.log.apply(console, [].slice.call(arguments, 1))
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // now run the next command in the queue
|
---|
| 102 | process.nextTick(run)
|
---|
| 103 | })
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | process.on('exit', function (code) {
|
---|
| 107 | if (!completed && !code) {
|
---|
| 108 | log.error('Completion callback never invoked!')
|
---|
| 109 | issueMessage()
|
---|
| 110 | process.exit(6)
|
---|
| 111 | }
|
---|
| 112 | })
|
---|
| 113 |
|
---|
| 114 | process.on('uncaughtException', function (err) {
|
---|
| 115 | log.error('UNCAUGHT EXCEPTION')
|
---|
| 116 | log.error('stack', err.stack)
|
---|
| 117 | issueMessage()
|
---|
| 118 | process.exit(7)
|
---|
| 119 | })
|
---|
| 120 |
|
---|
| 121 | function errorMessage () {
|
---|
| 122 | // copied from npm's lib/utils/error-handler.js
|
---|
| 123 | var os = require('os')
|
---|
| 124 | log.error('System', os.type() + ' ' + os.release())
|
---|
| 125 | log.error('command', process.argv
|
---|
| 126 | .map(JSON.stringify).join(' '))
|
---|
| 127 | log.error('cwd', process.cwd())
|
---|
| 128 | log.error('node -v', process.version)
|
---|
| 129 | log.error('node-gyp -v', 'v' + prog.package.version)
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | function issueMessage () {
|
---|
| 133 | errorMessage()
|
---|
| 134 | log.error('', ['Node-gyp failed to build your package.',
|
---|
| 135 | 'Try to update npm and/or node-gyp and if it does not help file an issue with the package author.'
|
---|
| 136 | ].join('\n'))
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | // start running the given commands!
|
---|
| 140 | run()
|
---|