Updated Branches:
  refs/heads/master 51306462e -> dedc29a81

CB-5125: replace child process exec with spawn


Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/01c7ecec
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/01c7ecec
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/01c7ecec

Branch: refs/heads/master
Commit: 01c7ecec7ccf4a3c1423ddf3844e125d24965025
Parents: 5130646
Author: Carlos Santana <[email protected]>
Authored: Mon Oct 21 14:30:27 2013 -0400
Committer: Carlos Santana <[email protected]>
Committed: Mon Oct 21 23:11:22 2013 -0400

----------------------------------------------------------------------
 src/compile.js | 45 ++++++++++++++++++++++++++++++++++-----------
 src/emulate.js | 50 +++++++++++++++++++++++++++++++++++++-------------
 src/run.js     | 50 +++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 108 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/01c7ecec/src/compile.js
----------------------------------------------------------------------
diff --git a/src/compile.js b/src/compile.js
index 34c24fe..97f7f21 100644
--- a/src/compile.js
+++ b/src/compile.js
@@ -16,6 +16,10 @@
     specific language governing permissions and limitations
     under the License.
 */
+
+/*global require: true, module: true, process: true*/
+/*jslint sloppy: true, white: true, newcap: true */
+
 var cordova_util      = require('./util'),
     path              = require('path'),
     child_process     = require('child_process'),
