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, or in the user's home directory (e.g. `~/.svglintrc.js` on Unix-like systems) - this is similar to tools such as ESLint.

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

Expand Down
27 changes: 26 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,21 @@ async function getDefaultConfigurationFileTraversingParents(folder) {
}
}

/**
* Get the configuration file to use from the home directory.
* @returns {Promise<String,Boolean>} The path to the configuration file, or false
*/
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;
}

return filepath;
}

/**
* Get the configuration file to use
* @param {String} filename The filename to look for
Expand All @@ -94,7 +113,12 @@ async function getConfigurationFile(filename, folder) {
}
}

return await getDefaultConfigurationFileTraversingParents(folder);
filepath = await getDefaultConfigurationFileTraversingParents(folder);
if (filepath) {
return filepath;
}

return await getConfigurationInHomedir();
}

/**
Expand All @@ -104,6 +128,7 @@ async function getConfigurationFile(filename, folder) {
*/
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