cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [33/50] cordova-paramedic git commit: split module out so this can be used both as a module and a cli
Date Fri, 04 Mar 2016 00:36:11 GMT
split module out so this can be used both as a module and a cli


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

Branch: refs/heads/master
Commit: bffb14e175ae4439e851544d2a171926a926d173
Parents: 9718efe
Author: Jesse MacFadyen <purplecabbage@gmail.com>
Authored: Mon Mar 23 15:40:49 2015 -0700
Committer: Jesse MacFadyen <purplecabbage@gmail.com>
Committed: Mon Mar 23 15:40:49 2015 -0700

----------------------------------------------------------------------
 README.md    |  12 ++-
 main.js      | 285 +++---------------------------------------------------
 package.json |   2 +-
 paramedic.js | 284 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 308 insertions(+), 275 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-paramedic/blob/bffb14e1/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index c65482c..77d9034 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,8 @@ To install :
 
 Usage :
 
-```cordova-paramedic --platform CORDOVA-PLATFORM --plugin PLUGIN-PATH [--justbuild --timeout
MSECS --port PORTNUM]```
+```
+cordova-paramedic --platform CORDOVA-PLATFORM --plugin PLUGIN-PATH [--justbuild --timeout
MSECS --port PORTNUM]
 
 	`CORDOVA-PLATFORM` : the platform id, currently only supports 'ios'
 	`PLUGIN-PATH` : the relative or absolute path to a plugin folder
@@ -23,4 +24,13 @@ Usage :
 			  (defaults to 10 minutes)
 	`PORTNUM` : (optional) port to use for posting results from emulator back to paramedic server
 	--justbuild : (optional) just builds the project, without running the tests
+```
+
+You can also use cordova-paramedic as a module directly :
+
+```
+  var paramedic = require('cordova-paramedic');
+  paramedic.run('ios','../cordova-plugin-device',bJustBuild,nPort,msTimeout);
+```
+
 

http://git-wip-us.apache.org/repos/asf/cordova-paramedic/blob/bffb14e1/main.js
----------------------------------------------------------------------
diff --git a/main.js b/main.js
index ee97e65..b0c0c86 100755
--- a/main.js
+++ b/main.js
@@ -1,289 +1,28 @@
 #!/usr/bin/env node
 
-var http = require('http'),
-    localtunnel = require('localtunnel'),
-    parseArgs = require('minimist'),
-    shell = require('shelljs'),
-    fs = require('fs'),
-    request = require('request'),
-    tmp = require('tmp'),
-    path = require('path');
-
-var tunneledUrl = "";
-var PORT = 8008;
-var TIMEOUT = 10 * 60 * 1000; // 10 minutes in msec - this will become a param
-var USAGE = "Error missing args. \n Usage: $cordova-paramedic " +
-            "  --platform CORDOVA-PLATFORM --plugin PLUGIN-PATH [--port PORT]";
-
-var TMP_FOLDER = null;
-var storedCWD = process.cwd();
-
-var JustBuild = false;
+var parseArgs = require('minimist'),
+    paramedic = require('./paramedic');
 
 var plugins,
     platformId;
 
+var USAGE = "Error missing args. \n Usage: $cordova-paramedic " +
+            "  --platform CORDOVA-PLATFORM --plugin PLUGIN-PATH [--port PORT]";    
 
