From 6a8c379bfc562e8715d3a63288cec29c74116940 Mon Sep 17 00:00:00 2001 From: David Cheung Date: Fri, 15 Jul 2016 14:29:50 -0400 Subject: [PATCH] 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 --- bin/lb-ng.js | 16 ++++++++++++++-- package.json | 1 + test/fixtures/app.js | 7 +++++++ test/lb-ng.test.js | 7 +++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/bin/lb-ng.js b/bin/lb-ng.js index aa2ad4d..88e0de2 100755 --- a/bin/lb-ng.js +++ b/bin/lb-ng.js @@ -5,6 +5,7 @@ var SG = require('strong-globalize'); SG.SetRootDir(path.resolve(__dirname, '..')); var g = SG(); var fs = require('fs'); +var Promise = require('bluebird'); var semver = require('semver'); var optimist = require('optimist'); var generator = require('loopback-sdk-angular'); @@ -55,9 +56,13 @@ function runGenerator() { // used to have a bug where it prevented the application from exiting. // To work around that issue, we are explicitly exiting here. // - // The exit is deferred to the next tick in order to prevent the Node bug: + // The exit is deferred to both stdout and err is drained + // in order to prevent the Node bug: // https://github.com/joyent/node/issues/3584 - process.nextTick(function() { + Promise.all([ + waitForEvent(process.stdout, 'drain'), + waitForEvent(process.stderr, 'drain') + ]).then(function() { process.exit(); }); } @@ -79,3 +84,10 @@ function assertLoopBackVersion() { process.exit(1); } } + +function waitForEvent(obj, name) { + return new Promise(function(resolve, reject) { + obj.once(name, resolve); + obj.once('error', reject); + }); +} diff --git a/package.json b/package.json index 1610bf6..a7f04f7 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "node": ">= 0.8.0" }, "dependencies": { + "bluebird": "^1.2.4", "loopback-sdk-angular": "^1.1.1", "optimist": "^0.6.1", "semver": "^2.2.1", diff --git a/test/fixtures/app.js b/test/fixtures/app.js index f48302e..80187f6 100644 --- a/test/fixtures/app.js +++ b/test/fixtures/app.js @@ -6,6 +6,13 @@ var loopback = require('loopback'); var app = loopback(); +// model creation is added so output has enough content to reproduce +// issue where node v6 to chunk output of child_process and +// nextTick exit before finish writing (see PR #45) +app.dataSource('db', { connector: 'memory' }); +var User = loopback.createModel('User'); +app.model(User, { dataSource: 'db' }); + app.set('restApiRoot', '/rest-api-root'); module.exports = app; diff --git a/test/lb-ng.test.js b/test/lb-ng.test.js index 4d8cefe..5f32cdf 100644 --- a/test/lb-ng.test.js +++ b/test/lb-ng.test.js @@ -73,6 +73,13 @@ describe('lb-ng', function() { }); }); + it('does not truncate the output', function() { + return runLbNg(sampleAppJs) + .spread(function(script, stderr) { + expect(script).to.match(/\}\)\(window, window.angular\);/); + }); + }); + //-- Helpers -- function runLbNg() {