Skip to content

Commit

Permalink
Add report time feature (webpack#195)
Browse files Browse the repository at this point in the history
* Add report time feature

* ReportTime feature changes

* remove whitespaces

* remove whitespaces
  • Loading branch information
alindesign authored and shellscape committed Jul 10, 2017
1 parent 712040d commit 63ac805
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/Shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ var mime = require("mime");
var parseRange = require("range-parser");
var pathIsAbsolute = require("path-is-absolute");
var MemoryFileSystem = require("memory-fs");
var timestamp = require("time-stamp");

var HASH_REGEXP = /[0-9a-f]{10,}/;

module.exports = function Shared(context) {
var share = {
setOptions: function(options) {
if(!options) options = {};
if(typeof options.reportTime === "undefined") options.reportTime = false;
if(typeof options.watchOptions === "undefined") options.watchOptions = {};
if(typeof options.reporter !== "function") options.reporter = share.defaultReporter;
if(typeof options.log !== "function") options.log = console.log.bind(console);
Expand Down Expand Up @@ -35,10 +38,14 @@ module.exports = function Shared(context) {
context.options = options;
},
defaultReporter: function(reporterOptions) {
var time = "";
var state = reporterOptions.state;
var stats = reporterOptions.stats;
var options = reporterOptions.options;

if(!!options.reportTime) {
time = "[" + timestamp("HH:mm:ss") + "] ";
}
if(state) {
var displayStats = (!options.quiet && options.stats !== false);
if(displayStats && !(stats.hasErrors() || stats.hasWarnings()) &&
Expand All @@ -59,11 +66,11 @@ module.exports = function Shared(context) {
msg = "Failed to compile.";
} else if(stats.hasWarnings()) {
msg = "Compiled with warnings.";
}
options.log("webpack: " + msg);
}
options.log(time + "webpack: " + msg);
}
} else {
options.log("webpack: Compiling...");
options.log(time + "webpack: Compiling...");
}
},
handleRangeHeaders: function handleRangeHeaders(content, req, res) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"memory-fs": "~0.4.1",
"mime": "^1.3.4",
"path-is-absolute": "^1.0.0",
"range-parser": "^1.0.3"
"range-parser": "^1.0.3",
"time-stamp": "^2.0.0"
},
"devDependencies": {
"codecov.io": "^0.1.6",
Expand Down
55 changes: 54 additions & 1 deletion test/Reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var middleware = require("../middleware");
var should = require("should");
var fs = require("fs");
var path = require("path");
var timestamp = require("time-stamp");
require("mocha-sinon");

var extendedStats = fs.readFileSync(path.join(__dirname, "fixtures", "stats.txt"), "utf8");
Expand Down Expand Up @@ -58,6 +59,7 @@ describe("Reporter", function() {

plugins.done(simpleStats);
setTimeout(function() {

should.strictEqual(console.log.callCount, 2);
should.strictEqual(console.warn.callCount, 0);
should.strictEqual(console.error.callCount, 0);
Expand All @@ -79,7 +81,33 @@ describe("Reporter", function() {
});
});

it("should show 'Compiled with warnings' message in console.warn", function(done) {
it("should show compiled successfully message, with log time", function(done) {
middleware(compiler, {
reportTime: true
});

plugins.done(simpleStats);
setTimeout(function() {

should.strictEqual(console.log.callCount, 2);
should.strictEqual(console.log.calledWith("[" + timestamp("HH:mm:ss") + "] webpack: Compiled successfully."), true);
done();
});
});

it("should show compiled successfully message, with log time", function(done) {
middleware(compiler, {
reportTime: true
});

plugins.done(errorStats);
setTimeout(function() {
should.strictEqual(console.log.calledWith("[" + timestamp("HH:mm:ss") + "] webpack: Failed to compile."), true);
done();
});
});

it("should show compiled with warnings message", function(done) {
middleware(compiler);

plugins.done(warningStats);
Expand All @@ -92,6 +120,18 @@ describe("Reporter", function() {
});
});

it("should show compiled with warnings message, with log time", function(done) {
middleware(compiler, {
reportTime: true
});

plugins.done(warningStats);
setTimeout(function() {
should.strictEqual(console.log.calledWith("[" + timestamp("HH:mm:ss") + "] webpack: Compiled with warnings."), true);
done();
});
});

it("should not show valid message if options.quiet is given", function(done) {
middleware(compiler, { quiet: true });

Expand Down Expand Up @@ -123,6 +163,19 @@ describe("Reporter", function() {
});
});

it("should show invalid message, with log time", function(done) {
middleware(compiler, {
reportTime: true
});
plugins.done(simpleStats);
plugins.invalid();
setTimeout(function() {
should.strictEqual(console.log.callCount, 1);
should.strictEqual(console.log.calledWith("[" + timestamp("HH:mm:ss") + "] webpack: Compiling..."), true);
done();
});
});

it("should not show invalid message if options.noInfo is given", function(done) {
middleware(compiler, { noInfo: true });

Expand Down

0 comments on commit 63ac805

Please sign in to comment.