remove 'q' dependence completely. Added server.spec
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": {
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
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 @@
* @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);
+
+ if (!fs.existsSync(opts.root)) {
+ reject('Error: Project does not include the specified platform: ' + platform);
+ }
+ else {
+ return that.launchServer(opts);
+ }
}
- 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);
- }
-
- return that.launchServer(opts);
});
+ return retPromise;
};
function findProjectRoot(path) {
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 @@
* @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);
+ opts = opts || {};
+ var port = opts.port || 8000;
+
+ var log = module.exports.log = function (msg) {
+ if (!opts.noLogOutput) {
+ if (opts.events) {
+ opts.events.emit('log', msg);
+ }
+ else {
+ console.log(msg);
+ }
}
+ };
+
+ var app = that.app;
+ var server = require('http').Server(app);
+ that.server = server;
+
+ if (opts.router) {
+ app.use(opts.router);
}
- };
- var app = this.app;
- var server = require('http').Server(app);
- this.server = server;
-
- if (opts.router) {
- app.use(opts.router);
- }
-
- if (opts.root) {
- this.root = opts.root;
- app.use(express.static(opts.root));
- }
-
- // 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));
- }
-
- 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));
}
+
+ 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 deferred.promise;
+ return promise;
};