cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [1/2] git commit: CB-5181 Use spawn helper in emulate.js and run.js
Date Mon, 17 Feb 2014 04:05:33 GMT
Repository: cordova-cli
Updated Branches:
  refs/heads/master a3c8badd8 -> 5aab49fad


CB-5181 Use spawn helper in emulate.js and run.js


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

Branch: refs/heads/master
Commit: 9e720db70c752aa8bb13e4706f4f82a19389a712
Parents: a3c8bad
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Sun Feb 16 21:56:55 2014 -0500
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Sun Feb 16 23:03:49 2014 -0500

----------------------------------------------------------------------
 spec/build.spec.js   |  5 ++--
 spec/emulate.spec.js | 49 ++++---------------------------
 spec/run.spec.js     | 52 +++++---------------------------
 src/compile.js       | 17 ++---------
 src/emulate.js       | 69 ++++++-------------------------------------
 src/run.js           | 75 +++++++----------------------------------------
 src/util.js          | 44 ++++++++++++---------------
 7 files changed, 56 insertions(+), 255 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9e720db7/spec/build.spec.js
----------------------------------------------------------------------
diff --git a/spec/build.spec.js b/spec/build.spec.js
index a563cb8..259c3d4 100644
--- a/spec/build.spec.js
+++ b/spec/build.spec.js
@@ -89,8 +89,9 @@ describe('build command', function() {
         });
         it('should pass down options', function(done) {
             cordova.raw.build({platforms: ['android'], options: ['--release']}).then(function()
{
-                expect(prepare_spy).toHaveBeenCalledWith({platforms: ['android'], options:
["--release"]});
-                expect(compile_spy).toHaveBeenCalledWith({platforms: ['android'], options:
["--release"]});
+                var opts = {platforms: ['android'], options: ["--release"], verbose: false};
+                expect(prepare_spy).toHaveBeenCalledWith(opts);
+                expect(compile_spy).toHaveBeenCalledWith(opts);
                 done();
             });
         });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9e720db7/spec/emulate.spec.js
----------------------------------------------------------------------
diff --git a/spec/emulate.spec.js b/spec/emulate.spec.js
index 7256936..853e5a3 100644
--- a/spec/emulate.spec.js
+++ b/spec/emulate.spec.js
@@ -18,7 +18,7 @@
 */
 var cordova = require('../cordova'),
     platforms = require('../platforms'),
-    child_process = require('child_process'),
+    superspawn = require('../src/superspawn'),
     path = require('path'),
     fs = require('fs'),
     hooker = require('../src/hooker'),
