Skip to content

Commit 6a8c379

Browse files
author
David Cheung
committed
Fix process.stdout exiting before done
In Node v6 process.nextTick exit causes long messages from stdout.write to exit before the data is finished writing
1 parent 25a57f4 commit 6a8c379

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

bin/lb-ng.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var SG = require('strong-globalize');
55
SG.SetRootDir(path.resolve(__dirname, '..'));
66
var g = SG();
77
var fs = require('fs');
8+
var Promise = require('bluebird');
89
var semver = require('semver');
910
var optimist = require('optimist');
1011
var generator = require('loopback-sdk-angular');
@@ -55,9 +56,13 @@ function runGenerator() {
5556
// used to have a bug where it prevented the application from exiting.
5657
// To work around that issue, we are explicitly exiting here.
5758
//
58-
// The exit is deferred to the next tick in order to prevent the Node bug:
59+
// The exit is deferred to both stdout and err is drained
60+
// in order to prevent the Node bug:
5961
// https://github.com/joyent/node/issues/3584
60-
process.nextTick(function() {
62+
Promise.all([
63+
waitForEvent(process.stdout, 'drain'),
64+
waitForEvent(process.stderr, 'drain')
65+
]).then(function() {
6166
process.exit();
6267
});
6368
}
@@ -79,3 +84,10 @@ function assertLoopBackVersion() {
7984
process.exit(1);
8085
}
8186
}
87+
88+
function waitForEvent(obj, name) {
89+
return new Promise(function(resolve, reject) {
90+
obj.once(name, resolve);
91+
obj.once('error', reject);
92+
});
93+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"node": ">= 0.8.0"
3232
},
3333
"dependencies": {
34+
"bluebird": "^1.2.4",
3435
"loopback-sdk-angular": "^1.1.1",
3536
"optimist": "^0.6.1",
3637
"semver": "^2.2.1",

test/fixtures/app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
var loopback = require('loopback');
77
var app = loopback();
88

9+
// model creation is added so output has enough content to reproduce
10+
// issue where node v6 to chunk output of child_process and
11+
// nextTick exit before finish writing (see PR #45)
12+
app.dataSource('db', { connector: 'memory' });
13+
var User = loopback.createModel('User');
14+
app.model(User, { dataSource: 'db' });
15+
916
app.set('restApiRoot', '/rest-api-root');
1017

1118
module.exports = app;

test/lb-ng.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ describe('lb-ng', function() {
7373
});
7474
});
7575

76+
it('does not truncate the output', function() {
77+
return runLbNg(sampleAppJs)
78+
.spread(function(script, stderr) {
79+
expect(script).to.match(/\}\)\(window, window.angular\);/);
80+
});
81+
});
82+
7683
//-- Helpers --
7784

7885
function runLbNg() {

0 commit comments

Comments
 (0)