You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ti...@apache.org on 2015/10/12 21:51:36 UTC

cordova-lib git commit: Update 'serve' to use 'express' implementation of cordova-serve.

Repository: cordova-lib
Updated Branches:
  refs/heads/master 1b55e26bc -> 3ebad25e8


Update 'serve' to use 'express' implementation of cordova-serve.


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

Branch: refs/heads/master
Commit: 3ebad25e8cb53d2108372db790693efe4d2e773f
Parents: 1b55e26
Author: Tim Barham <ti...@microsoft.com>
Authored: Mon Sep 28 16:34:53 2015 -0700
Committer: Tim Barham <ti...@microsoft.com>
Committed: Mon Oct 12 12:00:40 2015 -0700

----------------------------------------------------------------------
 cordova-lib/package.json         |   2 +-
 cordova-lib/src/cordova/serve.js | 158 ++++++++++++++--------------------
 2 files changed, 68 insertions(+), 92 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/3ebad25e/cordova-lib/package.json
----------------------------------------------------------------------
diff --git a/cordova-lib/package.json b/cordova-lib/package.json
index b16c295..f6927b8 100644
--- a/cordova-lib/package.json
+++ b/cordova-lib/package.json
@@ -22,7 +22,7 @@
     "cordova-common": "0.1.x",
     "cordova-js": "4.1.1",
     "cordova-registry-mapper": "1.x",
-    "cordova-serve": "^0.1.3",
+    "cordova-serve": "^1.0.0",
     "dep-graph": "1.1.0",
     "elementtree": "0.1.6",
     "glob": "4.0.6",

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/3ebad25e/cordova-lib/src/cordova/serve.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/serve.js b/cordova-lib/src/cordova/serve.js
index eae69fb..a73c054 100644
--- a/cordova-lib/src/cordova/serve.js
+++ b/cordova-lib/src/cordova/serve.js
@@ -21,87 +21,73 @@ var cordova_util = require('./util'),
     crypto       = require('crypto'),
     path         = require('path'),
     shell        = require('shelljs'),
+    url          = require('url'),
     platforms    = require('../platforms/platforms'),
     ConfigParser = require('cordova-common').ConfigParser,
     HooksRunner  = require('../hooks/HooksRunner'),
     Q            = require('q'),
     fs           = require('fs'),
+    events       = require('cordova-common').events,
     serve        = require('cordova-serve');
 
 var projectRoot;
+var installedPlatforms;
 
