cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject [09/24] cordova-serve git commit: Clean up cordova-serve console output.
Date Tue, 06 Jun 2017 05:43:15 GMT
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-serve/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-serve/commit/758a91a8
Tree: http://git-wip-us.apache.org/repos/asf/cordova-serve/tree/758a91a8
Diff: http://git-wip-us.apache.org/repos/asf/cordova-serve/diff/758a91a8

Branch: refs/heads/master
Commit: 758a91a8b86957c2db0507233201d3fe69133c60
Parents: b2da558
Author: Tim Barham <tim.barham@microsoft.com>
Authored: Sat Aug 22 21:18:34 2015 +1000
Committer: Tim Barham <tim.barham@microsoft.com>
Committed: Sat Aug 22 22:55:55 2015 +1000

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


http://git-wip-us.apache.org/repos/asf/cordova-serve/blob/758a91a8/package.json
----------------------------------------------------------------------
diff --git a/package.json b/package.json
index e075636..9072bdf 100644
--- a/package.json
+++ b/package.json
@@ -19,6 +19,7 @@
     "email": "dev@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-serve/blob/758a91a8/src/browser.js
----------------------------------------------------------------------
diff --git a/src/browser.js b/src/browser.js
index 2eb78df..c701ca0 100644
--- a/src/browser.js
+++ b/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-serve/blob/758a91a8/src/server.js
----------------------------------------------------------------------
diff --git a/src/server.js b/src/server.js
index eafacc1..4446cd5 100644
--- a/src/server.js
+++ b/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-serve/blob/758a91a8/src/stream.js
----------------------------------------------------------------------
diff --git a/src/stream.js b/src/stream.js
index 61a361b..dd6ebaf 100644
--- a/src/stream.js
+++ b/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-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message