incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [18/78] [abbrv] git commit: changed command modules to throw, and cli script to handle passing over to console. fixed tests.
Date Fri, 27 Jul 2012 00:29:15 GMT
changed command modules to throw, and cli script to handle passing over to console. fixed tests.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/commit/30ed15c5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/tree/30ed15c5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/diff/30ed15c5

Branch: refs/heads/cordova-client
Commit: 30ed15c5c63cdcc1e60d209e02b41cf38115a80f
Parents: e878b35
Author: Fil Maj <maj.fil@gmail.com>
Authored: Tue Jul 17 18:11:15 2012 -0700
Committer: Fil Maj <maj.fil@gmail.com>
Committed: Tue Jul 17 18:11:15 2012 -0700

----------------------------------------------------------------------
 bin/cordova           |    9 ++++--
 cordova.js            |    4 +-
 spec/cli.spec.js      |    5 ---
 spec/platform.spec.js |   72 +++++++++++++++++--------------------------
 src/build.js          |    6 +--
 src/emulate.js        |    7 +---
 src/platform.js       |   27 +++++++----------
 src/plugin.js         |   25 +++++----------
 8 files changed, 60 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/30ed15c5/bin/cordova
----------------------------------------------------------------------
diff --git a/bin/cordova b/bin/cordova
index b9816a8..19624bd 100755
--- a/bin/cordova
+++ b/bin/cordova
@@ -4,12 +4,15 @@ var cordova = require('./../cordova')
 ,   opts    = process.argv.slice(3, process.argv.length)
 
 if (cordova.hasOwnProperty(cmd)) {
-    cordova[cmd].apply(this, opts)
+    try {
+        console.log(cordova[cmd].apply(this, opts));
+    } catch(e) {
+        console.error(e);
+    }
 }
 else if (cmd === undefined) {
-    cordova.help()
+    console.log(cordova.help());
 }
 else {
     console.error('Cordova does not know ' + cmd + '; try help for a list of all the available
commands.')
 }
-

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/30ed15c5/cordova.js
----------------------------------------------------------------------
diff --git a/cordova.js b/cordova.js
index 2efe23a..de3a499 100755
--- a/cordova.js
+++ b/cordova.js
@@ -11,7 +11,7 @@ var fs            = require('fs')
 module.exports = {
     help: function help () {
         var raw = fs.readFileSync(path.join(__dirname, 'doc', 'help.txt')).toString('utf8').split("\n");
-        console.log(raw.map(function(line) {
+        raw.map(function(line) {
             if (line.match('    ')) {
                 var prompt = '    $ '
                 ,   isPromptLine = !!(line.indexOf(prompt) != -1);
@@ -32,7 +32,7 @@ module.exports = {
             else {
                 return line.magenta;
             }
-        }).join("\n"));
+        }).join("\n");
     },
     docs: function docs () {
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/30ed15c5/spec/cli.spec.js
----------------------------------------------------------------------
diff --git a/spec/cli.spec.js b/spec/cli.spec.js
index 2f1e46c..0a5e5e9 100644
--- a/spec/cli.spec.js
+++ b/spec/cli.spec.js
@@ -2,10 +2,5 @@ var cordova = require('../cordova');
 
 describe("cordova", function() {
     describe("'help' command", function() {
-        it("should console.log some stuff", function() {
-            spyOn(console, "log");
-            cordova.help();
-            expect(console.log).toHaveBeenCalled();
-        });
     });
 });

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/30ed15c5/spec/platform.spec.js
----------------------------------------------------------------------
diff --git a/spec/platform.spec.js b/spec/platform.spec.js
index a3ab65a..b147e03 100644
--- a/spec/platform.spec.js
+++ b/spec/platform.spec.js
@@ -14,9 +14,6 @@ describe('platform command', function() {
     });
 
     it('should run inside a Cordova-based project', function() {
-        spyOn(console, 'error');
-        spyOn(console, 'log');
-
         var cwd = process.cwd();
         this.after(function() {
             process.chdir(cwd);
@@ -26,15 +23,11 @@ describe('platform command', function() {
 
         process.chdir(tempDir);
 
-        cordova.platform();
-
-        expect(console.log).toHaveBeenCalled();
-        expect(console.error).not.toHaveBeenCalled();
+        expect(function() {
+            cordova.platform();
+        }).not.toThrow();
     });
     it('should not run outside of a Cordova-based project', function() {
-        spyOn(console, 'error');
-        spyOn(console, 'log');
-
         var cwd = process.cwd();
         this.after(function() {
             process.chdir(cwd);
@@ -42,10 +35,9 @@ describe('platform command', function() {
 
         process.chdir(tempDir);
 
-        cordova.platform();
-
-        expect(console.log).not.toHaveBeenCalled();
-        expect(console.error).toHaveBeenCalled();
+        expect(function() {
+            cordova.platform();
+        }).toThrow();
     });
 
     describe('ls', function() {
@@ -60,26 +52,21 @@ describe('platform command', function() {
         });
 
         it('should list out no platforms for a fresh project', function() {
-            spyOn(console, 'error');
-            spyOn(console, 'log');
-
             process.chdir(tempDir);
-            cordova.platform('ls');
 
-            expect(console.error).not.toHaveBeenCalled();
-            expect(console.log).toHaveBeenCalledWith('No platforms added. Use `cordova platforms
add <platform>`.');
+            expect(cordova.platform('ls')).toEqual('No platforms added. Use `cordova platform
add <platform>`.');
         });
 
         it('should list out added platforms in a project', function() {
-            spyOn(console, 'error');
-            spyOn(console, 'log');
+            var cb = jasmine.createSpy().andCallFake(function() {
+                expect(cordova.platform('ls')).toEqual('android');
+            });
 
             process.chdir(tempDir);
-            cordova.platform('add', 'android');
-            cordova.platform('ls');
-
-            expect(console.error).not.toHaveBeenCalled();
-            expect(console.log).toHaveBeenCalledWith('android');
+            runs(function() {
+                cordova.platform('add', 'android', cb);
+            });
+            waitsFor(function() { return cb.wasCalled; }, "create callback", 17500);
         });
     });
 
@@ -95,15 +82,15 @@ describe('platform command', function() {
         });
 
         it('should add a supported platform', function() {
-            spyOn(console, 'error');
-            spyOn(console, 'log');
+            var cb = jasmine.createSpy().andCallFake(function() {
+                expect(cordova.platform('ls')).toEqual('android');
+            });
 
             process.chdir(tempDir);
-            cordova.platform('add', 'android');
-            cordova.platform('ls');
-
-            expect(console.error).not.toHaveBeenCalled();
-            expect(console.log).toHaveBeenCalledWith('android');
+            runs(function() {
+                cordova.platform('add', 'android', cb);
+            });
+            waitsFor(function() { return cb.wasCalled; }, "create callback", 17500);
         });
     });
 
@@ -119,17 +106,16 @@ describe('platform command', function() {
         });
 
         it('should remove a supported and added platform', function() {
-            spyOn(console, 'error');
-            spyOn(console, 'log');
+            var cb = jasmine.createSpy().andCallFake(function() {
+                cordova.platform('remove', 'android');
+                expect(cordova.platform('ls')).toEqual('No platforms added. Use `cordova
platform add <platform>`.');
+            });
 
             process.chdir(tempDir);
-            cordova.platform('add', 'android');
-            cordova.platform('remove', 'android');
-
-            cordova.platform('ls');
-
-            expect(console.error).not.toHaveBeenCalled();
-            expect(console.log).toHaveBeenCalledWith('No platforms added. Use `cordova platforms
add <platform>`.');
+            runs(function() {
+                cordova.platform('add', 'android', cb);
+            });
+            waitsFor(function() { return cb.wasCalled; }, "create callback", 17500);
         });
     });
 });

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/30ed15c5/src/build.js
----------------------------------------------------------------------
diff --git a/src/build.js b/src/build.js
index 5554574..1efa1ff 100644
--- a/src/build.js
+++ b/src/build.js
@@ -12,8 +12,7 @@ module.exports = function build () {
     var projectRoot = cordova_util.isCordova(process.cwd());
 
     if (!projectRoot) {
-        console.error('Current working directory is not a Cordova-based project.');
-        return;
+        throw 'Current working directory is not a Cordova-based project.';
     }
 
     var xml = path.join(projectRoot, 'www', 'config.xml');
@@ -52,8 +51,7 @@ module.exports = function build () {
         // shell out to debug command
         var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'debug');
         exec(cmd, function(err, stderr, stdout) {
-            if (err) 
-                console.error('An error occurred while building the ' + platform + ' project.',
err);
+            if (err) throw 'An error occurred while building the ' + platform + ' project.
' + err;
         });
     });
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/30ed15c5/src/emulate.js
----------------------------------------------------------------------
diff --git a/src/emulate.js b/src/emulate.js
index 02dfe49..a7825c1 100644
--- a/src/emulate.js
+++ b/src/emulate.js
@@ -9,8 +9,7 @@ module.exports = function emulate () {
     var projectRoot = cordova_util.isCordova(process.cwd());
 
     if (!projectRoot) {
-        console.error('Current working directory is not a Cordova-based project.');
-        return;
+        throw 'Current working directory is not a Cordova-based project.';
     }
 
     var xml = path.join(projectRoot, 'www', 'config.xml');
@@ -21,9 +20,7 @@ module.exports = function emulate () {
     platforms.map(function(platform) {
         var cmd = path.join(projectRoot, 'platforms', platform, 'cordova', 'emulate');
         exec(cmd, function(err, stderr, stdout) {
-            if (err) 
-                console.error('An error occurred while emulating/deploying the ' + platform
+ ' project.', err);
-            console.log(stdout);
+            if (err) throw 'An error occurred while emulating/deploying the ' + platform
+ ' project.' + err;
         });
     });
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/30ed15c5/src/platform.js
----------------------------------------------------------------------
diff --git a/src/platform.js b/src/platform.js
index c462b69..16da06b 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -16,7 +16,7 @@ var repos = {
 // each repo.
 // TODO: eventually refactor to allow multiple versions to be created.
 // Currently only checks out HEAD.
-function create(target, dir, cfg) {
+function create(target, dir, cfg, callback) {
     // Check if it already exists.
     try {
         fs.lstatSync(dir);
@@ -29,18 +29,17 @@ function create(target, dir, cfg) {
         exec(cmd, function(err, stderr, stdout) {
             if (err) {
                 cfg.remove_platform(target);
-                console.error('An error occured during creation of ' + target + ' sub-project,
', err);
-            }
+                throw 'An error occured during creation of ' + target + ' sub-project. '
+ err;
+            } else if (callback) callback();
         });
     }
 }
 
-module.exports = function platform(command, target) {
+module.exports = function platform(command, target, callback) {
     var projectRoot = cordova_util.isCordova(process.cwd());
 
     if (!projectRoot) {
-        console.error('Current working directory is not a Cordova-based project.');
-        return;
+        throw 'Current working directory is not a Cordova-based project.';
     }
     if (arguments.length === 0) command = 'ls';
 
@@ -51,10 +50,8 @@ module.exports = function platform(command, target) {
         case 'ls':
             var platforms = cfg.ls_platforms();
             if (platforms.length) {
-                platforms.map(function(p) {
-                    console.log(p);
-                });
-            } else console.log('No platforms added. Use `cordova platform add <platform>`.');
+                return platforms.join('\n');
+            } else return 'No platforms added. Use `cordova platform add <platform>`.';
             break;
         case 'add':
             // Add the platform to the config.xml
@@ -70,14 +67,13 @@ module.exports = function platform(command, target) {
                 console.log('Cloning ' + repos[target] + ', this may take a while...');
                 exec(cmd, function(err, stderr, stdout) {
                     if (err) {
-                        console.error('An error occured during git-clone of ' + repos[target]
+ ', ', err);
                         cfg.remove_platform(target);
-                        return;
+                        throw 'An error occured during git-clone of ' + repos[target] + '.
' + err;
                     }
-                    create(target, output, cfg);
+                    create(target, output, cfg, callback);
                 });
             } else {
-                create(target, output, cfg);
+                create(target, output, cfg, callback);
             }
             break;
         case 'remove':
@@ -90,7 +86,6 @@ module.exports = function platform(command, target) {
             } catch(e) {}
             break;
         default:
-            console.error('Unrecognized command "' + command + '". Use either `add`, `remove`,
or `ls`.');
-            break;
+            throw 'Unrecognized command "' + command + '". Use either `add`, `remove`, or
`ls`.';
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/30ed15c5/src/plugin.js
----------------------------------------------------------------------
diff --git a/src/plugin.js b/src/plugin.js
index 8de5c01..f5acc4b 100644
--- a/src/plugin.js
+++ b/src/plugin.js
@@ -11,8 +11,7 @@ module.exports = function plugin(command, target) {
     var projectRoot = cordova_util.isCordova(process.cwd());
 
     if (!projectRoot) {
-        console.error('Current working directory is not a Cordova-based project.');
-        return;
+        throw 'Current working directory is not a Cordova-based project.';
     }
     if (arguments.length === 0) command = 'ls';
 
@@ -30,24 +29,20 @@ module.exports = function plugin(command, target) {
     switch(command) {
         case 'ls':
             if (plugins.length) {
-                plugins.map(function(plugin) {
-                    console.log(plugin);
-                });
-            } else console.log('No plugins added. Use `cordova plugin add <plugin>.');
+                return plugins.join('\n');
+            } else return 'No plugins added. Use `cordova plugin add <plugin>.';
             break;
         case 'add':
             // Check if we already have the plugin.
             if (plugins.indexOf(targetName) > -1) {
-                console.error('Plugin "' + targetName + '" already added to project.');
-                return;
+                throw 'Plugin "' + targetName + '" already added to project.';
             }
             
             // Check if the plugin has a plugin.xml in the root of the
             // specified dir.
             var pluginContents = ls(target);
             if (pluginContents.indexOf('plugin.xml') == -1) {
-                console.error('Plugin "' + targetName + '" does not have a plugin.xml in
the root. Plugin must support the Cordova Plugin Specification: https://github.com/alunny/cordova-plugin-spec');
-                return;
+                throw 'Plugin "' + targetName + '" does not have a plugin.xml in the root.
Plugin must support the Cordova Plugin Specification: https://github.com/alunny/cordova-plugin-spec';
             }
 
             // Iterate over all platforms in the project and install the
@@ -55,22 +50,18 @@ module.exports = function plugin(command, target) {
             var cli = path.join(__dirname, '..', 'node_modules', 'pluginstall', 'cli.js');
             platforms.map(function(platform) {
                 var cmd = util.format('%s %s "%s" "%s"', cli, platform, path.join(projectRoot,
'platforms', platform), target);
-                console.log(cmd);
                 exec(cmd, function(err, stderr, stdout) {
                     if (err) {
-                        console.error('An error occured during plugin installation. ', err);
                         console.error(stderr);
+                        throw 'An error occured during plugin installation. ' + err;
                     }
                 });
             });
 
             break;
         case 'remove':
-            console.error('Plugin removal not supported yet! sadface');
-            break;
+            throw 'Plugin removal not supported yet! sadface';
         default:
-            console.error('Unrecognized command "' + command + '". Use either `add`, `remove`,
or `ls`.');
-            break;
+            throw 'Unrecognized command "' + command + '". Use either `add`, `remove`, or
`ls`.';
     }
-    
 };


Mime
View raw message