diff --git a/README.md b/README.md index 9ea75c3..6d91d23 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/cli/config.js b/src/cli/config.js index 06f7ff8..e005f33 100644 --- a/src/cli/config.js +++ b/src/cli/config.js @@ -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 @@ -75,6 +79,21 @@ async function getDefaultConfigurationFileTraversingParents(folder) { } } +/** + * Get the configuration file to use from the home directory. + * @returns {Promise} 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 @@ -94,7 +113,12 @@ async function getConfigurationFile(filename, folder) { } } - return await getDefaultConfigurationFileTraversingParents(folder); + filepath = await getDefaultConfigurationFileTraversingParents(folder); + if (filepath) { + return filepath; + } + + return await getConfigurationInHomedir(); } /** @@ -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;