re-arrange code in src/, use Promise, default target gets open'd
diff --git a/package.json b/package.json
index 26671d1..464fe36 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
"name": "cordova-serve",
"version": "1.0.2-dev",
"description": "Apache Cordova server support for cordova-lib and cordova-browser.",
- "main": "serve.js",
+ "main": "src/main.js",
"repository": {
"type": "git",
"url": "https://github.com/apache/cordova-lib"
@@ -22,6 +22,7 @@
"chalk": "^1.1.1",
"compression": "^1.6.0",
"express": "^4.13.3",
+ "open": "0.0.5",
"q": "^1.4.1",
"shelljs": "^0.5.3"
},
diff --git a/src/browser.js b/src/browser.js
index 2b08afb..b27ab8b 100644
--- a/src/browser.js
+++ b/src/browser.js
@@ -18,9 +18,10 @@
*/
var child_process = require('child_process'),
- exec = require('./exec'),
fs = require('fs'),
- Q = require('q');
+ Q = require('q'),
+ open = require('open'),
+ exec = require('./exec');
var NOT_INSTALLED = 'The browser target is not installed: %target%';
var NOT_SUPPORTED = 'The browser target is not supported: %target%';
@@ -35,54 +36,64 @@
* @return {Q} Promise to launch the specified browser
*/
module.exports = function (opts) {
+
var target = opts.target || 'chrome';
var url = opts.url || '';
target = target.toLowerCase();
- return getBrowser(target, opts.dataDir).then(function (browser) {
- var args;
+ if(target === 'default') {
+ return open(url);
+ }
+ else {
- var urlAdded = false;
- switch (process.platform) {
- case 'darwin':
- args = ['open'];
- if (target == 'chrome') {
- // Chrome needs to be launched in a new window. Other browsers, particularly, opera does not work with this.
- args.push('-n');
- }
- args.push('-a', browser);
- break;
- case 'win32':
- // On Windows, we really want to use the "start" command. But, the rules regarding arguments with spaces, and
- // escaping them with quotes, can get really arcane. So the easiest way to deal with this is to pass off the
- // responsibility to "cmd /c", which has that logic built in.
- //
- // Furthermore, if "cmd /c" double-quoted the first parameter, then "start" will interpret it as a window title,
- // so we need to add a dummy empty-string window title: http://stackoverflow.com/a/154090/3191
+ return getBrowser(target, opts.dataDir).then(function (browser) {
+ var args;
- if (target === 'edge') {
- browser += ':' + url;
- urlAdded = true;
+ var urlAdded = false;
+
+
+ switch (process.platform) {
+ case 'darwin':
+ args = ['open'];
+ if (target == 'chrome') {
+ // Chrome needs to be launched in a new window. Other browsers, particularly, opera does not work with this.
+ args.push('-n');
+ }
+ args.push('-a', browser);
+ break;
+ case 'win32':
+ // On Windows, we really want to use the "start" command. But, the rules regarding arguments with spaces, and
+ // escaping them with quotes, can get really arcane. So the easiest way to deal with this is to pass off the
+ // responsibility to "cmd /c", which has that logic built in.
+ //
+ // Furthermore, if "cmd /c" double-quoted the first parameter, then "start" will interpret it as a window title,
+ // so we need to add a dummy empty-string window title: http://stackoverflow.com/a/154090/3191
+
+ if (target === 'edge') {
+ browser += ':' + url;
+ urlAdded = true;
+ }
+
+ args = ['cmd /c start ""', browser];
+ break;
+ case 'linux':
+ // if a browser is specified, launch it with the url as argument
+ // otherwise, use xdg-open.
+ args = [browser];
+ break;
}
- args = ['cmd /c start ""', browser];
- break;
- case 'linux':
- // if a browser is specified, launch it with the url as argument
- // otherwise, use xdg-open.
- args = [browser];
- break;
- }
+ if (!urlAdded) {
+ args.push(url);
+ }
+ var command = args.join(' ');
- if (!urlAdded) {
- args.push(url);
- }
- var command = args.join(' ');
- return exec(command).catch(function (error) {
- // Assume any error means that the browser is not installed and display that as a more friendly error.
- throw new Error(NOT_INSTALLED.replace('%target%', target));
- });
- });
+ return exec(command).catch(function (error) {
+ // Assume any error means that the browser is not installed and display that as a more friendly error.
+ throw new Error(NOT_INSTALLED.replace('%target%', target));
+ });
+ });
+ }
};
function getBrowser(target, dataDir) {
diff --git a/src/exec.js b/src/exec.js
index d1c02a4..d3f2e44 100644
--- a/src/exec.js
+++ b/src/exec.js
@@ -17,30 +17,31 @@
under the License.
*/
-var child_process = require('child_process'),
- Q = require('q');
+var child_process = require('child_process');
/**
* Executes the command specified.
* @param {string} cmd Command to execute
* @param {[string]} opt_cwd Current working directory
- * @return {Q} promise a promise that either resolves with the stdout, or rejects with an error message and the stderr.
+ * @return {Promise} a promise that either resolves with the stdout, or rejects with an error message and the stderr.
*/
module.exports = function (cmd, opt_cwd) {
- var d = Q.defer();
- try {
- child_process.exec(cmd, {cwd: opt_cwd, maxBuffer: 1024000}, function (err, stdout, stderr) {
- if (err) {
- d.reject(new Error('Error executing "' + cmd + '": ' + stderr));
- }
- else {
- d.resolve(stdout);
- }
- });
- } catch (e) {
- console.error('error caught: ' + e);
- d.reject(e);
- }
- return d.promise;
+ return new Promise(function(resolve,reject){
+ try {
+ var opt = {cwd: opt_cwd, maxBuffer: 1024000};
+ child_process.exec(cmd,opt,function (err, stdout, stderr) {
+ if (err) {
+ reject(new Error('Error executing "' + cmd + '": ' + stderr));
+ }
+ else {
+ resolve(stdout);
+ }
+ });
+ }
+ catch (e) {
+ console.error('error caught: ' + e);
+ reject(e);
+ }
+ });
};
diff --git a/serve.js b/src/main.js
similarity index 85%
rename from serve.js
rename to src/main.js
index 10d000a..01c5add 100644
--- a/serve.js
+++ b/src/main.js
@@ -19,8 +19,7 @@
var chalk = require('chalk'),
compression = require('compression'),
- express = require('express'),
- server = require('./src/server');
+ express = require('express');
module.exports = function () {
return new CordovaServe();
@@ -38,7 +37,7 @@
if (encoding) {
msg += chalk.gray(' (' + encoding + ')');
}
- server.log(msg);
+ require('./server').log(msg);
});
next();
});
@@ -46,11 +45,12 @@
// Turn on compression
this.app.use(compression());
- this.servePlatform = require('./src/platform');
- this.launchServer = server;
+ this.servePlatform = require('./platform');
+ this.launchServer = require('./server');
+ this.launchBrowser = require('./browser');
}
-module.exports.launchBrowser = require('./src/browser');
+// module.exports.launchBrowser = require('./browser');
// Expose some useful express statics
module.exports.Router = express.Router;