@@ -7,7 +7,10 @@ var Crawler = require("simplecrawler");
7
7
var program = require ( "commander" ) ;
8
8
var chalk = require ( "chalk" ) ;
9
9
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
+ } ;
11
14
var Spinner = require ( "cli-spinner" ) . Spinner ;
12
15
var findJavaHome = require ( "find-java-home" ) ;
13
16
var URL = require ( "url-parse" ) ;
@@ -31,6 +34,7 @@ var W3CValidator = function(url, java_home) {
31
34
this . valid = 0 ;
32
35
this . invalid = 0 ;
33
36
this . JAVA_HOME = java_home ;
37
+ this . logData = "" ;
34
38
35
39
this . uri = new URL ( url ) ;
36
40
this . crawler = new Crawler ( this . uri . host ) ;
@@ -94,21 +98,30 @@ W3CValidator.prototype.checkURL = function() {
94
98
} ) ;
95
99
}
96
100
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` ;
102
105
} ) ;
103
- fs . writeFile ( path . join ( "./" , `${ this . logFileName } .txt` ) , data ) ;
104
106
}
105
107
}
106
108
107
109
if ( ! _ . isEmpty ( this . chunk ) ) {
108
110
this . checkURL ( ) ;
109
111
} 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
+ }
112
125
}
113
126
} ) ;
114
127
} ;
0 commit comments