|
1 | | -const spawn = require("child_process").spawn; |
| 1 | +const spawn = require('child_process').spawn; |
2 | 2 |
|
3 | 3 | class HTMLValidatePlugin { |
4 | | - constructor(options) { |
5 | | - ({ |
6 | | - path: this.path, |
7 | | - extensions: this.extensions, |
8 | | - config: this.config, |
9 | | - global: this.global, |
10 | | - } = options); |
| 4 | + constructor(options = {}) { |
| 5 | + Object.assign( |
| 6 | + this, |
| 7 | + { |
| 8 | + // default values |
| 9 | + path: 'src/**/*', |
| 10 | + extensions: 'html', |
| 11 | + config: '.htmlvalidate', |
| 12 | + global: false, |
| 13 | + }, |
| 14 | + // destructure params |
| 15 | + ({ |
| 16 | + path: this.path, |
| 17 | + extensions: this.extensions, |
| 18 | + config: this.config, |
| 19 | + global: this.global, |
| 20 | + } = options) |
| 21 | + ); |
11 | 22 | } |
12 | 23 |
|
13 | 24 | convertExtensionArrayToRegex() { |
14 | 25 | // replace array as curly braced string for replacing commas and spaces |
15 | | - let processedExtension = `"{${this.extensions}}"` |
16 | | - .replace(/\'/g, "") |
17 | | - .replace(/\ /g, ""); |
| 26 | + let processedExtension = `"{${this.extensions}}"`.replace(/\"/g, '').replace(/\ /g, ''); |
18 | 27 | // strip out curly braces if there was only one index provided in extensions array |
19 | | - return processedExtension.includes(",") |
| 28 | + return processedExtension.includes(',') |
20 | 29 | ? processedExtension |
21 | | - : processedExtension.replace(/\{/g, "").replace(/\}/g, ""); |
| 30 | + : processedExtension.replace(/\{/g, '').replace(/\}/g, ''); |
22 | 31 | } |
23 | 32 |
|
24 | 33 | runCliBasedOnScope(userParams, spawnParams) { |
25 | 34 | /* |
26 | | - arguments are in an array and shell option is 'false' by default; this is better for security |
| 35 | + arguments are in an array and shell option is "false" by default; this is better for security |
27 | 36 | https://stackoverflow.com/a/50424976d |
28 | 37 | https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options |
29 | 38 | */ |
30 | 39 | return this.global |
31 | | - ? spawn("html-validate", [`${userParams}`], spawnParams) |
32 | | - : spawn( |
33 | | - "node", |
34 | | - ["node_modules/.bin/html-validate", `${userParams}`], |
35 | | - spawnParams |
36 | | - ); |
| 40 | + ? spawn('html-validate', [`${userParams}`], spawnParams) |
| 41 | + : spawn('node', ['node_modules/.bin/html-validate', `${userParams}`], spawnParams); |
37 | 42 | } |
38 | 43 |
|
39 | 44 | apply(compiler) { |
40 | 45 | // initiate script when webpack compilation is completed |
41 | | - compiler.hooks.done.tap("HTMLValidatePlugin", () => { |
42 | | - const path = `${this.path || "src/**/*"}`; |
43 | | - const extension = `${ |
44 | | - this.extensions ? this.convertExtensionArrayToRegex() : "html" |
45 | | - }`; |
46 | | - const config = `${ |
47 | | - this.config ? "--config " + this.config + ".json" : false |
48 | | - }`; |
| 46 | + compiler.hooks.done.tap('HTMLValidatePlugin', () => { |
| 47 | + const path = `${this.path || 'src/**/*'}`; |
| 48 | + const extension = `${this.convertExtensionArrayToRegex()}`; |
| 49 | + const config = `${'--config ' + this.config + '.json'}`; |
49 | 50 |
|
50 | 51 | // set up cli payload |
51 | 52 | const userParams = `${path}.${extension} ${config}`; |
52 | 53 | const spawnParams = { |
53 | 54 | shell: true, |
54 | | - /*inherit color output */ stdio: "inherit", |
| 55 | + /*inherit color output */ stdio: 'inherit', |
55 | 56 | }; |
56 | 57 |
|
57 | 58 | this.runCliBasedOnScope(userParams, spawnParams); |
|
0 commit comments