diff --git a/index.js b/index.js index 07a7da9..b5a4877 100644 --- a/index.js +++ b/index.js @@ -6,9 +6,10 @@ module.exports.linter = Linter const os = require('os') const path = require('path') const fs = require('fs') +const xdgBasedir = require('xdg-basedir') const { cosmiconfigSync } = require('cosmiconfig') -const CACHE_HOME = require('xdg-basedir').cache || os.tmpdir() +const CACHE_HOME = xdgBasedir.cache || os.tmpdir() const DEFAULT_EXTENSIONS = [ '.js', @@ -24,6 +25,15 @@ const DEFAULT_IGNORE = [ 'vendor/**' ] +const XDG_CONFIG_SEARCH_PLACES = [ + 'config', + 'config.json', + 'config.yaml', + 'config.yml', + 'config.js', + 'config.cjs' +] + function Linter (opts) { if (!(this instanceof Linter)) return new Linter(opts) if (!opts) opts = {} @@ -152,7 +162,19 @@ Linter.prototype.parseOpts = function (opts) { let rootPath = null // try default search places up to ~ - const explorerRes = cosmiconfigSync(self.cmd).search(opts.cwd) + let explorerRes = cosmiconfigSync(self.cmd).search(opts.cwd) + + // Fallback to XDG config base dir if no config is found + if (!explorerRes && xdgBasedir.config) { + explorerRes = cosmiconfigSync(self.cmd, { + searchPlaces: XDG_CONFIG_SEARCH_PLACES, + // Only search the specific config dir + stopDir: path.join(xdgBasedir.config) + }).search( + // ie. ~/.config/standard/config.js + path.join(xdgBasedir.config, self.cmd) + ) + } if (opts.usePackageJson || opts.useGitIgnore) { packageOpts = explorerRes ? explorerRes.config : {} diff --git a/test/api.js b/test/api.js index c38f82f..3df8677 100644 --- a/test/api.js +++ b/test/api.js @@ -1,8 +1,9 @@ -const eslint = require('eslint') -const Linter = require('../').linter const path = require('path') +const eslint = require('eslint') const test = require('tape') +let Linter = require('../').linter + function getStandard () { return new Linter({ cmd: 'pocketlint', @@ -74,3 +75,20 @@ test('api: parseOpts -- load config from rc file', function (t) { t.deepEqual(opts.globals, undefined) t.deepEqual(opts.eslintConfig.globals, ['foorc']) }) + +test('api: parseOpts -- load config from XDG config base dir', function (t) { + process.env.XDG_CONFIG_HOME = path.join(__dirname, 'lib', '.xdgconfig') + + delete require.cache['xdg-basedir'] + delete require.cache['../'] + + // re-require to ensure env variable is used + Linter = require('../').linter + + t.plan(2) + const standard = getStandard() + const opts = standard.parseOpts() + + t.deepEqual(opts.globals, undefined) + t.deepEqual(opts.eslintConfig.globals, ['xdgrc']) +}) diff --git a/test/lib/.xdgconfig/pocketlint/config.js b/test/lib/.xdgconfig/pocketlint/config.js new file mode 100644 index 0000000..851fbf8 --- /dev/null +++ b/test/lib/.xdgconfig/pocketlint/config.js @@ -0,0 +1,4 @@ +// used for testing loading a config from XDG config directory +module.exports = { + globals: ['xdgrc'] +} \ No newline at end of file