@@ -25,25 +29,44 @@ var cordova_util      = require('./util'),
 
 // Returns a promise.
 function shell_out_to_build(projectRoot, platform, options) {
-    var cmd = '"' + path.join(projectRoot, 'platforms', platform, 'cordova', 
'build') + (options.length ? '" ' + options.join(" ") : '"');
-    events.emit('log', 'Compiling ' + platform + ' project.');
-    var d = Q.defer();
-    child_process.exec(cmd, function(err, stdout, stderr) {
-        events.emit('verbose', stdout);
-        events.emit('verbose', stderr);
-        if (err) {
-            d.reject(new Error('An error occurred while building the ' + 
platform + ' project. ' + stderr));
-        } else {
+    var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 
'build'),
+        args = options.length ? options : [],
+        d = Q.defer(),
+        errors = "",
+        child;
+
+    events.emit('log', 'Compiling app on platform "' + platform + '" via 
command "' + cmd + '" ' + args.join(" "));
+
+    //CB-5125 using spawn instead of exec to avoid errors with stdout on 
maxBuffer
+    child = child_process.spawn(cmd, args);
+    child.stdout.setEncoding('utf8');
+    child.stdout.on('data', function (data) {
+        events.emit('verbose', data);
+    });
+
+    child.stderr.setEncoding('utf8');
+    child.stderr.on('data', function (data) {
+        events.emit('verbose', data);
+        errors = errors + data;
+    });
+
+    child.on('close', function (code) {
+        events.emit('verbose', "child_process.spawn(" + cmd + "," + 
args.join(" ") + ") = " + code);
+        if (code === 0) {
             events.emit('log', 'Platform "' + platform + '" compiled 
successfully.');
             d.resolve();
+        } else {
+            d.reject(new Error('An error occurred while building the ' + 
platform + ' project.' + errors));
         }
     });
+
     return d.promise;
 }
 
 // Returns a promise.
 module.exports = function compile(options) {
-    var projectRoot = cordova_util.isCordova(process.cwd());
+    var projectRoot = cordova_util.isCordova(process.cwd()),
+        hooks;
 
     if (!options) {
         options = {
@@ -58,7 +81,7 @@ module.exports = function compile(options) {
         return Q.reject(options);
     }
 
-    var hooks = new hooker(projectRoot);
+    hooks = new hooker(projectRoot);
     return hooks.fire('before_compile', options)
     .then(function() {
         // Iterate over each added platform

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/01c7ecec/src/emulate.js
----------------------------------------------------------------------
diff --git a/src/emulate.js b/src/emulate.js
index cd4c038..808812f 100644
--- a/src/emulate.js
+++ b/src/emulate.js
@@ -16,6 +16,10 @@
     specific language governing permissions and limitations
     under the License.
 */
+
+/*global require: true, module: true, process: true*/
+/*jslint sloppy: true, white: true, newcap: true */
+
 var cordova_util      = require('./util'),
     path              = require('path'),
     child_process     = require('child_process'),
@@ -25,26 +29,46 @@ var cordova_util      = require('./util'),
     DEFAULT_OPTIONS   = ["--emulator"];
 
 // Returns a promise.
-function shell_out_to_emulate(root, platform, options) {
-    options = options.length ? DEFAULT_OPTIONS.concat(options) : 
DEFAULT_OPTIONS;
-    var cmd = '"' + path.join(root, 'platforms', platform, 'cordova', 'run') + 
'" ' + options.join(" ");
-    events.emit('log', 'Running on emulator for platform "' + platform + '" 
via command "' + cmd + '"');
-    var d = Q.defer();
-    child_process.exec(cmd, function(err, stdout, stderr) {
-        events.emit('verbose', stdout + stderr);
-        if (err) {
-            d.reject(new Error('An error occurred while emulating/deploying 
the ' + platform + ' project.' + stdout + stderr));
-        } else {
+function shell_out_to_emulate(projectRoot, platform, options) {
+    var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'run'),
+        args = options.length ? DEFAULT_OPTIONS.concat(options) : 
DEFAULT_OPTIONS,
+        d = Q.defer(),
+        errors = "",
+        child;
+
+    events.emit('log', 'Running on emulator for platform "' + platform + '" 
via command "' + cmd + '" ' + args.join(" "));
+
+    //CB-5125 using spawn instead of exec to avoid errors with stdout on 
maxBuffer
+    child = child_process.spawn(cmd, args);
+
+    child.stdout.setEncoding('utf8');
+    child.stdout.on('data', function (data) {
+        events.emit('verbose', data);
+    });
+
+    child.stderr.setEncoding('utf8');
+    child.stderr.on('data', function (data) {
+        events.emit('verbose', data);
+        errors = errors + data;
+    });
+
+    child.on('close', function (code) {
+        events.emit('verbose', "child_process.spawn(" + cmd + "," + 
args.join(" ") + ") = " + code);
+        if (code === 0) {
             events.emit('log', 'Platform "' + platform + '" deployed to 
emulator.');
             d.resolve();
+        } else {
+            d.reject(new Error('An error occurred while emulating/deploying 
the ' + platform + ' project.' + errors));
         }
     });
+
     return d.promise;
 }
 
 // Returns a promise.
-module.exports = function emulate (options) {
-    var projectRoot = cordova_util.isCordova(process.cwd());
+module.exports = function emulate(options) {
+    var projectRoot = cordova_util.isCordova(process.cwd()),
+        hooks;
 
     if (!options) {
         options = {
@@ -59,7 +83,7 @@ module.exports = function emulate (options) {
         return Q.reject(options);
     }
 
-    var hooks = new hooker(projectRoot);
+    hooks = new hooker(projectRoot);
     return hooks.fire('before_emulate', options)
     .then(function() {
         // Run a prepare first!

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/01c7ecec/src/run.js
----------------------------------------------------------------------
diff --git a/src/run.js b/src/run.js
index dddc1fc..d5487b3 100644
--- a/src/run.js
+++ b/src/run.js
@@ -16,6 +16,10 @@
     specific language governing permissions and limitations
     under the License.
 */
+
+/*global require: true, module: true, process: true*/
+/*jslint sloppy: true, white: true, newcap: true */
+
 var cordova_util      = require('./util'),
     path              = require('path'),
     hooker            = require('./hooker'),
@@ -26,10 +30,16 @@ var cordova_util      = require('./util'),
 
 // Returns a promise.
 function shell_out_to_run(projectRoot, platform, options) {
-    options = options.length ? DEFAULT_OPTIONS.concat(options) : 
DEFAULT_OPTIONS;
-    var cmd = '"' + path.join(projectRoot, 'platforms', platform, 'cordova', 
'run') + '" ' + options.join(" ");
+    var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'run'),
+        args = options.length ? DEFAULT_OPTIONS.concat(options) : 
DEFAULT_OPTIONS,
+        d = Q.defer(),
+        errors = "",
+        child;
+
+    events.emit('log', 'Running app on platform "' + platform + '" via command 
"' + cmd + '" ' + args.join(" "));
+
     // TODO: inconsistent API for BB10 run command
-/*    if (platform == 'blackberry') {
+/*  if (platform == 'blackberry') {
         var bb_project = path.join(projectRoot, 'platforms', 'blackberry')
         var project = new platforms.blackberry.parser(bb_project);
         if (project.has_device_target()) {
@@ -43,24 +53,38 @@ function shell_out_to_run(projectRoot, platform, options) {
         }
     }
 */
-    events.emit('log', 'Running app on ' + platform);
-    events.emit('verbose', 'Run command: "' + cmd + '" (output to follow)');
-    var d = Q.defer();
-    child_process.exec(cmd, function(err, output, stderr) {
-        events.emit('verbose', output);
-        if (err) {
-            d.reject(new Error('An error occurred while running the ' + 
platform + ' project. ' + output + stderr));
-        } else {
+
+    //CB-5125 using spawn instead of exec to avoid errors with stdout on 
maxBuffer
+    child = child_process.spawn(cmd, args);
+
+    child.stdout.setEncoding('utf8');
+    child.stdout.on('data', function (data) {
+        events.emit('verbose', data);
+    });
+
+    child.stderr.setEncoding('utf8');
+    child.stderr.on('data', function (data) {
+        events.emit('verbose', data);
+        errors = errors + data;
+    });
+
+    child.on('close', function (code) {
+        events.emit('verbose', "child_process.spawn(" + cmd + "," + 
args.join(" ") + ") = " + code);
+        if (code === 0) {
             events.emit('log', 'Platform "' + platform + '" ran 
successfully.');
             d.resolve();
+        } else {
+            d.reject(new Error('n error occurred while running the ' + 
platform + ' project.' + errors));
         }
     });
+
     return d.promise;
 }
 
 // Returns a promise.
 module.exports = function run(options) {
-    var projectRoot = cordova_util.isCordova(process.cwd());
+    var projectRoot = cordova_util.isCordova(process.cwd()),
+        hooks;
 
     if (!options) {
         options = {
@@ -75,7 +99,7 @@ module.exports = function run(options) {
         return Q.reject(options);
     }
 
-    var hooks = new hooker(projectRoot);
+    hooks = new hooker(projectRoot);
     return hooks.fire('before_run', options)
     .then(function() {
         // Run a prepare first, then shell out to run

Reply via email to