1#!/usr/bin/env node 2 3'use strict' 4 5process.title = 'node-gyp' 6 7const envPaths = require('env-paths') 8const gyp = require('../') 9const log = require('npmlog') 10const os = require('os') 11 12/** 13 * Process and execute the selected commands. 14 */ 15 16const prog = gyp() 17var completed = false 18prog.parseArgv(process.argv) 19prog.devDir = prog.opts.devdir 20 21var homeDir = os.homedir() 22if (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 33if (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 42log.info('it worked if it ends with', 'ok') 43log.verbose('cli', process.argv) 44log.info('using', 'node-gyp@%s', prog.version) 45log.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 51var dir = prog.opts.directory 52if (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 71function 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 106process.on('exit', function (code) { 107 if (!completed && !code) { 108 log.error('Completion callback never invoked!') 109 issueMessage() 110 process.exit(6) 111 } 112}) 113 114process.on('uncaughtException', function (err) { 115 log.error('UNCAUGHT EXCEPTION') 116 log.error('stack', err.stack) 117 issueMessage() 118 process.exit(7) 119}) 120 121function 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 132function issueMessage () { 133 errorMessage() 134 log.error('', ['This is a bug in `node-gyp`.', 135 'Try to update node-gyp and file an Issue if it does not help:', 136 ' <https://github.com/nodejs/node-gyp/issues>' 137 ].join('\n')) 138} 139 140// start running the given commands! 141run() 142