Skip to content

Commit 63ac805

Browse files
alindesignshellscape
authored andcommitted
Add report time feature (#195)
* Add report time feature * ReportTime feature changes * remove whitespaces * remove whitespaces
1 parent 712040d commit 63ac805

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

lib/Shared.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ var mime = require("mime");
22
var parseRange = require("range-parser");
33
var pathIsAbsolute = require("path-is-absolute");
44
var MemoryFileSystem = require("memory-fs");
5+
var timestamp = require("time-stamp");
6+
57
var HASH_REGEXP = /[0-9a-f]{10,}/;
68

79
module.exports = function Shared(context) {
810
var share = {
911
setOptions: function(options) {
1012
if(!options) options = {};
13+
if(typeof options.reportTime === "undefined") options.reportTime = false;
1114
if(typeof options.watchOptions === "undefined") options.watchOptions = {};
1215
if(typeof options.reporter !== "function") options.reporter = share.defaultReporter;
1316
if(typeof options.log !== "function") options.log = console.log.bind(console);
@@ -35,10 +38,14 @@ module.exports = function Shared(context) {
3538
context.options = options;
3639
},
3740
defaultReporter: function(reporterOptions) {
41+
var time = "";
3842
var state = reporterOptions.state;
3943
var stats = reporterOptions.stats;
4044
var options = reporterOptions.options;
4145

46+
if(!!options.reportTime) {
47+
time = "[" + timestamp("HH:mm:ss") + "] ";
48+
}
4249
if(state) {
4350
var displayStats = (!options.quiet && options.stats !== false);
4451
if(displayStats && !(stats.hasErrors() || stats.hasWarnings()) &&
@@ -59,11 +66,11 @@ module.exports = function Shared(context) {
5966
msg = "Failed to compile.";
6067
} else if(stats.hasWarnings()) {
6168
msg = "Compiled with warnings.";
62-
}
63-
options.log("webpack: " + msg);
69+
}
70+
options.log(time + "webpack: " + msg);
6471
}
6572
} else {
66-
options.log("webpack: Compiling...");
73+
options.log(time + "webpack: Compiling...");
6774
}
6875
},
6976
handleRangeHeaders: function handleRangeHeaders(content, req, res) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"memory-fs": "~0.4.1",
1111
"mime": "^1.3.4",
1212
"path-is-absolute": "^1.0.0",
13-
"range-parser": "^1.0.3"
13+
"range-parser": "^1.0.3",
14+
"time-stamp": "^2.0.0"
1415
},
1516
"devDependencies": {
1617
"codecov.io": "^0.1.6",

test/Reporter.test.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var middleware = require("../middleware");
22
var should = require("should");
33
var fs = require("fs");
44
var path = require("path");
5+
var timestamp = require("time-stamp");
56
require("mocha-sinon");
67

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

5960
plugins.done(simpleStats);
6061
setTimeout(function() {
62+
6163
should.strictEqual(console.log.callCount, 2);
6264
should.strictEqual(console.warn.callCount, 0);
6365
should.strictEqual(console.error.callCount, 0);
@@ -79,7 +81,33 @@ describe("Reporter", function() {
7981
});
8082
});
8183

82-
it("should show 'Compiled with warnings' message in console.warn", function(done) {
84+
it("should show compiled successfully message, with log time", function(done) {
85+
middleware(compiler, {
86+
reportTime: true
87+
});
88+
89+
plugins.done(simpleStats);
90+
setTimeout(function() {
91+
92+
should.strictEqual(console.log.callCount, 2);
93+
should.strictEqual(console.log.calledWith("[" + timestamp("HH:mm:ss") + "] webpack: Compiled successfully."), true);
94+
done();
95+
});
96+
});
97+
98+
it("should show compiled successfully message, with log time", function(done) {
99+
middleware(compiler, {
100+
reportTime: true
101+
});
102+
103+
plugins.done(errorStats);
104+
setTimeout(function() {
105+
should.strictEqual(console.log.calledWith("[" + timestamp("HH:mm:ss") + "] webpack: Failed to compile."), true);
106+
done();
107+
});
108+
});
109+
110+
it("should show compiled with warnings message", function(done) {
83111
middleware(compiler);
84112

85113
plugins.done(warningStats);
@@ -92,6 +120,18 @@ describe("Reporter", function() {
92120
});
93121
});
94122

123+
it("should show compiled with warnings message, with log time", function(done) {
124+
middleware(compiler, {
125+
reportTime: true
126+
});
127+
128+
plugins.done(warningStats);
129+
setTimeout(function() {
130+
should.strictEqual(console.log.calledWith("[" + timestamp("HH:mm:ss") + "] webpack: Compiled with warnings."), true);
131+
done();
132+
});
133+
});
134+
95135
it("should not show valid message if options.quiet is given", function(done) {
96136
middleware(compiler, { quiet: true });
97137

@@ -123,6 +163,19 @@ describe("Reporter", function() {
123163
});
124164
});
125165

166+
it("should show invalid message, with log time", function(done) {
167+
middleware(compiler, {
168+
reportTime: true
169+
});
170+
plugins.done(simpleStats);
171+
plugins.invalid();
172+
setTimeout(function() {
173+
should.strictEqual(console.log.callCount, 1);
174+
should.strictEqual(console.log.calledWith("[" + timestamp("HH:mm:ss") + "] webpack: Compiling..."), true);
175+
done();
176+
});
177+
});
178+
126179
it("should not show invalid message if options.noInfo is given", function(done) {
127180
middleware(compiler, { noInfo: true });
128181

0 commit comments

Comments
 (0)