1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var debug = require('debug')('appc:run');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function run(installBin, additionalArgs, cb, dontexit) {
|
19 | var async = require('async'),
|
20 | chalk = require('chalk'),
|
21 | spawn = require('child_process').spawn,
|
22 | path = require('path'),
|
23 | fs = require('fs'),
|
24 | util = require('./util'),
|
25 | isWin = /^win/.test(process.platform);
|
26 |
|
27 |
|
28 | var args = [ installBin ].concat(process.argv.splice(2));
|
29 |
|
30 |
|
31 | if (additionalArgs) {
|
32 | args = args.concat(additionalArgs);
|
33 | }
|
34 |
|
35 |
|
36 | var env = process.env;
|
37 |
|
38 |
|
39 | env.NODE_PATH = path.resolve(path.join(path.dirname(installBin), '..', 'node_modules')) + path.delimiter
|
40 |
|
41 | + path.join(util.getCacheDir(), 'node_modules') + path.delimiter
|
42 |
|
43 | + (env.NODE_PATH || '');
|
44 |
|
45 | debug('run with env=%j, args=%o', env, args);
|
46 |
|
47 | async.series([
|
48 | function (next) {
|
49 | var packageDir = path.join(installBin, '..', '..'),
|
50 | isJSON = args.indexOf('json') !== -1,
|
51 | rebuildOpts = { stdio: 'ignore', cwd: packageDir },
|
52 | child;
|
53 |
|
54 | debug('run check package modules version');
|
55 | if (installBin && util.isModuleVersionChanged(installBin)) {
|
56 | util.outputInfo(chalk.yellow('\nYou are attempting to run appc '
|
57 | + util.getActiveVersion()
|
58 | + ' which was compiled for node '
|
59 | + util.getPackageNodeVersion(installBin)
|
60 | + ' but you are now running node ' + process.version + '\n'), isJSON);
|
61 | util.outputInfo(chalk.yellow('Rebuilding package modules ...\n'), isJSON);
|
62 |
|
63 | debug('exec: npm rebuild in dir %s', packageDir);
|
64 | if (/^win/.test(process.platform)) {
|
65 | child = spawn(process.env.comspec, [ '/c', 'npm' ].concat([ 'rebuild' ]), rebuildOpts);
|
66 | } else {
|
67 | child = spawn('npm', [ 'rebuild' ], rebuildOpts);
|
68 | }
|
69 |
|
70 | child.on('exit', function (exit) {
|
71 | if (exit === 0) {
|
72 |
|
73 | util.writeVersions(path.join(packageDir, '..'));
|
74 | util.outputInfo(chalk.yellow('Package modules rebuilt!\n'), isJSON);
|
75 | } else {
|
76 | util.outputInfo(chalk.yellow('The rebuild was unsuccessful, please run the following command to re-build for the newer version:\n'), isJSON);
|
77 | util.outputInfo(chalk.green('appc use ' + util.getActiveVersion() + ' --force\n'), isJSON);
|
78 | }
|
79 | next();
|
80 | });
|
81 | } else {
|
82 | next();
|
83 | }
|
84 | },
|
85 | function (next) {
|
86 |
|
87 |
|
88 | const packageJsonLocation = path.join(installBin, '..', '..', 'package.json');
|
89 | if (!fs.existsSync(packageJsonLocation)) {
|
90 | next();
|
91 | }
|
92 | try {
|
93 | const packageJson = require(packageJsonLocation);
|
94 | if (!packageJson.engines || !packageJson.engines.node) {
|
95 | return next();
|
96 | }
|
97 |
|
98 | util.checkNodeVersion(packageJson.engines.node);
|
99 | } catch (e) { }
|
100 |
|
101 | next();
|
102 |
|
103 | },
|
104 | function (next) {
|
105 |
|
106 | var child = spawn(process.execPath, args, { env: env, stdio: 'inherit' });
|
107 |
|
108 |
|
109 | child.on('close', function (code) {
|
110 | next(null, code);
|
111 | });
|
112 |
|
113 |
|
114 | [ 'SIGTERM', 'SIGUSR1', 'SIGUSR2', 'SIGINT', 'SIGHUP', 'SIGQUIT', 'SIGABRT', 'exit' ].forEach(function (name) {
|
115 | process.on(name, function (code) {
|
116 |
|
117 | if (!isWin) {
|
118 |
|
119 |
|
120 | debug('signal received', name, 'code:', code, ', sending to', child);
|
121 | if (name === 'exit') {
|
122 | child.kill();
|
123 | process.exit(Array.isArray(code) ? code[code.length - 1] : code);
|
124 | } else {
|
125 | child.kill(name);
|
126 | }
|
127 | }
|
128 | });
|
129 | });
|
130 | }
|
131 | ], function (error, results) {
|
132 | if (cb) {
|
133 | cb(results);
|
134 | }
|
135 | if (!dontexit) {
|
136 | process.exit(Array.isArray(results) ? results[results.length - 1] : results);
|
137 | }
|
138 | });
|
139 | }
|
140 |
|
141 | module.exports = run;
|