-function processUrlPath(urlPath, request, response, do302, do404, serveFile) {
-    function doRoot() {
-        var p;
-        response.writeHead(200, {'Content-Type': 'text/html'});
-        var config = new ConfigParser(cordova_util.projectConfig(projectRoot));
-        response.write('<html><head><title>'+config.name()+'</title></head><body>');
-        response.write('<table border cellspacing=0><thead><caption><h3>Package Metadata</h3></caption></thead><tbody>');
-        for (var c in {'name': true, 'packageName': true, 'version': true}) {
-            response.write('<tr><th>' + c + '</th><td>' + config[c]() + '</td></tr>');
-        }
-        response.write('</tbody></table>');
-        response.write('<h3>Platforms</h3><ul>');
-        var installed_platforms = cordova_util.listPlatforms(projectRoot);
-        for (p in platforms) {
-            if (installed_platforms.indexOf(p) >= 0) {
-                response.write('<li><a href="' + p + '/">' + p + '</a></li>\n');
-            } else {
-                response.write('<li><em>' + p + '</em></li>\n');
-            }
-        }
-        response.write('</ul>');
-        response.write('<h3>Plugins</h3><ul>');
-        var pluginPath = path.join(projectRoot, 'plugins');
-        var plugins = cordova_util.findPlugins(pluginPath);
-        for (p in plugins) {
-            response.write('<li>'+plugins[p]+'</li>\n');
-        }
-        response.write('</ul>');
-        response.write('</body></html>');
-        response.end();
-    }
-
-    var firstSegment = /\/(.*?)\//.exec(urlPath);
-
-    if (!firstSegment) {
-        doRoot();
-        return;
-    }
-    var platformId = firstSegment[1];
-    if (!platforms[platformId]) {
-        do404();
+function handleRoot(request, response, next) {
+    if (url.parse(request.url).pathname !== '/') {
+        response.sendStatus(404);
         return;
     }
-    // Strip the platform out of the path.
-    urlPath = urlPath.slice(platformId.length + 1);
 
-    var platformApi;
-    try {
-        platformApi = platforms.getPlatformApi(platformId);
-    } catch (e) {
-        do404();
-        return;
-    }
-
-    var filePath = null;
-
-    if (urlPath == '/config.xml') {
-        filePath = platformApi.getPlatformInfo().configXml;
-    } else if (urlPath == '/project.json') {
-        processAddRequest(request, response, platformApi);
-        return;
-    } else if (/^\/www\//.test(urlPath)) {
-        filePath = path.join(platformApi.getPlatformInfo().locations.www, urlPath.slice(5));
-    } else if (/^\/+[^\/]*$/.test(urlPath)) {
-        do302('/' + platformId + '/www/');
-        return;
-    } else {
-        do404();
-        return;
-    }
+    response.writeHead(200, {'Content-Type': 'text/html'});
+    var config = new ConfigParser(cordova_util.projectConfig(projectRoot));
+    response.write('<html><head><title>' + config.name() + '</title></head><body>');
+    response.write('<table border cellspacing=0><thead><caption><h3>Package Metadata</h3></caption></thead><tbody>');
+    ['name', 'packageName', 'version'].forEach(function (c) {
+        response.write('<tr><th>' + c + '</th><td>' + config[c]() + '</td></tr>');
+    });
+    response.write('</tbody></table>');
+    response.write('<h3>Platforms</h3><ul>');
+    Object.keys(platforms).forEach(function (platform) {
+        if (installedPlatforms.indexOf(platform) >= 0) {
+            response.write('<li><a href="' + platform + '/www/">' + platform + '</a></li>\n');
+        } else {
+            response.write('<li><em>' + platform + '</em></li>\n');
+        }
+    });
+    response.write('</ul>');
+    response.write('<h3>Plugins</h3><ul>');
+    var pluginPath = path.join(projectRoot, 'plugins');
+    var plugins = cordova_util.findPlugins(pluginPath);
+    Object.keys(plugins).forEach(function (plugin) {
+        response.write('<li>' + plugins[plugin] + '</li>\n');
+    });
+    response.write('</ul>');
+    response.write('</body></html>');
+    response.end();
+}
 
-    serveFile(filePath);
+function getPlatformHandler(platform, wwwDir, configXml) {
+    return function (request, response, next) {
+        switch (url.parse(request.url).pathname) {
+            case '/' + platform + '/config.xml':
+                response.sendFile(configXml);
+                break;
+
+            case '/' + platform + '/project.json':
+                response.send({
+                    'configPath': '/' + platform + '/config.xml',
+                    'wwwPath': '/' + platform + '/www',
+                    'wwwFileList': shell.find(wwwDir)
+                        .filter(function (a) { return !fs.statSync(a).isDirectory() && !/(^\.)|(\/\.)/.test(a); })
+                        .map(function (a) { return {'path': a.slice(wwwDir.length), 'etag': '' + calculateMd5(a)}; })
+                });
+                break;
+
+            default:
+                next();
+        }
+    };
 }
 
 function calculateMd5(fileName) {
@@ -125,40 +111,30 @@ function calculateMd5(fileName) {
     return md5sum.digest('hex');
 }
 
-function processAddRequest(request, response, platformApi) {
-    var wwwDir = platformApi.getPlatformInfo().locations.www;
-    var payload = {
-        'configPath': '/' + platformApi.platform + '/config.xml',
-        'wwwPath': '/' + platformApi.platform + '/www',
-        'wwwFileList': shell.find(wwwDir)
-            .filter(function(a) { return !fs.statSync(a).isDirectory() && !/(^\.)|(\/\.)/.test(a); })
-            .map(function(a) { return {'path': a.slice(wwwDir.length), 'etag': '' + calculateMd5(a)}; })
-    };
-    console.log('200 ' + request.url);
-    response.writeHead(200, {
-        'Content-Type': 'application/json',
-        'Cache-Control': 'no-cache'
-    });
-    response.write(JSON.stringify(payload));
-    response.end();
-}
-
 module.exports = function server(port) {
     var d = Q.defer();
     projectRoot = cordova_util.cdProjectRoot();
     port = +port || 8000;
 
     var hooksRunner = new HooksRunner(projectRoot);
-    hooksRunner.fire('before_serve')
-    .then(function () {
+    hooksRunner.fire('before_serve').then(function () {
         // Run a prepare first!
         return require('./cordova').raw.prepare([]);
     }).then(function () {
-        var server = serve.launchServer({port: port, urlPathHandler: processUrlPath}).server;
+        var server = serve();
+
+        installedPlatforms = cordova_util.listPlatforms(projectRoot);
+        installedPlatforms.forEach(function (platform) {
+            var locations = platforms.getPlatformApi(platform).getPlatformInfo().locations;
+            server.app.use('/' + platform + '/www', serve.static(locations.www));
+            server.app.get('/' + platform + '/*', getPlatformHandler(platform, locations.www, locations.configXml));
+        });
+        server.app.get('*', handleRoot);
+
+        server.launchServer({port: port, events: events});
         hooksRunner.fire('after_serve').then(function () {
-            d.resolve(server);
+            d.resolve(server.server);
         });
     });
     return d.promise;
 };
-


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