-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (59 loc) · 2.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const spawn = require('child_process').spawn;
const validateOptions = require('schema-utils');
const schema = require('./schema.json');
class HTMLValidatePlugin {
constructor(options = {}) {
// validate options being passed through the plugin
validateOptions(schema, options, 'HTMLValidatePlugin');
Object.assign(
this,
{
// default values
path: 'src/**/*',
extensions: 'html',
config: '.htmlvalidate',
},
({
// destructure params
path: this.path,
extensions: this.extensions,
config: this.config,
// user provided params override defaults
} = options)
);
}
getExtensions() {
return this.extensions === 'html' ? this.extensions : this.convertExtensionArrayToRegex();
}
getConfig() {
return '--config ' + this.config + '.json';
}
convertExtensionArrayToRegex() {
// replace array as curly braced string for replacing commas and spaces
let processedExtension = `"{${this.extensions}}"`.replace(/\"/g, '').replace(/\ /g, '');
// strip out curly braces if there was only one index provided in extensions array
return processedExtension.includes(',')
? processedExtension
: processedExtension.replace(/\{/g, '').replace(/\}/g, '');
}
runCli(userParams, spawnParams) {
/*
Attempts at better security:
- schema utils used to validate user input
- spawn command (by default) is not exec under a shell env
https://gist.github.com/evilpacket/5a9655c752982faf7c4ec6450c1cbf1b
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
*/
return spawn('html-validate', [`${userParams}`], spawnParams);
}
apply(compiler) {
// initiate script when webpack compilation is completed
compiler.hooks.done.tap('HTMLValidatePlugin', () => {
// set up cli payload
const userParams = `${this.path}.${this.getExtensions()} ${this.getConfig()}`;
const spawnParams = { shell: true, stdio: 'inherit' };
this.runCli(userParams, spawnParams);
});
}
}
module.exports = HTMLValidatePlugin;