Commit cd458e3e authored by Anton's avatar Anton Committed by Anton
Browse files

feat: support .cjs configuration files

parent 6b1cec5b
......@@ -32,8 +32,11 @@ Run with:
Configuration can be added to:
- `.htmlvalidate.js`
- `.htmlvalidate.cjs`
- `.htmlvalidate.json`
ESM configuration files are not currently supported.
Configuration files will be searched from the target file and up until either no more parent folders exist or `"root": true` is found.
### `extends`
......@@ -153,11 +156,11 @@ Use a relative path to use a local script (use `<rootDir>` to refer to the path
By default, configuration is search in the file structure until the root directory (typically `/`) is found:
- `/home/user/project/src/.htmlvalidate.{js,json}`
- `/home/user/project/.htmlvalidate.{js,json}`
- `/home/user/.htmlvalidate.{js,json}`
- `/home/.htmlvalidate.{js,json}`
- `/.htmlvalidate.{js,json}`
- `/home/user/project/src/.htmlvalidate.{js,cjs,json}`
- `/home/user/project/.htmlvalidate.{js,cjs,json}`
- `/home/user/.htmlvalidate.{js,cjs,json}`
- `/home/.htmlvalidate.{js,cjs,json}`
- `/.htmlvalidate.{js,cjs,json}`
By setting the `root` property to `true` the search is stopped. This can be used
to prevent searching from outside the project directory or to use a specific
......@@ -173,8 +176,8 @@ For instance, if `/home/project/.htmlvalidate.json` contains:
Only the following files would be searched:
- `/home/user/project/src/.htmlvalidate.{js,json}`
- `/home/user/project/.htmlvalidate.{js,json}`
- `/home/user/project/src/.htmlvalidate.{js,cjs,json}`
- `/home/user/project/.htmlvalidate.{js,cjs,json}`
This also affects CLI `--config` and the API, e.g. when using `--config` with a
configuration using `"root": true` will prevent any additional files to be
......
......@@ -101,10 +101,13 @@ describe("ConfigLoader", () => {
mockFilenames: [
/* ConfigMock adds all visited filenames to this array */
path.resolve("/.htmlvalidate.js"),
path.resolve("/.htmlvalidate.cjs"),
path.resolve("/.htmlvalidate.json"),
path.resolve("/path/.htmlvalidate.js"),
path.resolve("/path/.htmlvalidate.cjs"),
path.resolve("/path/.htmlvalidate.json"),
path.resolve("/path/to/.htmlvalidate.js"),
path.resolve("/path/to/.htmlvalidate.cjs"),
path.resolve("/path/to/.htmlvalidate.json"),
],
})
......@@ -120,8 +123,10 @@ describe("ConfigLoader", () => {
mockFilenames: [
/* ConfigMock adds all visited filenames to this array */
path.resolve("/project/root/.htmlvalidate.js"),
path.resolve("/project/root/.htmlvalidate.cjs"),
path.resolve("/project/root/.htmlvalidate.json"),
path.resolve("/project/root/src/.htmlvalidate.js"),
path.resolve("/project/root/src/.htmlvalidate.cjs"),
path.resolve("/project/root/src/.htmlvalidate.json"),
],
})
......@@ -146,10 +151,13 @@ describe("ConfigLoader", () => {
mockFilenames: [
/* ConfigMock adds all visited filenames to this array */
path.resolve("/.htmlvalidate.js"),
path.resolve("/.htmlvalidate.cjs"),
path.resolve("/.htmlvalidate.json"),
path.resolve("/path/.htmlvalidate.js"),
path.resolve("/path/.htmlvalidate.cjs"),
path.resolve("/path/.htmlvalidate.json"),
path.resolve("/path/to/.htmlvalidate.js"),
path.resolve("/path/to/.htmlvalidate.cjs"),
path.resolve("/path/to/.htmlvalidate.json"),
],
})
......@@ -171,10 +179,13 @@ describe("ConfigLoader", () => {
mockFilenames: [
/* ConfigMock adds all visited filenames to this array */
path.resolve("/.htmlvalidate.js"),
path.resolve("/.htmlvalidate.cjs"),
path.resolve("/.htmlvalidate.json"),
path.resolve("/path/.htmlvalidate.js"),
path.resolve("/path/.htmlvalidate.cjs"),
path.resolve("/path/.htmlvalidate.json"),
path.resolve("/path/to/.htmlvalidate.js"),
path.resolve("/path/to/.htmlvalidate.cjs"),
path.resolve("/path/to/.htmlvalidate.json"),
],
})
......
......@@ -48,6 +48,7 @@ export class ConfigLoader {
*
* @param filename - Filename to get configuration for.
*/
// eslint-disable-next-line complexity, sonarjs/cognitive-complexity
public fromTarget(filename: string): Config | null {
if (filename === "inline") {
return null;
......@@ -63,18 +64,13 @@ export class ConfigLoader {
// eslint-disable-next-line no-constant-condition
while (true) {
const jsonFile = path.join(current, ".htmlvalidate.json");
if (fs.existsSync(jsonFile)) {
const local = this.configClass.fromFile(jsonFile);
found = true;
config = local.merge(config);
}
const jsFile = path.join(current, ".htmlvalidate.js");
if (fs.existsSync(jsFile)) {
const local = this.configClass.fromFile(jsFile);
found = true;
config = local.merge(config);
for (const potentialExtension of ["json", "cjs", "js"]) {
const filePath = path.join(current, `.htmlvalidate.${potentialExtension}`);
if (fs.existsSync(filePath)) {
const local = this.configClass.fromFile(filePath);
found = true;
config = local.merge(config);
}
}
/* stop if a configuration with "root" is set to true */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment