Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for a default config file in $HOME/.svglintrc.js #90

Merged
merged 10 commits into from
Oct 31, 2023
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ linting.on("done", () => {
## Config

In order to specify what should be linted SVGLint must be given a configuration object.
If you are using the CLI, this configuration object is read from the file specified by `--config`. This defaults to `.svglintrc.js`, which will be searched for up through the directory tree - this is similar to tools such as ESLint.
If you are using the CLI, this configuration object is read from the file specified by `--config`. This defaults to `.svglintrc.js`, which will be searched for up through the directory tree - this is similar to tools such as ESLint. If no configuration file is found searching up the directory tree, a configuration file located in the user's home directory is used (i.e. "$HOME/.svglintrc.js" on Unix-like systems).
ericcornelissen marked this conversation as resolved.
Show resolved Hide resolved

This configuration file should export a single object, of the format:

Expand Down
24 changes: 23 additions & 1 deletion src/cli/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import path from "path";
import fs from "fs";
import os from "os";
import process from "process";

import Logger from "../lib/logger.js";
const logger = Logger("");

/**
* Check if a file exists
* @param {String} filepath The file to check for existence
Expand Down Expand Up @@ -75,6 +79,17 @@
}
}

async function getConfigurationInHomedir() {
kcaran marked this conversation as resolved.
Show resolved Hide resolved
let filepath;

const homedirFile = path.join(os.homedir(), ".svglintrc.js");
if (await fileExists(homedirFile)) {
filepath = homedirFile;

Check failure on line 87 in src/cli/config.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 7
}

return filepath;

Check failure on line 90 in src/cli/config.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 3
}

Check failure on line 91 in src/cli/config.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 0 spaces but found 1

/**
* Get the configuration file to use
* @param {String} filename The filename to look for
Expand All @@ -94,7 +109,13 @@
}
}

return await getDefaultConfigurationFileTraversingParents(folder);
// Look at parent directories or, finally, the user's home directory
ericcornelissen marked this conversation as resolved.
Show resolved Hide resolved
filepath = await getDefaultConfigurationFileTraversingParents(folder);
if (filepath) {
return filepath;
}

return await getConfigurationInHomedir();
}

/**
Expand All @@ -104,6 +125,7 @@
*/
async function loadConfigurationFile(filename, folder=process.cwd()) {
const filepath = await getConfigurationFile(filename, folder);
logger.debug("Using configuration file: " + filepath);
if (filepath) {
const module = await import(`file://${filepath}`);
return module.default;
Expand Down
4 changes: 2 additions & 2 deletions test/svgs/attr.test.svg
ericcornelissen marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading