You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2017/08/12 08:02:00 UTC

[09/12] cordova-serve git commit: remove 'q' dependence completely. Added server.spec

remove 'q' dependence completely. Added server.spec


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

Branch: refs/heads/master
Commit: db1ae5518dbf0938da1035dc386102946b821add
Parents: 2be7e2b
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Fri Jun 30 17:56:59 2017 -0700
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Fri Jun 30 17:56:59 2017 -0700

----------------------------------------------------------------------
 package.json        |  1 -
 spec/server.spec.js | 47 ++++++++++++++++++++++++
 src/platform.js     | 28 ++++++++------
 src/server.js       | 96 +++++++++++++++++++++++++-----------------------
 4 files changed, 115 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/db1ae551/package.json
----------------------------------------------------------------------
diff --git a/package.json b/package.json
index 4d20d9d..7df53cd 100644
--- a/package.json
+++ b/package.json
@@ -28,7 +28,6 @@
     "compression": "^1.6.0",
     "express": "^4.13.3",
     "open": "0.0.5",
-    "q": "^1.4.1",
     "shelljs": "^0.5.3"
   },
   "devDependencies": {

http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/db1ae551/spec/server.spec.js
----------------------------------------------------------------------
diff --git a/spec/server.spec.js b/spec/server.spec.js
new file mode 100644
index 0000000..fb35c1f
--- /dev/null
+++ b/spec/server.spec.js
@@ -0,0 +1,47 @@
+/**
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    http://www.apache.org/licenses/LICENSE-2.0
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+*/
+
+var server = require("../src/server");
+
+function expectPromise(obj){
+    // 3 slightly different ways of verifying a promise
+    expect(typeof obj.then).toBe('function');
+    expect(obj instanceof Promise).toBe(true);
+    expect(obj).toBe(Promise.resolve(obj));
+}
+
+describe('server', function() {
+
+    it('exists and has expected properties', function() {
+        expect(server).toBeDefined();
+        expect(typeof server).toBe('function');
+    });
+
+    it('should return a promise', function(done) {
+        var result = server({port:8008,noServerInfo:1});
+        expect(result).toBeDefined();
+        expectPromise(result);
+        result.then(function(res) {
+            console.log("success : " + res);
+            done();
+        });
+        result.catch(function(err){
+            console.log("error : " + err);
+            done();
+        });
+    });
+});
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/db1ae551/src/platform.js
----------------------------------------------------------------------
diff --git a/src/platform.js b/src/platform.js
index 7abbb81..27f72b4 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -18,7 +18,6 @@
  */
 
 var fs     = require('fs'),
-    Q      = require('q'),
     util   = require('./util');
 
 /**
@@ -30,23 +29,30 @@ var fs     = require('fs'),
  * @returns {*|promise}
  */
 module.exports = function (platform, opts) {
+
+    // note: `this` is actually an instance of main.js CordovaServe
+    // this module is a mixin
     var that = this;
-    return Q().then(function () {
+    var retPromise = new Promise(function(resolve,reject){
         if (!platform) {
-            throw new Error('A platform must be specified');
+            reject('Error: A platform must be specified');
         }
+        else {
+            opts = opts || {};
+            var projectRoot = findProjectRoot(opts.root);
+            that.projectRoot = projectRoot;
+            opts.root = util.getPlatformWwwRoot(projectRoot, platform);
 
-        opts = opts || {};
-        var projectRoot = findProjectRoot(opts.root);
-        that.projectRoot = projectRoot;
-
-        opts.root = util.getPlatformWwwRoot(projectRoot, platform);
-        if (!fs.existsSync(opts.root)) {
-            throw new Error('Project does not include the specified platform: ' + platform);
+            if (!fs.existsSync(opts.root)) {
+                reject('Error: Project does not include the specified platform: ' + platform);
+            }
+            else {
+                return that.launchServer(opts);
+            }
         }
 
-        return that.launchServer(opts);
     });
+    return retPromise;
 };
 
 function findProjectRoot(path) {

http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/db1ae551/src/server.js
----------------------------------------------------------------------
diff --git a/src/server.js b/src/server.js
index 9040d2b..03bf5fe 100644
--- a/src/server.js
+++ b/src/server.js
@@ -18,8 +18,7 @@
  */
 
 var chalk   = require('chalk'),
-    express = require('express'),
-    Q       = require('q');
+    express = require('express');
 
 /**
  * @desc Launches a server with the specified options and optional custom handlers.
@@ -27,56 +26,63 @@ var chalk   = require('chalk'),
  * @returns {*|promise}
  */
 module.exports = function (opts) {
-    var deferred = Q.defer();
 
-    opts = opts || {};
-    var port = opts.port || 8000;
+   var that = this;
+   var promise = new Promise(function(resolve,reject){
 
-    var log = module.exports.log = function (msg) {
-        if (!opts.noLogOutput) {
-            if (opts.events) {
-                opts.events.emit('log', msg);
-            } else {
-                console.log(msg);
-            }
-        }
-    };
-
-    var app = this.app;
-    var server = require('http').Server(app);
-    this.server = server;
+        opts = opts || {};
+        var port = opts.port || 8000;
 
-    if (opts.router) {
-        app.use(opts.router);
-    }
+        var log = module.exports.log = function (msg) {
+            if (!opts.noLogOutput) {
+                if (opts.events) {
+                    opts.events.emit('log', msg);
+                }
+                else {
+                    console.log(msg);
+                }
+            }
+        };
 
-    if (opts.root) {
-        this.root = opts.root;
-        app.use(express.static(opts.root));
-    }
+        var app = that.app;
+        var server = require('http').Server(app);
+        that.server = server;
 
-    // If we have a project root, make that available as a static root also. This can be useful in cases where source
-    // files that have been transpiled (such as TypeScript) are located under the project root on a path that mirrors
-    // the the transpiled file's path under the platform root and is pointed to by a map file.
-    if (this.projectRoot) {
-        app.use(express.static(this.projectRoot));
-    }
+        if (opts.router) {
+            app.use(opts.router);
+        }
 
-    var that = this;
-    server.listen(port).on('listening', function () {
-        that.port = port;
-        if (!opts.noServerInfo) {
-            log('Static file server running on: ' + chalk.green('http://localhost:' + port) + ' (CTRL + C to shut down)');
+        if (opts.root) {
+            that.root = opts.root;
+            app.use(express.static(opts.root));
         }
-        deferred.resolve();
-    }).on('error', function (e) {
-        if (e && e.toString().indexOf('EADDRINUSE') !== -1) {
-            port++;
-            server.listen(port);
-        } else {
-            deferred.reject(e);
+
+        // If we have a project root, make that available as a static root also. This can be useful in cases where source
+        // files that have been transpiled (such as TypeScript) are located under the project root on a path that mirrors
+        // the the transpiled file's path under the platform root and is pointed to by a map file.
+        console.log("pr " + that.projectRoot)
+        if (that.projectRoot) {
+            app.use(express.static(that.projectRoot));
         }
-    });
 
-    return deferred.promise;
+        var listener = server.listen(port)
+        listener.on('listening', function () {
+            that.port = port;
+            var message = 'Static file server running on: ' + chalk.green('http://localhost:' + port) + ' (CTRL + C to shut down)';
+            if (!opts.noServerInfo) {
+                log(message);
+            }
+            resolve(message);
+        });
+        listener.on('error', function (e) {
+            if (e && e.toString().indexOf('EADDRINUSE') > -1) {
+                port++;
+                server.listen(port);
+            }
+            else {
+                reject(e);
+            }
+        });
+    });
+    return promise;
 };


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