Repository: cordova-serve Updated Branches: refs/heads/master 1119b5eb9 -> 338a86baf (forced update)
re-arrange code in src/, use Promise, default target gets open'd Project: http://git-wip-us.apache.org/repos/asf/cordova-serve/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-serve/commit/ef88237f Tree: http://git-wip-us.apache.org/repos/asf/cordova-serve/tree/ef88237f Diff: http://git-wip-us.apache.org/repos/asf/cordova-serve/diff/ef88237f Branch: refs/heads/master Commit: ef88237fda44dffd25ba9ae2953253b6af038553 Parents: 0c2fbd9 Author: Jesse MacFadyen <[email protected]> Authored: Thu Jun 29 10:44:35 2017 -0700 Committer: Jesse MacFadyen <[email protected]> Committed: Thu Jun 29 10:44:35 2017 -0700 ---------------------------------------------------------------------- package.json | 3 +- serve.js | 57 ------------------------------- src/browser.js | 97 +++++++++++++++++++++++++++++------------------------ src/exec.js | 37 ++++++++++---------- src/main.js | 57 +++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 119 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/ef88237f/package.json ---------------------------------------------------------------------- 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" }, http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/ef88237f/serve.js ---------------------------------------------------------------------- diff --git a/serve.js b/serve.js deleted file mode 100644 index 10d000a..0000000 --- a/serve.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - 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 chalk = require('chalk'), - compression = require('compression'), - express = require('express'), - server = require('./src/server'); - -module.exports = function () { - return new CordovaServe(); -}; - -function CordovaServe() { - this.app = express(); - - // Attach this before anything else to provide status output - this.app.use(function (req, res, next) { - res.on('finish', function () { - var color = this.statusCode == '404' ? chalk.red : chalk.green; - var msg = color(this.statusCode) + ' ' + this.req.originalUrl; - var encoding = this._headers && this._headers['content-encoding']; - if (encoding) { - msg += chalk.gray(' (' + encoding + ')'); - } - server.log(msg); - }); - next(); - }); - - // Turn on compression - this.app.use(compression()); - - this.servePlatform = require('./src/platform'); - this.launchServer = server; -} - -module.exports.launchBrowser = require('./src/browser'); - -// Expose some useful express statics -module.exports.Router = express.Router; -module.exports.static = express.static; http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/ef88237f/src/browser.js ---------------------------------------------------------------------- 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 @@ var NOT_SUPPORTED = 'The browser target is not supported: %target%'; * @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; - - 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; + if(target === 'default') { + return open(url); + } + else { + + return getBrowser(target, opts.dataDir).then(function (browser) { + var args; + + 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) { http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/ef88237f/src/exec.js ---------------------------------------------------------------------- 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); + } + }); }; http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/ef88237f/src/main.js ---------------------------------------------------------------------- diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..01c5add --- /dev/null +++ b/src/main.js @@ -0,0 +1,57 @@ +/** + 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 chalk = require('chalk'), + compression = require('compression'), + express = require('express'); + +module.exports = function () { + return new CordovaServe(); +}; + +function CordovaServe() { + this.app = express(); + + // Attach this before anything else to provide status output + this.app.use(function (req, res, next) { + res.on('finish', function () { + var color = this.statusCode == '404' ? chalk.red : chalk.green; + var msg = color(this.statusCode) + ' ' + this.req.originalUrl; + var encoding = this._headers && this._headers['content-encoding']; + if (encoding) { + msg += chalk.gray(' (' + encoding + ')'); + } + require('./server').log(msg); + }); + next(); + }); + + // Turn on compression + this.app.use(compression()); + + this.servePlatform = require('./platform'); + this.launchServer = require('./server'); + this.launchBrowser = require('./browser'); +} + +// module.exports.launchBrowser = require('./browser'); + +// Expose some useful express statics +module.exports.Router = express.Router; +module.exports.static = express.static; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
