Repository: cordova-lib
Updated Branches:
  refs/heads/master 4a10cc1c3 -> 448daed61


Clean up cordova-serve console output.

* Simplifies cordova-serve console output and adds some color.
* Also adds ability for caller to turn off all console messages, and 
specificall the "Static file server running on..." message.


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

Branch: refs/heads/master
Commit: 448daed614729d13a8c9b192c326ddb46a75cbd0
Parents: 4a10cc1
Author: Tim Barham <tim.bar...@microsoft.com>
Authored: Sat Aug 22 21:18:34 2015 +1000
Committer: Tim Barham <tim.bar...@microsoft.com>
Committed: Sat Aug 22 22:55:55 2015 +1000

----------------------------------------------------------------------
 cordova-serve/package.json   |  1 +
 cordova-serve/src/browser.js |  1 -
 cordova-serve/src/server.js  | 23 ++++++++++++++++-------
 cordova-serve/src/stream.js  | 13 ++++++++-----
 4 files changed, 25 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/448daed6/cordova-serve/package.json
----------------------------------------------------------------------
diff --git a/cordova-serve/package.json b/cordova-serve/package.json
index e075636..9072bdf 100644
--- a/cordova-serve/package.json
+++ b/cordova-serve/package.json
@@ -19,6 +19,7 @@
     "email": "d...@cordova.apache.org"
   },
   "dependencies": {
+    "chalk": "^1.1.1",
     "combined-stream": "^1.0.3",
     "d8": "^0.4.4",
     "mime": "^1.2.11",

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/448daed6/cordova-serve/src/browser.js
----------------------------------------------------------------------
diff --git a/cordova-serve/src/browser.js b/cordova-serve/src/browser.js
index 2eb78df..c701ca0 100644
--- a/cordova-serve/src/browser.js
+++ b/cordova-serve/src/browser.js
@@ -72,7 +72,6 @@ module.exports = function (opts) {
             args.push(url);
         }
         var command = args.join(' ');
-        console.log('Executing command: ' + command);
         return exec(command);
     });
 };

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/448daed6/cordova-serve/src/server.js
----------------------------------------------------------------------
diff --git a/cordova-serve/src/server.js b/cordova-serve/src/server.js
index eafacc1..4446cd5 100644
--- a/cordova-serve/src/server.js
+++ b/cordova-serve/src/server.js
@@ -17,7 +17,8 @@
  under the License.
  */
 
-var fs     = require('fs'),
+var chalk  = require('chalk'),
+    fs     = require('fs'),
     http   = require('http'),
     url    = require('url'),
     path   = require('path'),
@@ -26,7 +27,7 @@ var fs     = require('fs'),
 
 /**
  * @desc Launches a server with the specified options and optional custom 
handlers.
- * @param {{root: ?string, port: ?number, urlPathHandler: ?function, 
streamHandler: ?function, serverExtender: ?function}} opts
+ * @param {{root: ?string, port: ?number, noLogOutput: ?bool, noServerInfo: 
?bool, urlPathHandler: ?function, streamHandler: ?function, serverExtender: 
?function}} opts
  *     urlPathHandler(urlPath, request, response, do302, do404, serveFile) - 
an optional method to provide custom handling for
  *         processing URLs and serving up the resulting data. Can serve up the 
data itself using response.write(), or determine
  *         a custom local file path and call serveFile to serve it up, or do 
no processing and call serveFile with no params to
@@ -43,23 +44,29 @@ module.exports = function (opts) {
     var root = opts.root;
     var port = opts.port || 8000;
 
+    var log = module.exports.log = function () {
+        if (!opts.noLogOutput) {
+            console.log.apply(console, arguments);
+        }
+    };
+
     var server = http.createServer(function (request, response) {
         function do404() {
-            console.log('404 ' + request.url);
+            log(chalk.red('404 ') + request.url);
             response.writeHead(404, {'Content-Type': 'text/plain'});
             response.write('404 Not Found\n');
             response.end();
         }
 
         function do302(where) {
-            console.log('302 ' + request.url);
+            log(chalk.green('302 ') + request.url);
             response.setHeader('Location', where);
             response.writeHead(302, {'Content-Type': 'text/plain'});
             response.end();
         }
 
         function do304() {
-            console.log('304 ' + request.url);
+            log(chalk.green('304 ') + request.url);
             response.writeHead(304, {'Content-Type': 'text/plain'});
             response.end();
         }
@@ -102,7 +109,7 @@ module.exports = function (opts) {
                         do302(request.url + '/');
                         return;
                     }
-                    console.log('200 ' + request.url);
+                    log(chalk.green('200 ') + request.url);
                     response.writeHead(200, {'Content-Type': 'text/html'});
                     response.write('<html><head><title>Directory listing of ' 
+ urlPath + '</title></head>');
                     response.write('<h3>Items in this directory</h3>');
@@ -122,7 +129,9 @@ module.exports = function (opts) {
             });
         }
     }).on('listening', function () {
-        console.log('Static file server running on port ' + port + ' (i.e. 
http://localhost:' + port + ')\nCTRL + C to shut down');
+        if (!opts.noServerInfo) {
+            log('Static file server running on: ' + 
chalk.green('http://localhost:' + port) + ' (CTRL + C to shut down)');
+        }
         deferred.resolve({server: server, port: port});
     }).on('error', function (e) {
         if (e && e.toString().indexOf('EADDRINUSE') !== -1) {

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/448daed6/cordova-serve/src/stream.js
----------------------------------------------------------------------
diff --git a/cordova-serve/src/stream.js b/cordova-serve/src/stream.js
index 61a361b..dd6ebaf 100644
--- a/cordova-serve/src/stream.js
+++ b/cordova-serve/src/stream.js
@@ -17,9 +17,11 @@
  under the License.
  */
 
-var fs = require('fs'),
+var chalk = require('chalk'),
+    fs = require('fs'),
     mime = require('mime'),
-    zlib = require('zlib');
+    zlib = require('zlib'),
+    server = require('./server');
 
 // d8 is a date parsing and formatting micro-framework
 // Used only for RFC 2822 formatting
@@ -51,12 +53,13 @@ module.exports = function (filePath, request, response, 
readStream, noCache) {
     }
 
     var acceptEncoding = request.headers['accept-encoding'] || '';
+    var encoding = '';
     if (acceptEncoding.match(/\bgzip\b/)) {
-        console.log('gzip');
+        encoding ='(gzip)';
         respHeaders['content-encoding'] = 'gzip';
         readStream = readStream.pipe(zlib.createGzip());
     } else if (acceptEncoding.match(/\bdeflate\b/)) {
-        console.log('deflate');
+        encoding ='(deflate)';
         respHeaders['content-encoding'] = 'deflate';
         readStream = readStream.pipe(zlib.createDeflate());
     }
@@ -65,7 +68,7 @@ module.exports = function (filePath, request, response, 
readStream, noCache) {
     if (noCache) {
         respHeaders['Cache-Control'] = 'no-store';
     }
-    console.log('200 ' + request.url + ' (' + filePath + ')');
+    server.log(chalk.green('200 ') + request.url + ' ' + encoding);
     response.writeHead(200, respHeaders);
     readStream.pipe(response);
     return readStream;


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

Reply via email to