@@ -29,39 +29,9 @@ var cordova = require('../cordova'),
 var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www';
});
 
 describe('emulate command', function() {
-    var is_cordova, cd_project_root, list_platforms, fire, result, child, spawn_wrap;
+    var is_cordova, cd_project_root, list_platforms, fire, result;
     var project_dir = '/some/path';
     var prepare_spy;
-    child = {
-        on: function(child_event,cb){
-            if(child_event === 'close'){
-                cb(0);
-            }
-        },
-        stdout: {
-            setEncoding: function(){},
-            on: function(){}
-        },
-        stderr: {
-            setEncoding: function(){},
-            on: function(){}
-        }
-    };
-    spawn_wrap = function(cmd,args,options){
-        var _cmd = cmd,
-            _args = args;
-
-        if (os.platform() === 'win32') {
-            _args = ['/c',_cmd].concat(_args);
-            _cmd = 'cmd';
-        }
-
-        return {
-                    "cmd": _cmd,
-                    "args": _args,
-                    "options": options
-                };
-    };
 
     function wrapper(f, post) {
         runs(function() {
@@ -77,7 +47,7 @@ describe('emulate command', function() {
         list_platforms = spyOn(util, 'listPlatforms').andReturn(supported_platforms);
         fire = spyOn(hooker.prototype, 'fire').andReturn(Q());
         prepare_spy = spyOn(cordova.raw, 'prepare').andReturn(Q());
-        spyOn(child_process, 'spawn').andReturn(child);
+        spyOn(superspawn, 'spawn').andCallFake(Q);
     });
     describe('failure', function() {
         it('should not run inside a Cordova-based project with no added platforms by calling
util.listPlatforms', function() {
@@ -95,16 +65,11 @@ describe('emulate command', function() {
     });
 
     describe('success', function() {
-        var spawn_call;
         it('should run inside a Cordova-based project with at least one added platform and
call prepare and shell out to the emulate script', function(done) {
             cordova.raw.emulate(['android','ios']).then(function(err) {
                 expect(prepare_spy).toHaveBeenCalledWith(['android', 'ios']);
-
-                spawn_call = spawn_wrap(path.join(project_dir, 'platforms', 'android', 'cordova',
'run'), ['--emulator']);
-                expect(child_process.spawn).toHaveBeenCalledWith(spawn_call.cmd, spawn_call.args);
-
-                spawn_call = spawn_wrap(path.join(project_dir, 'platforms', 'ios', 'cordova',
'run'), ['--emulator']);
-                expect(child_process.spawn).toHaveBeenCalledWith(spawn_call.cmd, spawn_call.args);
+                expect(superspawn.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms',
'android', 'cordova', 'run'), ['--emulator'], jasmine.any(Object));
+                expect(superspawn.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms',
'ios', 'cordova', 'run'), ['--emulator'], jasmine.any(Object));
 
                 done();
             });
@@ -112,9 +77,7 @@ describe('emulate command', function() {
         it('should pass down options', function(done) {
             cordova.raw.emulate({platforms: ['ios'], options:["--optionTastic"]}).then(function(err)
{
                 expect(prepare_spy).toHaveBeenCalledWith(['ios']);
-
-                spawn_call = spawn_wrap(path.join(project_dir, 'platforms', 'ios', 'cordova',
'run'), ['--emulator', '--optionTastic']);
-                expect(child_process.spawn).toHaveBeenCalledWith(spawn_call.cmd, spawn_call.args);
+                expect(superspawn.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms',
'ios', 'cordova', 'run'), ['--emulator', '--optionTastic'], jasmine.any(Object));
 
                 done();
             });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9e720db7/spec/run.spec.js
----------------------------------------------------------------------
diff --git a/spec/run.spec.js b/spec/run.spec.js
index 26b3e83..bfaf3f8 100644
--- a/spec/run.spec.js
+++ b/spec/run.spec.js
@@ -18,50 +18,19 @@
 */
 var cordova = require('../cordova'),
     platforms = require('../platforms'),
-    child_process = require('child_process'),
+    superspawn = require('../src/superspawn'),
     path = require('path'),
     fs = require('fs'),
     hooker = require('../src/hooker'),
     Q = require('q'),
-    util = require('../src/util'),
-    os = require('os');
+    util = require('../src/util');
 
 var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www';
});
 
 describe('run command', function() {
-    var is_cordova, cd_project_root, list_platforms, fire, child, spawn_wrap;
+    var is_cordova, cd_project_root, list_platforms, fire;
     var project_dir = '/some/path';
     var prepare_spy;
-    child = {
-        on: function(child_event,cb){
-            if(child_event === 'close'){
-                cb(0);
-            }
-        },
-        stdout: {
-            setEncoding: function(){},
-            on: function(){}
-        },
-        stderr: {
-            setEncoding: function(){},
-            on: function(){}
-        }
-    };
-    spawn_wrap = function(cmd,args,options){
-        var _cmd = cmd,
-            _args = args;
-
-        if (os.platform() === 'win32') {
-            _args = ['/c',_cmd].concat(_args);
-            _cmd = 'cmd';
-        }
-
-        return {
-                    "cmd": _cmd,
-                    "args": _args,
-                    "options": options
-                };
-    };
 
     beforeEach(function() {
         is_cordova = spyOn(util, 'isCordova').andReturn(project_dir);
@@ -69,7 +38,7 @@ describe('run command', function() {
         list_platforms = spyOn(util, 'listPlatforms').andReturn(supported_platforms);
         fire = spyOn(hooker.prototype, 'fire').andReturn(Q());
         prepare_spy = spyOn(cordova.raw, 'prepare').andReturn(Q());
-        spyOn(child_process, 'spawn').andReturn(child);
+        spyOn(superspawn, 'spawn').andReturn(Q);
     });
     describe('failure', function() {
         it('should not run inside a Cordova-based project with no added platforms by calling
util.listPlatforms', function(done) {
@@ -96,13 +65,8 @@ describe('run command', function() {
         it('should run inside a Cordova-based project with at least one added platform and
call prepare and shell out to the run script', function(done) {
             cordova.raw.run(['android','ios']).then(function() {
                 expect(prepare_spy).toHaveBeenCalledWith(['android', 'ios']);
-
-                spawn_call = spawn_wrap(path.join(project_dir, 'platforms', 'android', 'cordova',
'run'), []);
-                expect(child_process.spawn).toHaveBeenCalledWith(spawn_call.cmd, spawn_call.args);
-
-                spawn_call = spawn_wrap(path.join(project_dir, 'platforms', 'ios', 'cordova',
'run'), []);
-                expect(child_process.spawn).toHaveBeenCalledWith(spawn_call.cmd, spawn_call.args);
-
+                expect(superspawn.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms',
'android', 'cordova', 'run'), [], jasmine.any(Object));
+                expect(superspawn.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms',
'ios', 'cordova', 'run'), [], jasmine.any(Object));
             }, function(err) {
                 expect(err).toBeUndefined();
             }).fin(done);
@@ -110,9 +74,7 @@ describe('run command', function() {
         it('should pass down parameters', function(done) {
             cordova.raw.run({platforms: ['blackberry10'], options:['--password', '1q1q']}).then(function()
{
                 expect(prepare_spy).toHaveBeenCalledWith(['blackberry10']);
-
-                spawn_call = spawn_wrap(path.join(project_dir, 'platforms', 'blackberry10',
'cordova', 'run'), ['--password', '1q1q']);
-                expect(child_process.spawn).toHaveBeenCalledWith(spawn_call.cmd, spawn_call.args);
+                expect(superspawn.spawn).toHaveBeenCalledWith(path.join(project_dir, 'platforms',
'blackberry10', 'cordova', 'run'), ['--password', '1q1q'], jasmine.any(Object));
             }, function(err) {
                 expect(err).toBeUndefined();
             }).fin(done);

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9e720db7/src/compile.js
----------------------------------------------------------------------
diff --git a/src/compile.js b/src/compile.js
index 02f81b3..857c3a5 100644
--- a/src/compile.js
+++ b/src/compile.js
@@ -23,19 +23,11 @@
 var path              = require('path'),
     cordova_util      = require('./util'),
     hooker            = require('./hooker'),
-    superspawn        = require('./superspawn'),
-    events            = require('./events');
+    superspawn        = require('./superspawn');
 
 // Returns a promise.
 module.exports = function compile(options) {
     var projectRoot = cordova_util.cdProjectRoot();
-
-    options = options || {
-        verbose: false,
-        platforms: [],
-        options: []
-    };
-
     options = cordova_util.preProcessOptions(options);
 
     var hooks = new hooker(projectRoot);
@@ -43,12 +35,7 @@ module.exports = function compile(options) {
     options.platforms.forEach(function(platform) {
         ret = ret.then(function() {
             var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'build');
-
-            events.emit('log', 'Compiling ' + platform + ' project');
-            return superspawn.spawn(cmd, options.options, { stdio: 'inherit' })
-            .then(function() {
-                events.emit('log', 'Platform "' + platform + '" compiled successfully.');
-            });
+            return superspawn.spawn(cmd, options.options, { stdio: 'inherit', printCommand:
true });
         });
     });
     ret = ret.then(function() {

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9e720db7/src/emulate.js
----------------------------------------------------------------------
diff --git a/src/emulate.js b/src/emulate.js
index fee4c28..c1d3a10 100644
--- a/src/emulate.js
+++ b/src/emulate.js
@@ -22,78 +22,27 @@
 
 var cordova_util      = require('./util'),
     path              = require('path'),
-    child_process     = require('child_process'),
-    events            = require('./events'),
     hooker            = require('./hooker'),
-    Q                 = require('q'),
-    DEFAULT_OPTIONS   = ["--emulator"],
-    os                = require('os');
-
-// Returns a promise.
-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;
-
-    if (os.platform() === 'win32') {
-        args = ['/c',cmd].concat(args);
-        cmd = 'cmd';
-    }
-
-    events.emit('log', 'Running on emulator for platform "' + platform + '" via command "'
+ cmd + '" ' + args.join(" "));
-
-    //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;
-}
+    superspawn        = require('./superspawn'),
+    Q                 = require('q');
 
 // Returns a promise.
 module.exports = function emulate(options) {
-    var projectRoot = cordova_util.cdProjectRoot(),
-        hooks;
-
-    if (!options) {
-        options = {
-            verbose: false,
-            platforms: [],
-            options: []
-        };
-    }
-
+    var projectRoot = cordova_util.cdProjectRoot();
     options = cordova_util.preProcessOptions(options);
 
-    hooks = new hooker(projectRoot);
+    var hooks = new hooker(projectRoot);
     return hooks.fire('before_emulate', options)
     .then(function() {
         // Run a prepare first!
         return require('../cordova').raw.prepare(options.platforms);
     }).then(function() {
+        // Deploy in parallel (output gets intermixed though...)
         return Q.all(options.platforms.map(function(platform) {
-            return shell_out_to_emulate(projectRoot, platform, options.options);
+            var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'run');
+            var args = ['--emulator'].concat(options.options);
+
+            return superspawn.spawn(cmd, args, {stdio: 'inherit', printCommand: true});
         }));
     }).then(function() {
         return hooks.fire('after_emulate', options);

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9e720db7/src/run.js
----------------------------------------------------------------------
diff --git a/src/run.js b/src/run.js
index fa34e36..5de3031 100644
--- a/src/run.js
+++ b/src/run.js
@@ -23,79 +23,26 @@
 var cordova_util      = require('./util'),
     path              = require('path'),
     hooker            = require('./hooker'),
-    events            = require('./events'),
-    Q                 = require('q'),
-    child_process     = require('child_process'),
-    os                = require('os');
-
-// Returns a promise.
-function shell_out_to_run(projectRoot, platform, options) {
-    var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'run'),
-        args = options || [],
-        d = Q.defer(),
-        errors = "",
-        child;
-
-    if (os.platform() === 'win32') {
-        args = ['/c',cmd].concat(args);
-        cmd = 'cmd';
-    }
-
-    events.emit('log', 'Running app on platform "' + platform + '" via command "' + cmd +
'" ' + args.join(" "));
-
-    //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('An error occurred while running the ' + platform + ' project.'
+ errors));
-        }
-    });
-
-    return d.promise;
-}
+    superspawn        = require('./superspawn'),
+    Q                 = require('q');
 
 // Returns a promise.
 module.exports = function run(options) {
     var projectRoot = cordova_util.cdProjectRoot(),
-        hooks;
-
-    if (!options) {
-        options = {
-            verbose: false,
-            platforms: [],
-            options: []
-        };
-    }
-
     options = cordova_util.preProcessOptions(options);
 
-    hooks = new hooker(projectRoot);
+    var hooks = new hooker(projectRoot);
     return hooks.fire('before_run', options)
     .then(function() {
         // Run a prepare first, then shell out to run
         return require('../cordova').raw.prepare(options.platforms)
-        .then(function() {
-            return Q.all(options.platforms.map(function(platform) {
-                return shell_out_to_run(projectRoot, platform, options.options);
-            }));
-        }).then(function() {
-            return hooks.fire('after_run', options);
-        });
+    }).then(function() {
+        // Deploy in parallel (output gets intermixed though...)
+        return Q.all(options.platforms.map(function(platform) {
+            var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'run');
+            return superspawn.spawn(cmd, options.options, { printCommand: true, stdio: 'inherit'
});
+        }));
+    }).then(function() {
+        return hooks.fire('after_run', options);
     });
 };

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/9e720db7/src/util.js
----------------------------------------------------------------------
diff --git a/src/util.js b/src/util.js
index 164c322..ca8a76e 100644
--- a/src/util.js
+++ b/src/util.js
@@ -138,21 +138,6 @@ exports = module.exports = {
         return rootPath;
     },
     preProcessOptions: function (inputOptions) {
-        var DEFAULT_OPTIONS = {
-                verbose: false,
-                platforms: [],
-                options: []
-            },
-            result = inputOptions || DEFAULT_OPTIONS,
-            projectRoot = this.isCordova();
-
-        if (!projectRoot) {
-            throw new CordovaError('Current working directory is not a Cordova-based project.');
-        }
-        var projectPlatforms = this.listPlatforms(projectRoot);
-        if (projectPlatforms.length === 0) {
-            throw new CordovaError('No platforms added to this project. Please use `cordova
platform add <platform>`.');
-        }
         /**
          * Current Desired Arguments
          * options: {verbose: boolean, platforms: [String], options: [String]}
@@ -160,22 +145,29 @@ exports = module.exports = {
          * platformList: [String] -- assume just a list of platforms
          * platform: String -- assume this is a platform
          */
+        var result = inputOptions || {};
         if (Array.isArray(inputOptions)) {
-            result = {
-                verbose: false,
-                platforms: inputOptions,
-                options: []
-            };
+            result = { platforms: inputOptions };
         } else if (typeof inputOptions === 'string') {
-            result = {
-                verbose: false,
-                platforms: [inputOptions],
-                options: []
-            };
+            result = { platforms: [inputOptions] };
         }
-        if (!result.platforms || (result.platforms && result.platforms.length ===
0) ) {
+        result.verbose = result.verbose || false;
+        result.platforms = result.platforms || [];
+        result.options = result.options || [];
+
+        var projectRoot = this.isCordova();
+
+        if (!projectRoot) {
+            throw new CordovaError('Current working directory is not a Cordova-based project.');
+        }
+        var projectPlatforms = this.listPlatforms(projectRoot);
+        if (projectPlatforms.length === 0) {
+            throw new CordovaError('No platforms added to this project. Please use `cordova
platform add <platform>`.');
+        }
+        if (result.platforms.length === 0) {
             result.platforms = projectPlatforms;
         }
+
         return result;
     }
 };


Mime
View raw message