Skip to content

Commit

Permalink
feat: look for a default config file in ~/.svglintrc.js (#90)
Browse files Browse the repository at this point in the history
Co-authored-by: Eric Cornelissen <[email protected]>
  • Loading branch information
kcaran and ericcornelissen authored Oct 31, 2023
1 parent 718e622 commit f15e463
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
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() {
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

0 comments on commit f15e463

Please sign in to comment.