Skip to content

Commit 9ab8247

Browse files
committed
fix no params bug, set param defaults
1 parent 625a880 commit 9ab8247

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

index.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
1-
const spawn = require("child_process").spawn;
1+
const spawn = require('child_process').spawn;
22

33
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+
);
1122
}
1223

1324
convertExtensionArrayToRegex() {
1425
// 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, '');
1827
// strip out curly braces if there was only one index provided in extensions array
19-
return processedExtension.includes(",")
28+
return processedExtension.includes(',')
2029
? processedExtension
21-
: processedExtension.replace(/\{/g, "").replace(/\}/g, "");
30+
: processedExtension.replace(/\{/g, '').replace(/\}/g, '');
2231
}
2332

2433
runCliBasedOnScope(userParams, spawnParams) {
2534
/*
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
2736
https://stackoverflow.com/a/50424976d
2837
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
2938
*/
3039
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);
3742
}
3843

3944
apply(compiler) {
4045
// 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'}`;
4950

5051
// set up cli payload
5152
const userParams = `${path}.${extension} ${config}`;
5253
const spawnParams = {
5354
shell: true,
54-
/*inherit color output */ stdio: "inherit",
55+
/*inherit color output */ stdio: 'inherit',
5556
};
5657

5758
this.runCliBasedOnScope(userParams, spawnParams);

0 commit comments

Comments
 (0)