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