@@ -7,7 +7,10 @@ var Crawler = require("simplecrawler");
77var program = require ( "commander" ) ;
88var chalk = require ( "chalk" ) ;
99var exec = require ( "child_process" ) . exec ;
10- var _ = require ( "lodash" ) ;
10+ var _ = {
11+ isEmpty : require ( "lodash/lang/isEmpty" ) ,
12+ forEach : require ( "lodash/collection/forEach" )
13+ } ;
1114var Spinner = require ( "cli-spinner" ) . Spinner ;
1215var findJavaHome = require ( "find-java-home" ) ;
1316var 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} ;
0 commit comments