Skip to content

Commit 528cda0

Browse files
committed
chore(cleanup): Handle exists for all spawned processes + deadline implementation + cleanup
1 parent 5b8718f commit 528cda0

File tree

5 files changed

+155
-61
lines changed

5 files changed

+155
-61
lines changed

bin/clever-init

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var path = require('path')
1010
, async = require('async')
1111
, exec = require('child_process').exec
1212
, spawn = require('child_process').spawn
13+
, readline = require('readline')
1314
, Promise = require('bluebird')
1415
, singleSeed = true
1516
, seedsToInstall = []
@@ -112,15 +113,24 @@ function installNPMPackages(projectDir) {
112113
var args = ['install']
113114
, opts = { env: process.env, cwd: projectDir };
114115

115-
if (!!program.verbose) {
116-
opts.stdio = 'inherit';
117-
} else {
116+
if (!program.verbose) {
118117
args.push('--silent');
119118
}
120119

121120
var proc = spawn('npm', args, opts)
122121
, error = '';
123122

123+
if (!!program.verbose) {
124+
readline
125+
.createInterface({
126+
input : proc.stdout,
127+
terminal : false
128+
})
129+
.on('line', function(line) {
130+
console.log(' ' + line);
131+
});
132+
}
133+
124134
proc.on('error', function(err) {
125135
error += err;
126136
});

bin/clever-setup

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var path = require('path')
55
, program = GLOBAL.program = require('commander')
66
, async = require('async')
77
, spawn = require('child_process').spawn
8+
, readline = require('readline')
89
, fs = require('fs')
910
, os = require('os')
1011
, isWin = /^win32/.test(os.platform())
@@ -99,23 +100,39 @@ async.waterfall(
99100
var args = ['install']
100101
, opts = { cwd: _path, env: process.env };
101102

102-
if (program.verbose) {
103-
opts.stdio = 'inherit';
104-
lib.utils.info(' Installing NPM modules for ' + _path + '...');
105-
} else {
103+
lib
104+
.utils
105+
.info(' Installing NPM modules for ' + seed.name + '...')
106+
.running(' Installing NPM modules for ' + seed.name + '...');
107+
108+
if (!program.verbose) {
106109
args.push('--silent');
107-
lib.utils.info(' Installing NPM modules for ' + seed.name + '...');
108110
}
109-
lib.utils.running(' Installing NPM modules for ' + seed.name + '...');
110111

111-
var proc = spawn(!isWin ? 'npm' : 'npm.cmd', args, opts)
112-
, error = '';
112+
var posixProc = spawn(!isWin ? 'npm' : 'npm.cmd', args, opts)
113+
, exitHdlr = process.kill.bind(process, posixProc)
114+
, error = '';
115+
116+
if (!!program.verbose) {
117+
readline
118+
.createInterface({
119+
input : posixProc.stdout,
120+
terminal : false
121+
})
122+
.on('line', function(line) {
123+
console.log(' ' + line);
124+
});
125+
}
113126

114-
proc.on('error', function(err) {
127+
process.on('SIGTERM', exitHdlr);
128+
129+
posixProc.on('error', function(err) {
115130
error += err;
116131
});
117132

118-
proc.on('close', function(code) {
133+
posixProc.on('close', function(code) {
134+
process.removeListener('SIGTERM', exitHdlr);
135+
119136
if (code !== 0) {
120137
return lib.utils.fail(error);
121138
}

lib/project.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
var Promise = require('bluebird')
2-
, path = require('path')
3-
, async = require('async')
4-
, spawn = require('child_process').spawn
5-
, fs = Promise.promisifyAll(require('fs'))
6-
, install = GLOBAL.lib.install
7-
, _bower = GLOBAL.lib.util.bower
8-
, utils = GLOBAL.lib.utils
9-
, os = require('os')
10-
, isWin = /^win32/.test(os.platform());
1+
var Promise = require('bluebird')
2+
, path = require('path')
3+
, async = require('async')
4+
, spawn = require('child_process').spawn
5+
, readline = require('readline')
6+
, fs = Promise.promisifyAll(require('fs'))
7+
, install = GLOBAL.lib.install
8+
, _bower = GLOBAL.lib.util.bower
9+
, utils = GLOBAL.lib.utils
10+
, os = require('os')
11+
, isWin = /^win32/.test(os.platform());
1112

1213
Promise.longStackTraces();
1314

@@ -156,20 +157,34 @@ exports.installModule = function(project, modulePath) {
156157
, args = ['install', '--prefix', projectFolder].concat(deps)
157158
, cmd = !isWin ? 'npm' : 'npm.cmd';
158159

159-
if (!!program.verbose) {
160-
opts.stdio = 'inherit';
161-
} else {
160+
if (!program.verbose) {
162161
args.push('--silent');
163162
}
164163

165-
var proc = spawn(cmd, args, opts)
166-
, error = '';
164+
var posixProc = spawn(cmd, args, opts)
165+
, exitHdlr = process.kill.bind(process, posixProc)
166+
, error = '';
167+
168+
process.on('SIGTERM', exitHdlr);
169+
170+
if (!!program.verbose) {
171+
readline
172+
.createInterface({
173+
input : posixProc.stdout,
174+
terminal : false
175+
})
176+
.on('line', function(line) {
177+
console.log(' ' + line);
178+
});
179+
}
167180

168-
proc.on('error', function(err) {
181+
posixProc.on('error', function(err) {
169182
error += err;
170183
});
171184

172-
proc.on('close', function(code) {
185+
posixProc.on('close', function(code) {
186+
process.removeListener('SIGTERM', exitHdlr);
187+
173188
if (code !== 0 || !!error) {
174189
callback(error);
175190
} else {

lib/util/bower.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
var spawn = require('child_process').spawn
2-
, os = require('os')
3-
, isWin = /^win32/.test(os.platform());
1+
var spawn = require('child_process').spawn
2+
, readline = require('readline')
3+
, os = require('os')
4+
, isWin = /^win32/.test(os.platform());
45

56
/**
67
* Runs bower install
@@ -54,14 +55,25 @@ exports.install = function(modulePath, options, fn) {
5455
additionalOptions.push('--verbose');
5556
}
5657

57-
var proc = spawn(!isWin ? 'bower' : 'bower.cmd', additionalOptions, params)
58+
var posixProc = spawn(!isWin ? 'bower' : 'bower.cmd', additionalOptions, params)
5859
, error;
5960

60-
proc.on('error', function(err) {
61+
if (!!program.verbose || process.mainModule.filename.match('build')) {
62+
readline
63+
.createInterface({
64+
input : posixProc.stdout,
65+
terminal : false
66+
})
67+
.on('line', function(line) {
68+
console.log(' ' + line);
69+
});
70+
}
71+
72+
posixProc.on('error', function(err) {
6173
error += err;
6274
});
6375

64-
proc.on('close', function(code) {
76+
posixProc.on('close', function(code) {
6577
if (code !== 0 || !!error) {
6678
fn(error);
6779
} else {

lib/util/grunt.js

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
var Promise = require('bluebird')
2-
, path = require('path')
3-
, async = require('async')
4-
, fs = require('fs')
5-
, spawn = require('child_process').spawn
6-
, os = require('os')
7-
, isWin = /^win32/.test(os.platform());
1+
var Promise = require('bluebird')
2+
, path = require('path')
3+
, async = require('async')
4+
, fs = require('fs')
5+
, spawn = require('child_process').spawn
6+
, readline = require('readline')
7+
, os = require('os')
8+
, isWin = /^win32/.test(os.platform());
89

910
/**
1011
* Mimmicks a small grunt-cli utility
@@ -107,31 +108,65 @@ var readTasks = exports.readTasks = function(pathSrc, silence) {
107108
* @return {Promise} Returns a promise from bluebird
108109
* @api private
109110
*/
110-
function runTask(projectFolder, cmd) {
111+
exports.runTask = function runTask(projectFolder, command) {
112+
var args = [].slice.call(arguments, 1);
113+
111114
return new Promise(function(resolve, reject) {
112-
var env = process.env
113-
, paths = process.env.NODE_PATH ? [process.env.NODE_PATH] : [];
115+
var cmd = !isWin ? 'grunt' : 'grunt.cmd'
116+
, env = process.env
117+
, opts = {env: env, cwd: projectFolder}
118+
, paths = process.env.NODE_PATH ? [process.env.NODE_PATH] : [];
119+
120+
// Pass the --verbose call down to grunt as well
121+
if (!!program.verbose) {
122+
args.push('--verbose');
123+
124+
var logMsg = 'Spawning posix child process to run '+lib.colors.lightGreen(cmd+' '+args.join(' ')) + '...';
125+
lib.utils.info(logMsg).running(logMsg);
126+
}
114127

115128
paths.push(path.resolve(path.join(projectFolder, 'lib')) + path.sep);
116129
paths.push(path.resolve(path.join(projectFolder, 'modules')) + path.sep);
117130

118-
env.NODE_PATH = paths.join(os.platform() === 'win32' ? ';' : ':');
131+
env.NODE_PATH = paths.join(!!isWin ? ';' : ':');
119132

120-
spawn(!isWin ? 'grunt' : 'grunt.cmd', [ cmd ], { cwd: projectFolder, env: env, stdio: 'inherit' })
121-
.on('close', function(code) {
122-
if (code !== 0) {
123-
return reject();
124-
}
133+
var posixProc = spawn(cmd, args, opts)
134+
, exitHdlr = process.kill.bind(process, posixProc)
135+
, error = '';
125136

126-
if (cmd.indexOf('prompt')) {
127-
lib.utils.progress();
128-
lib.utils.startBar(function() {
129-
resolve();
130-
});
131-
} else {
137+
process.on('SIGTERM', exitHdlr);
138+
139+
if (!!program.verbose || process.mainModule.filename.match('build')) {
140+
readline
141+
.createInterface({
142+
input : posixProc.stdout,
143+
terminal : false
144+
})
145+
.on('line', function(line) {
146+
console.log(' ' + line);
147+
});
148+
}
149+
150+
posixProc.on('error', function(err) {
151+
error += err;
152+
});
153+
154+
posixProc.on('close', function(code) {
155+
process.removeListener('SIGTERM', exitHdlr);
156+
157+
if (code !== 0 || !!error) {
158+
return reject(error);
159+
}
160+
161+
if (command.indexOf('prompt')) {
162+
lib.utils.progress();
163+
lib.utils.startBar(function() {
132164
resolve();
133-
}
134-
});
165+
});
166+
} else {
167+
resolve();
168+
}
169+
});
135170
});
136171
}
137172

@@ -152,7 +187,7 @@ function runDBMigrations(projectFolder) {
152187
paths.push(path.resolve(path.join(projectFolder, 'lib')) + path.sep);
153188
paths.push(path.resolve(path.join(projectFolder, 'modules')) + path.sep);
154189

155-
env.NODE_PATH = paths.join(os.platform() === 'win32' ? ';' : ':');
190+
env.NODE_PATH = paths.join(!!isWin ? ';' : ':');
156191

157192
// check for NODE_ENV json config file if it doesn't exist then revert to local
158193
if (!fs.existsSync(path.join(projectFolder, 'config', env.NOD_ENV + '.json'))) {
@@ -198,8 +233,13 @@ function runDBMigrations(projectFolder) {
198233
*/
199234
exports.runTasks = function(projectFolder, modulePath) {
200235
return new Promise(function(resolve, reject) {
236+
var originalCwd = process.cwd();
237+
process.chdir(projectFolder);
238+
201239
readTasks(modulePath)
202240
.spread(function(tasks) {
241+
242+
process.chdir(originalCwd);
203243

204244
if (tasks.length) {
205245
lib.utils.warn(' Running grunt tasks for module ' + modulePath.split(path.sep).pop() + '...');

0 commit comments

Comments
 (0)