cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fil...@apache.org
Subject [11/50] git commit: Add `serve` command.
Date Wed, 28 Nov 2012 18:16:23 GMT
Add `serve` command.


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

Branch: refs/heads/master
Commit: 6033427e8c0c4e2a5a7ed24d8b8ff64fca8b3910
Parents: bab6419
Author: Braden Shepherdson <braden@chromium.org>
Authored: Thu Nov 8 15:18:44 2012 -0500
Committer: Braden Shepherdson <braden@chromium.org>
Committed: Thu Nov 8 15:18:44 2012 -0500

----------------------------------------------------------------------
 README.md                         |    1 +
 bin/cordova                       |    2 +-
 cordova.js                        |    1 +
 doc/help.txt                      |    4 +
 src/metadata/android_parser.js    |    6 ++
 src/metadata/blackberry_parser.js |    6 ++
 src/metadata/ios_parser.js        |    6 ++
 src/serve.js                      |  109 ++++++++++++++++++++++++++++++++
 8 files changed, 134 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6033427e/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index b36f46d..9289d54 100644
--- a/README.md
+++ b/README.md
@@ -46,6 +46,7 @@ cordova-client has a single global `create` command that creates new cordova
pro
 - `plugin [rm | remove] <plugin-name> [<plugin-name> ...]` remove one (or more)
added plugins
 - `build [<platform> [<platform> [...]]]` compile the app and deploy to a connected
+ compatible device. With no parameters builds for all platforms added to the project, otherwise
builds for the specified platforms
 - `emulate [<platform> [<platform> [...]]]` launch emulators and deploy app to
them. With no parameters emulates for all platforms added to the project, otherwise emulates
for the specified platforms
+- `serve <platform> [port]` launch a local web server for that platform's www directory
on the given port (default 8000).
 
 
 # Project Directory Structure

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6033427e/bin/cordova
----------------------------------------------------------------------
diff --git a/bin/cordova b/bin/cordova
index a83cf6c..4d29175 100755
--- a/bin/cordova
+++ b/bin/cordova
@@ -27,7 +27,7 @@ if (version) {
 } else if (cordova.hasOwnProperty(cmd)) {
     var opts = Array.prototype.slice.call(tokens, 0);
     var r;
-    if (cmd == 'create' || cmd == 'docs') {
+    if (cmd == 'create' || cmd == 'docs' || cmd == 'serve') {
         r = cordova[cmd].apply(this, opts);
     } else if (cmd == 'emulate' || cmd == 'build') {
         r = cordova[cmd].call(this, opts);

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6033427e/cordova.js
----------------------------------------------------------------------
diff --git a/cordova.js b/cordova.js
index 56a3c61..7d56ab1 100755
--- a/cordova.js
+++ b/cordova.js
@@ -7,6 +7,7 @@ module.exports = {
     build:    require('./src/build'),
     emulate:  require('./src/emulate'),
     plugin:   require('./src/plugin'),
+    serve:    require('./src/serve'),
     on:       function() {
         cordova_events.on.apply(cordova_events, arguments);
     },

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6033427e/doc/help.txt
----------------------------------------------------------------------
diff --git a/doc/help.txt b/doc/help.txt
index 254146a..c0cc0ac 100644
--- a/doc/help.txt
+++ b/doc/help.txt
@@ -13,6 +13,9 @@ Project-Level Commands
     plugin [add|remove|ls [path]] ..... adds or removes a plugin (from the specified path),
or lists all currently-added plugins
     build ............................. builds a cordova project
     emulate ........................... starts emulator for cordova project
+    serve <platform> [port] ........... runs a local web server for the www/ directory
of the given platform
+                                        the default port is 8000
+                                        note that you must edit the native code to point
at the server!
     help .............................. shows this!
 
 Example usage
@@ -21,4 +24,5 @@ Example usage
     $ cd Baz
     $ cordova platform add android
     $ cordova build
+    $ cordova serve android
 

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6033427e/src/metadata/android_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/android_parser.js b/src/metadata/android_parser.js
index fae804f..3c6a844 100644
--- a/src/metadata/android_parser.js
+++ b/src/metadata/android_parser.js
@@ -52,6 +52,11 @@ module.exports.prototype = {
         });
     },
 
+    // Returns the platform-specific www directory.
+    www_dir:function() {
+        return path.join(this.path, 'assets', 'www');
+    },
+
     update_www:function() {
         var projectRoot = util.isCordova(process.cwd());
         var www = path.join(projectRoot, 'www');
@@ -65,3 +70,4 @@ module.exports.prototype = {
         this.update_www();
     }
 };
+

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6033427e/src/metadata/blackberry_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/blackberry_parser.js b/src/metadata/blackberry_parser.js
index cfc3262..377db45 100644
--- a/src/metadata/blackberry_parser.js
+++ b/src/metadata/blackberry_parser.js
@@ -49,6 +49,12 @@ module.exports.prototype = {
         this.write_project_properties();
         if (callback) callback();
     },
+
+    // Returns the platform-specific www directory.
+    www_dir:function() {
+        return path.join(this.path, 'www');
+    },
+
     update_www:function() {
         var projectRoot = util.isCordova(this.path);
         var www = path.join(projectRoot, 'www');

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6033427e/src/metadata/ios_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/ios_parser.js b/src/metadata/ios_parser.js
index e841bd6..0dcf322 100644
--- a/src/metadata/ios_parser.js
+++ b/src/metadata/ios_parser.js
@@ -52,6 +52,12 @@ module.exports.prototype = {
             }
         });
     },
