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