@@ -4,125 +4,32 @@ const os = require('os');
44const path = require ( 'path' ) ;
55const { execFile } = require ( 'child_process' ) ;
66const async = require ( 'async' ) ;
7- const jar = require ( 'vnu-jar' ) ;
87const chunkify = require ( './chunkify.js' ) ;
9- const javadetect = require ( './javadetect.js' ) ;
8+ const javaDetect = require ( './javaDetect.js' ) ;
9+ const javaArgs = require ( './javaArgs.js' ) ;
10+ const parseErrorMessages = require ( './parseErrorMessages.js' ) ;
11+ const processErrorMessages = require ( './processErrorMessages.js' ) ;
1012
1113// chunkify's max characters
1214const MAX_CHARS = 5000 ;
1315
14- // Increase child process buffer to accommodate large amounts of
15- // validation output. (the default is a paltry 200k)
16- // https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
16+ /**
17+ * Increase child process buffer to accommodate large amounts of validation output.
18+ * The default is a paltry 200k:
19+ * https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
20+ */
1721const MAX_BUFFER = 20_000 * 1024 ;
1822
1923// eslint-disable-next-line unicorn/explicit-length-check
2024const CPUS = os . cpus ( ) && os . cpus ( ) . length ;
2125const THREADS = CPUS > 2 ? CPUS - 1 : 1 ;
2226
23- // Replace left/right quotation marks with normal quotation marks
24- function normalizeQuotationMarks ( string ) {
25- if ( string ) {
26- string = string . replace ( / [ \u201C \u201D ] / g, '"' ) ;
27- }
28-
29- return string ;
30- }
31-
32- // Parse and, if needed, normalize error messages from HttpClient to java -jar format
33- // java -jar: one object containing messages for all files
34- // { messages: [{ message, type, url, ... }, ...] }
35- // HttpClient: one object per file, separated by a newline,
36- // each object containing messages for only that file
37- // { messages: [{ message, type, ...}, ...], url }\n{ ... }
38- function parseErrorMessages ( errors , config ) {
39- const parsed = JSON . parse ( config . server ? `[${ errors . trim ( ) . replace ( / \n / g, ',' ) } ]` : errors ) ;
40- let { messages } = parsed ;
41-
42- if ( config . server ) {
43- // Extract "messages" property from each object and set the url of each message.
44- // This results in an array of arrays instead of array of objects, which is then
45- // flattened
46- messages = parsed . flatMap ( file => {
47- return file . messages . map ( message => {
48- message . url = file . url ;
49- return message ;
50- } ) ;
51- } ) ;
52- }
53-
54- return messages ;
55- }
56-
57- function processErrorMessages ( errors , config ) {
58- const result = errors . map ( message => {
59- if ( message . url ) {
60- message . file = path . relative ( '.' , message . url . replace ( path . sep === '\\' ? 'file:/' : 'file:' , '' ) ) ;
61- }
62-
63- if ( config . absoluteFilePathsForReporter ) {
64- message . file = path . resolve ( message . file ) ;
65- }
66-
67- return message ;
68- } ) ;
69-
70- if ( ! config . ignore ) {
71- return result ;
72- }
73-
74- const ignore = Array . isArray ( config . ignore ) ? config . ignore : [ config . ignore ] ;
75-
76- return result . filter ( ( { message } ) => {
77- // Iterate over the ignore rules and test the message against each rule.
78- // A match should return false, which causes every() to return false and
79- // the message to be filtered out.
80- return ignore . every (
81- currentValue => currentValue instanceof RegExp ?
82- ! currentValue . test ( message ) :
83- normalizeQuotationMarks ( currentValue ) !== normalizeQuotationMarks ( message )
84- ) ;
85- } ) ;
86- }
87-
88- // Determine proper jarfile arguments
89- function javaArgs ( java , chunk , config ) {
90- const args = [ ] ;
91-
92- // Increase the default stack size for ia32 versions
93- if ( java . arch === 'ia32' ) {
94- args . push ( '-Xss512k' ) ;
95- }
96-
97- args . push ( config . server ? '-cp' : '-jar' , `"${ jar } "` ) ;
98-
99- if ( config . server ) {
100- if ( config . server . host ) {
101- args . push ( `-Dnu.validator.client.host=${ config . server . host } ` ) ;
102- }
103-
104- if ( config . server . port ) {
105- args . push ( `-Dnu.validator.client.port=${ config . server . port } ` ) ;
106- }
107-
108- args . push ( '-Dnu.validator.client.out=json nu.validator.client.HttpClient' ) ;
109- } else {
110- args . push ( '--format' , 'json' ) ;
111- }
112-
113- if ( config . noLangDetect ) {
114- args . push ( '--no-langdetect' ) ;
115- }
116-
117- return [ ...args , chunk ] ;
118- }
119-
12027function htmllint ( config , done ) {
12128 if ( config . files . length === 0 ) {
12229 return done ( null , [ ] ) ;
12330 }
12431
125- javadetect ( ( err , java ) => {
32+ javaDetect ( ( err , java ) => {
12633 if ( err ) {
12734 throw err ;
12835 }
0 commit comments