+
+    // Returns the platform-specific www directory.
+    www_dir:function() {
+        return path.join(this.path, 'www');
+    },
+
     update_www:function() {
         var projectRoot = util.isCordova(process.cwd());
         var www = path.join(projectRoot, 'www');

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6033427e/src/serve.js
----------------------------------------------------------------------
diff --git a/src/serve.js b/src/serve.js
new file mode 100644
index 0000000..b80610f
--- /dev/null
+++ b/src/serve.js
@@ -0,0 +1,109 @@
+var cordova_util = require('./util'),
+    path = require('path'),
+    shell = require('shelljs'),
+    config_parser = require('./config_parser'),
+    android_parser = require('./metadata/android_parser'),
+    ios_parser = require('./metadata/ios_parser'),
+    blackberry_parser = require('./metadata/blackberry_parser'),
+    fs = require('fs'),
+    ls = fs.readdirSync,
+    util = require('util'),
+    http = require("http"),
+    url = require("url");
+
+function launch_server(www, port) {
+    port = port || 8000;
+
+    http.createServer(function(request, response) {
+        var uri = url.parse(request.url).pathname;
+        var filename = path.join(www, uri);
+
+        fs.exists(filename, function(exists) {
+            if(!exists) {
+                response.writeHead(404, {"Content-Type": "text/plain"});
+                response.write("404 Not Found\n");
+                response.end();
+                return;
+            }
+
+            if (fs.statSync(filename).isDirectory()) filename += path.sep + 'index.html';
+
+            fs.readFile(filename, "binary", function(err, file) {
+                if(err) {
+                    response.writeHead(500, {"Content-Type": "text/plain"});
+                    response.write(err + "\n");
+                    response.end();
+                    return;
+                }
+
+                response.writeHead(200);
+                response.write(file, "binary");
+                response.end();
+            });
+        });
+    }).listen(parseInt(''+port, 10));
+
+    console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL
+ C to shutdown");
+}
+
+module.exports = function serve (platform, port) {
+    var projectRoot = cordova_util.isCordova(process.cwd());
+
+    if (!projectRoot) {
+        throw 'Current working directory is not a Cordova-based project.';
+    }
+
+    var xml = path.join(projectRoot, 'www', 'config.xml');
+    var cfg = new config_parser(xml);
+
+    // Retrieve the platforms.
+    var platforms = ls(path.join(projectRoot, 'platforms'));
+    if (!platform) {
+        console.log('You need to specify a platform.');
+        process.exit(1);
+    } else if (platforms.length == 0) {
+        console.log('No platforms to serve.');
+        process.exit(1);
+    } else if (platforms.filter(function(x) { return x == platform }).length == 0) {
+        console.log(platform + ' is not an installed platform.');
+        process.exit(1);
+    }
+
+    // If we got to this point, the given platform is valid.
+
+    // Default port is 8000 if not given. This is also the default of the Python module.
+    port = port || 8000;
+
+    var parser, platformPath;
+    switch (platform) {
+        case 'android':
+            platformPath = path.join(projectRoot, 'platforms', 'android');
+            parser = new android_parser(platformPath);
+
+            // Update the related platform project from the config
+            parser.update_project(cfg);
+            var www = parser.www_dir();
+            launch_server(www, port);
+            break;
+        case 'blackberry-10':
+            platformPath = path.join(projectRoot, 'platforms', 'blackberry-10');
+            parser = new blackberry_parser(platformPath);
+
+            // Update the related platform project from the config
+            parser.update_project(cfg, function() {
+                // Shell it
+                launch_server(parser.www_dir(), port);
+            });
+            break;
+        case 'ios':
+            platformPath = path.join(projectRoot, 'platforms', 'ios');
+            js = path.join(__dirname, '..', 'lib', 'ios', 'CordovaLib', 'javascript', 'cordova.ios.js');
+            parser = new ios_parser(platformPath);
+            // Update the related platform project from the config
+            parser.update_project(cfg, function() {
+                launch_server(parser.www_dir(), port);
+            });
+            break;
+    }
+};
+


Mime
View raw message