-(function run_main() { // main program here 
-    init();
-    createTempProject();
-    installPlugins();
-    startServer();
-})();
-
-function init() {
-    var argv = parseArgs(process.argv.slice(2));
-
-    if(!argv.platform || !argv.plugin) {
-        console.log(USAGE);
-        process.exit(1);
-    }
-
-    platformId = argv.platform;
-    plugins = argv.plugin;
-    plugins = Array.isArray(plugins) ? plugins : [plugins];
-
-
-    // TODO: validate that port is a number
-    PORT = argv.port || PORT;
-    JustBuild = argv.justbuild || JustBuild; 
-    TIMEOUT = argv.timeout || TIMEOUT;
-
-    var cordovaResult = shell.exec('cordova --version', {silent:true});
-    if(cordovaResult.code) {
-        console.error(cordovaResult.output);
-        process.exit(cordovaResult.code);
-    }
-}
-
-function createTempProject() {
-    TMP_FOLDER = tmp.dirSync();
-    tmp.setGracefulCleanup();
-    
-    console.log("cordova-paramedic :: creating temp project");
-    shell.exec('cordova create ' + TMP_FOLDER.name);
-    shell.cd(TMP_FOLDER.name);
-}
-
-function installSinglePlugin(plugin) {
-    console.log("cordova-paramedic :: installing " + plugin);
-    
-    var pluginPath = path.resolve(storedCWD, plugin);
-
-    var installExitCode = shell.exec('cordova plugin add ' + pluginPath,
-                                     {silent:true}).code;
-    if(installExitCode !== 0) {
-        console.error('Failed to install plugin : ' + plugin);
-        cleanUpAndExitWithCode(1);
-        return;
-    }
-}
-
-
-function installPlugins() {
-
-    for(var n = 0; n < plugins.length; n++) {
-
-        var plugin = plugins[n];
-        installSinglePlugin(plugin);
-
-        if(!JustBuild) {
-            installSinglePlugin(path.join(plugin,"tests"));
-        }
-    }
-
-
-    if(!JustBuild) {
-        console.log("cordova-paramedic :: installing plugin-test-framework");
-        installExitCode = shell.exec('cordova plugin add https://github.com/apache/cordova-plugin-test-framework',
-                                     {silent:true}).code;
-        if(installExitCode !== 0) {
-            console.error('cordova-plugin-test-framework');
-            cleanUpAndExitWithCode(1);
-            return;
-        }
-    }
-}
-
-function addAndRunPlatform() {
-
-    if(JustBuild) {
-        console.log("cordova-paramedic :: adding platform");
-        shell.exec('cordova platform add ' + platformId,{silent:true});
-        shell.exec('cordova prepare',{silent:true});
-        console.log("building ...");
-        shell.exec('cordova build ' + platformId.split("@")[0],
-            {async:true,silent:true},
-            function(code,output){
-                if(code !== 0) {
-                    console.error("Error: cordova build returned error code " + code);
-                    console.log("output: " + output);
-                    cleanUpAndExitWithCode(1);
-                }
-                else {
-                    console.log("lookin' good!");
-                    cleanUpAndExitWithCode(0);
-                }
-            }
-        );
-    }
-    else {
-        setConfigStartPage();
-        console.log("cordova-paramedic :: adding platform");
-        shell.exec('cordova platform add ' + platformId,{silent:true});
-        shell.exec('cordova prepare',{silent:true});
-        // limit runtime to TIMEOUT msecs
-        setTimeout(function(){
-            console.error("This test seems to be blocked :: timeout exceeded. Exiting ...");
-            cleanUpAndExitWithCode(1);
-        },(TIMEOUT));
-
-        shell.exec('cordova emulate ' + platformId.split("@")[0] + " --phone",
-            {async:true,silent:true},
-            function(code,output){
-                if(code !== 0) {
-                    console.error("Error: cordova emulate return error code " + code);
-                    console.log("output: " + output);
-                    cleanUpAndExitWithCode(1);
-                }
-            }
-        );
-    }
-}
-
-function cleanUpAndExitWithCode(exitCode) {
-    shell.cd(storedCWD);
-    // the TMP_FOLDER.removeCallback() call is throwing an exception, so we explicitly delete
it here
-    shell.exec('rm -rf ' + TMP_FOLDER.name);
-    
-    process.exit(exitCode);
-}
-
-function writeMedicLogUrl(url) {
-    console.log("cordova-paramedic :: writing medic log url to project");
-    var obj = {logurl:url};
-    fs.writeFileSync(path.join("www","medic.json"),JSON.stringify(obj));
-}
-
-
-function setConfigStartPage() {
-
-    console.log("cordova-paramedic :: setting app start page to test page");
+var argv = parseArgs(process.argv.slice(2));
 
-    var fileName = 'config.xml';
-    var configStr = fs.readFileSync(fileName).toString();
-    if(configStr) {
-        configStr = configStr.replace("src=\"index.html\"","src=\"cdvtests/index.html\"");
-        fs.writeFileSync(fileName, configStr);
-    }
-    else {
-        console.error("Oops, could not find config.xml");
-    }
+if(!argv.platform || !argv.plugin) {
+    console.log(USAGE);
+    process.exit(1);
 }
 
-function startServer() {
+platformId = argv.platform;
+plugins = argv.plugin;
+plugins = Array.isArray(plugins) ? plugins : [plugins];
 
-    if(JustBuild) {
-        addAndRunPlatform();
-        return;
-    }
 
-    console.log("cordova-paramedic :: starting local medic server " + platformId);
-    var server = http.createServer(requestListener);
-    server.listen(PORT, '127.0.0.1',function onServerConnect() {
+paramedic.run(platformId,plugins,argv.justbuild,argv.port,argv.timeout);
 
-        switch(platformId) {
-            case "ios"     :  // intentional fallthrough
-            case "windows" :
-                writeMedicLogUrl("http://127.0.0.1:" + PORT);
-                addAndRunPlatform();
-                break;
-            case "android" :
-                writeMedicLogUrl("http://10.0.2.2:" + PORT);
-                addAndRunPlatform();
-                break;
-            case "wp8" :
-                //localtunnel(PORT, tunnelCallback);
-                request.get('http://google.com/', function(e, res, data) {
-                    if(e) {
-                        console.error("failed to detect ip address");
-                        cleanUpAndExitWithCode(1);
-                    }
-                    else {
-                        console.log("res.req.connection = " + res.req.connection);
-                        var ip = res.req.connection.localAddress ||
-                                 res.req.socket.localAddress;
-                        console.log("Using ip : " + ip);
-                        writeMedicLogUrl("http://" + ip + ":" + PORT);
-                        addAndRunPlatform();
-                    }
-                });
-                break;
-            default :
-                console.log("platform is not supported :: " + platformId);
-                cleanUpAndExitWithCode(1);
-        }
-    });
-}
 
-function requestListener(request, response) {
-    if (request.method == 'PUT' || request.method == 'POST') {
-        var body = '';
-        request.on('data', function (data) {
-            body += data;
-            // Too much POST data, kill the connection!
-            if (body.length > 1e6) {
-                req.connection.destroy();
-            }
-        });
-        request.on('end', function (res) {
-            if(body.indexOf("mobilespec")  == 2){ // {\"mobilespec\":{...}}
-                try {
-                    console.log("body = " + body);
-                    var results = JSON.parse(body);
-                    console.log("Results:: ran " + 
-                        results.mobilespec.specs + 
-                        " specs with " + 
-                        results.mobilespec.failures + 
-                        " failures");
-                    if(results.mobilespec.failures > 0) {
-                        cleanUpAndExitWithCode(1);
-                    }
-                    else {
-                        cleanUpAndExitWithCode(0);
-                    }
-                    
-                }
-                catch(err) {
-                    console.log("parse error :: " + err);
-                    cleanUpAndExitWithCode(1);
-                }
-            }
-            else {
-                console.log("console-log:" + body);
-            }
-        });
-    }
-    else {
-        console.log(request.method);
-        response.writeHead(200, { 'Content-Type': 'text/plain'});
-        response.write("Hello"); // sanity check to make sure server is running
-        response.end();
-    }
-}
 
-function tunnelCallback(err, tunnel) {
-    if (err){
-        console.log("failed to create tunnel url, check your internet connectivity.");
-        cleanUpAndExitWithCode(1);
-    }
-    else {
-        // the assigned public url for your tunnel
-        // i.e. https://abcdefgjhij.localtunnel.me
-        tunneledUrl = tunnel.url;
-        console.log("cordova-paramedic :: tunneledURL = " + tunneledUrl);
-        writeMedicLogUrl(tunneledUrl);
-        addAndRunPlatform();
-    }
-}
 

http://git-wip-us.apache.org/repos/asf/cordova-paramedic/blob/bffb14e1/package.json
----------------------------------------------------------------------
diff --git a/package.json b/package.json
index a8e2b14..156901f 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
   "name": "cordova-paramedic",
   "version": "0.0.11",
   "description": "Use medic to test a cordova plugin locally",
-  "main": "cordova-paramedic",
+  "main": "paramedic.js",
   "bin": {
     "cordova-paramedic": "./main.js"
   },

http://git-wip-us.apache.org/repos/asf/cordova-paramedic/blob/bffb14e1/paramedic.js
----------------------------------------------------------------------
diff --git a/paramedic.js b/paramedic.js
new file mode 100644
index 0000000..88cd09c
--- /dev/null
+++ b/paramedic.js
@@ -0,0 +1,284 @@
+#!/usr/bin/env node
+
+var http = require('http'),
+    localtunnel = require('localtunnel'),
+    parseArgs = require('minimist'),
+    shell = require('shelljs'),
+    fs = require('fs'),
+    request = require('request'),
+    tmp = require('tmp'),
+    path = require('path');
+
+var tunneledUrl = "";
+var PORT = 8008;
+var TIMEOUT = 10 * 60 * 1000; // 10 minutes in msec - this will become a param
+
+var TMP_FOLDER = null;
+var storedCWD = process.cwd();
+
+var JustBuild = false;
+
+var plugins,
+    platformId;
+
+
+exports.run = function(_platformId,_plugins,bJustBuild,nPort,msTimeout) {
+
+    if(_platformId && _plugins) {
+
+        platformId = _platformId;
+        // make it an array if it's not
+        plugins = Array.isArray(_plugins) ? _plugins : [_plugins];
+
+        JustBuild = bJustBuild == true;
+        PORT = nPort || PORT;
+        TIMEOUT = msTimeout || TIMEOUT;
+
+        var cordovaResult = shell.exec('cordova --version', {silent:true});
+        if(cordovaResult.code) {
+            console.error(cordovaResult.output);
+            process.exit(cordovaResult.code);
+        }
+
+        createTempProject();
+        installPlugins();
+        startServer();
+    }
+    else {
+        console.log("Error : Missing platformId and/or plugins");
+    }
+
+}
+
+
+function createTempProject() {
+    TMP_FOLDER = tmp.dirSync();
+    tmp.setGracefulCleanup();
+    
+    console.log("cordova-paramedic :: creating temp project");
+    shell.exec('cordova create ' + TMP_FOLDER.name);
+    shell.cd(TMP_FOLDER.name);
+}
+
+function installSinglePlugin(plugin) {
+    console.log("cordova-paramedic :: installing " + plugin);
+    
+    var pluginPath = path.resolve(storedCWD, plugin);
+
+    var installExitCode = shell.exec('cordova plugin add ' + pluginPath,
+                                     {silent:true}).code;
+    if(installExitCode !== 0) {
+        console.error('Failed to install plugin : ' + plugin);
+        cleanUpAndExitWithCode(1);
+        return;
+    }
+}
+
+
+function installPlugins() {
+
+    for(var n = 0; n < plugins.length; n++) {
+
+        var plugin = plugins[n];
+        installSinglePlugin(plugin);
+
+        if(!JustBuild) {
+            installSinglePlugin(path.join(plugin,"tests"));
+        }
+    }
+
+
+    if(!JustBuild) {
+        console.log("cordova-paramedic :: installing plugin-test-framework");
+        installExitCode = shell.exec('cordova plugin add https://github.com/apache/cordova-plugin-test-framework',
+                                     {silent:true}).code;
+        if(installExitCode !== 0) {
+            console.error('cordova-plugin-test-framework');
+            cleanUpAndExitWithCode(1);
+            return;
+        }
+    }
+}
+
+function addAndRunPlatform() {
+
+    if(JustBuild) {
+        console.log("cordova-paramedic :: adding platform");
+        shell.exec('cordova platform add ' + platformId,{silent:true});
+        shell.exec('cordova prepare',{silent:true});
+        console.log("building ...");
+        shell.exec('cordova build ' + platformId.split("@")[0],
+            {async:true,silent:true},
+            function(code,output){
+                if(code !== 0) {
+                    console.error("Error: cordova build returned error code " + code);
+                    console.log("output: " + output);
+                    cleanUpAndExitWithCode(1);
+                }
+                else {
+                    console.log("lookin' good!");
+                    cleanUpAndExitWithCode(0);
+                }
+            }
+        );
+    }
+    else {
+        setConfigStartPage();
+        console.log("cordova-paramedic :: adding platform");
+        shell.exec('cordova platform add ' + platformId,{silent:true});
+        shell.exec('cordova prepare',{silent:true});
+        // limit runtime to TIMEOUT msecs
+        setTimeout(function(){
+            console.error("This test seems to be blocked :: timeout exceeded. Exiting ...");
+            cleanUpAndExitWithCode(1);
+        },(TIMEOUT));
+
+        shell.exec('cordova emulate ' + platformId.split("@")[0] + " --phone",
+            {async:true,silent:true},
+            function(code,output){
+                if(code !== 0) {
+                    console.error("Error: cordova emulate return error code " + code);
+                    console.log("output: " + output);
+                    cleanUpAndExitWithCode(1);
+                }
+            }
+        );
+    }
+}
+
+function cleanUpAndExitWithCode(exitCode) {
+    shell.cd(storedCWD);
+    // the TMP_FOLDER.removeCallback() call is throwing an exception, so we explicitly delete
it here
+    shell.exec('rm -rf ' + TMP_FOLDER.name);
+    
+    process.exit(exitCode);
+}
+
+function writeMedicLogUrl(url) {
+    console.log("cordova-paramedic :: writing medic log url to project");
+    var obj = {logurl:url};
+    fs.writeFileSync(path.join("www","medic.json"),JSON.stringify(obj));
+}
+
+
+function setConfigStartPage() {
+
+    console.log("cordova-paramedic :: setting app start page to test page");
+
+    var fileName = 'config.xml';
+    var configStr = fs.readFileSync(fileName).toString();
+    if(configStr) {
+        configStr = configStr.replace("src=\"index.html\"","src=\"cdvtests/index.html\"");
+        fs.writeFileSync(fileName, configStr);
+    }
+    else {
+        console.error("Oops, could not find config.xml");
+    }
+}
+
+function startServer() {
+
+    if(JustBuild) {
+        addAndRunPlatform();
+        return;
+    }
+
+    console.log("cordova-paramedic :: starting local medic server " + platformId);
+    var server = http.createServer(requestListener);
+    server.listen(PORT, '127.0.0.1',function onServerConnect() {
+
+        switch(platformId) {
+            case "ios"     :  // intentional fallthrough
+            case "windows" :
+                writeMedicLogUrl("http://127.0.0.1:" + PORT);
+                addAndRunPlatform();
+                break;
+            case "android" :
+                writeMedicLogUrl("http://10.0.2.2:" + PORT);
+                addAndRunPlatform();
+                break;
+            case "wp8" :
+                //localtunnel(PORT, tunnelCallback);
+                request.get('http://google.com/', function(e, res, data) {
+                    if(e) {
+                        console.error("failed to detect ip address");
+                        cleanUpAndExitWithCode(1);
+                    }
+                    else {
+                        console.log("res.req.connection = " + res.req.connection);
+                        var ip = res.req.connection.localAddress ||
+                                 res.req.socket.localAddress;
+                        console.log("Using ip : " + ip);
+                        writeMedicLogUrl("http://" + ip + ":" + PORT);
+                        addAndRunPlatform();
+                    }
+                });
+                break;
+            default :
+                console.log("platform is not supported :: " + platformId);
+                cleanUpAndExitWithCode(1);
+        }
+    });
+}
+
+function requestListener(request, response) {
+    if (request.method == 'PUT' || request.method == 'POST') {
+        var body = '';
+        request.on('data', function (data) {
+            body += data;
+            // Too much POST data, kill the connection!
+            if (body.length > 1e6) {
+                req.connection.destroy();
+            }
+        });
+        request.on('end', function (res) {
+            if(body.indexOf("mobilespec")  == 2){ // {\"mobilespec\":{...}}
+                try {
+                    console.log("body = " + body);
+                    var results = JSON.parse(body);
+                    console.log("Results:: ran " + 
+                        results.mobilespec.specs + 
+                        " specs with " + 
+                        results.mobilespec.failures + 
+                        " failures");
+                    if(results.mobilespec.failures > 0) {
+                        cleanUpAndExitWithCode(1);
+                    }
+                    else {
+                        cleanUpAndExitWithCode(0);
+                    }
+                    
+                }
+                catch(err) {
+                    console.log("parse error :: " + err);
+                    cleanUpAndExitWithCode(1);
+                }
+            }
+            else {
+                console.log("console-log:" + body);
+            }
+        });
+    }
+    else {
+        console.log(request.method);
+        response.writeHead(200, { 'Content-Type': 'text/plain'});
+        response.write("Hello"); // sanity check to make sure server is running
+        response.end();
+    }
+}
+
+function tunnelCallback(err, tunnel) {
+    if (err){
+        console.log("failed to create tunnel url, check your internet connectivity.");
+        cleanUpAndExitWithCode(1);
+    }
+    else {
+        // the assigned public url for your tunnel
+        // i.e. https://abcdefgjhij.localtunnel.me
+        tunneledUrl = tunnel.url;
+        console.log("cordova-paramedic :: tunneledURL = " + tunneledUrl);
+        writeMedicLogUrl(tunneledUrl);
+        addAndRunPlatform();
+    }
+}
+


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message