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