Skip to content

Commit 3cf159a

Browse files
committed
Fixed log file generation
1 parent f8fc529 commit 3cf159a

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

lib/W3CValidator.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ var Crawler = require("simplecrawler");
77
var program = require("commander");
88
var chalk = require("chalk");
99
var exec = require("child_process").exec;
10-
var _ = require("lodash");
10+
var _ = {
11+
isEmpty: require("lodash/lang/isEmpty"),
12+
forEach: require("lodash/collection/forEach")
13+
};
1114
var Spinner = require("cli-spinner").Spinner;
1215
var findJavaHome = require("find-java-home");
1316
var URL = require("url-parse");
@@ -31,6 +34,7 @@ var W3CValidator = function(url, java_home) {
3134
this.valid = 0;
3235
this.invalid = 0;
3336
this.JAVA_HOME = java_home;
37+
this.logData = "";
3438

3539
this.uri = new URL(url);
3640
this.crawler = new Crawler(this.uri.host);
@@ -94,21 +98,30 @@ W3CValidator.prototype.checkURL = function() {
9498
});
9599
}
96100

97-
if (program.log) {
98-
var data = "";
99-
data += `${url}\n`;
100-
_.forEach(errors.messages, function(m) {
101-
data += ` Line ${m.lastLine}: ${m.message}\n`;
101+
if (program.log && errors.messages) {
102+
this.logData += `\n${url}\n`;
103+
_.forEach(errors.messages, (m) => {
104+
this.logData += ` Line ${m.lastLine}: ${m.message}\n`;
102105
});
103-
fs.writeFile(path.join("./", `${this.logFileName}.txt`), data);
104106
}
105107
}
106108

107109
if (!_.isEmpty(this.chunk)) {
108110
this.checkURL();
109111
} else {
110-
console.log(chalk.white("Checked %s sites. %s valid, %s invalid."), this.count, this.valid, this.invalid);
111-
process.exit();
112+
var finish = function() {
113+
console.log(chalk.white("Checked %s sites. %s valid, %s invalid."), this.count, this.valid, this.invalid);
114+
process.exit();
115+
};
116+
117+
if (program.log) {
118+
fs.writeFile(path.join("./", `${this.logFileName}.txt`), this.logData, () => {
119+
console.log("Wrote logged error data to \"%s.txt\".", this.logFileName);
120+
finish.call(this);
121+
});
122+
} else {
123+
finish.call(this);
124+
}
112125
}
113126
});
114127
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "w3c-validator",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Crawls a given site and checks for W3C validity.",
55
"homepage": "https://github.com/lgraubner/node-w3c-validator",
66
"author": {

0 commit comments

Comments
 (0)