cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [2/2] git commit: CB-5181 Use spawn helper in hooker.js. Catch error event in spawn helper.
Date Mon, 17 Feb 2014 04:05:34 GMT
CB-5181 Use spawn helper in hooker.js. Catch error event in spawn helper.


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

Branch: refs/heads/master
Commit: 5aab49fad39a6f0df8140ebc1e227cce62438f27
Parents: 9e720db
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Sun Feb 16 23:04:03 2014 -0500
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Sun Feb 16 23:04:03 2014 -0500

----------------------------------------------------------------------
 src/hooker.js     | 33 +++++++++++++++------------------
 src/superspawn.js | 22 +++++++++++++++++-----
 2 files changed, 32 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/5aab49fa/src/hooker.js
----------------------------------------------------------------------
diff --git a/src/hooker.js b/src/hooker.js
index 41cf7a3..fceda84 100644
--- a/src/hooker.js
+++ b/src/hooker.js
@@ -20,7 +20,8 @@ var util  = require('./util'),
     fs    = require('fs'),
     os    = require('os'),
     events= require('./events'),
-    child_process = require('child_process'),
+    superspawn = require('./superspawn'),
+    CordovaError = require('./CordovaError'),
     Q     = require('q'),
     path  = require('path'),
     _ = require('underscore');
@@ -110,41 +111,37 @@ function execute_scripts_serially(scripts, root, dir, opts) {
             events.emit('verbose', 'skipped directory "' + fullpath + '" within hook directory');
             return execute_scripts_serially(scripts, root, dir, opts); // skip directories
if they're in there.
         } else {
-            var command = '"' + fullpath + '" "' + root + '"';
+            var command = fullpath;
+            var args = [root];
             if (os.platform().slice(0, 3) == 'win') {
                 // TODO: Make shebang sniffing a setting (not everyone will want this).
                 var interpreter = extractSheBangInterpreter(fullpath);
                 // we have shebang, so try to run this script using correct interpreter
                 if (interpreter) {
-                    command = '"' + interpreter + '" ' + command;
+                    args.unshift(command);
+                    command = interpreter;
                 }
             }
 
-            var execOpts = {cwd: root};
-            execOpts.env = _.extend({}, process.env);
+            var execOpts = {cwd: root, printCommand: true, stdio: 'inherit'};
+            execOpts.env = {};
             execOpts.env.CORDOVA_VERSION = require('../package').version;
             execOpts.env.CORDOVA_PLATFORMS = opts.platforms ? opts.platforms.join() : '';
             execOpts.env.CORDOVA_PLUGINS = opts.plugins?opts.plugins.join():'';
             execOpts.env.CORDOVA_HOOK = fullpath;
             execOpts.env.CORDOVA_CMDLINE = process.argv.join(' ');
 
-            events.emit('verbose', 'Executing hook "' + command + '"');
-            var d = Q.defer();
-            child_process.exec(command, execOpts, function(err, stdout, stderr) {
+            return superspawn.spawn(command, args, execOpts)
+            .catch(function(err) {
                 // Don't treat non-executable files as errors. They could be READMEs, or
Windows-only scripts.
-                if (!isWindows && err && err.code === 126) {
-                    events.emit('verbose', 'skipped non-executable file: "' + fullpath);
-                    d.resolve(execute_scripts_serially(scripts, root, dir, opts));
+                if (!isWindows && err.code == 'EACCES') {
+                    events.emit('verbose', 'skipped non-executable file: ' + fullpath);
                 } else {
-                    events.emit('verbose', stdout, stderr);
-                    if (err) {
-                        d.reject(new Error('Script "' + fullpath + '" exited with status
code ' + err.code + '. Aborting. Output: \n' + stdout + '\n' + stderr));
-                    } else {
-                        d.resolve(execute_scripts_serially(scripts, root, dir, opts));
-                    }
+                    throw new CordovaError('Hook failed with error code ' + err.code + ':
' + command);
                 }
+            }).then(function() {
+                return execute_scripts_serially(scripts, root, dir, opts);
             });
-            return d.promise;
         }
     } else {
         return Q(); // Nothing to do.

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/5aab49fa/src/superspawn.js
----------------------------------------------------------------------
diff --git a/src/superspawn.js b/src/superspawn.js
index c827ec3..15801ea 100644
--- a/src/superspawn.js
+++ b/src/superspawn.js
@@ -62,8 +62,6 @@ exports.spawn = function(cmd, args, opts) {
         }
     }
 
-    events.emit(opts.printCommand ? 'log' : 'verbose', 'Running command: ' + cmd + ' Args:
' + args);
-
     var spawnOpts = {};
     if (opts.stdio == 'ignore') {
         spawnOpts.stdio = 'ignore';
@@ -77,6 +75,8 @@ exports.spawn = function(cmd, args, opts) {
         spawnOpts.env = _.extend(_.extend({}, process.env), opts.env);
     }
 
+    events.emit(opts.printCommand ? 'log' : 'verbose', 'Running command: ' + cmd + ' args='
+ JSON.stringify(args));
+
     var child = child_process.spawn(cmd, args, spawnOpts);
     var capturedOut = '';
     var capturedErr = '';
@@ -93,14 +93,26 @@ exports.spawn = function(cmd, args, opts) {
         });
     }
 
-    child.on('close', function(code) {
+    child.on('close', whenDone);
+    child.on('error', whenDone);
+    function whenDone(arg) {
+        child.removeListener('close', whenDone);
+        child.removeListener('error', whenDone);
+        var code = typeof arg == 'number' ? arg : arg && arg.code;
+
         events.emit('verbose', 'Command finished with error code ' + code + ': ' + cmd +
' ' + args);
         if (code === 0) {
             d.resolve(capturedOut.trim());
         } else {
-            d.reject(new Error(capturedErr.trim()));
+            var errMsg = cmd + ': Command failed with exit code ' + code;
+            if (capturedErr) {
+                errMsg += ' Error output:\n' + capturedErr.trim();
+            }
+            var err = new Error(errMsg);
+            err.code = code;
+            d.reject(err);
         }
-    });
+    }
 
     return d.promise;
 };


Mime
View raw message