Skip to content

Commit 3df4e33

Browse files
committed
feat: fallback to XDG config directory
1 parent 632fef0 commit 3df4e33

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ module.exports.linter = Linter
66
const os = require('os')
77
const path = require('path')
88
const fs = require('fs')
9+
const xdgBasedir = require('xdg-basedir')
910
const { cosmiconfigSync } = require('cosmiconfig')
1011

11-
const CACHE_HOME = require('xdg-basedir').cache || os.tmpdir()
12+
const CACHE_HOME = xdgBasedir.cache || os.tmpdir()
1213

1314
const DEFAULT_EXTENSIONS = [
1415
'.js',
@@ -24,6 +25,15 @@ const DEFAULT_IGNORE = [
2425
'vendor/**'
2526
]
2627

28+
const XDG_CONFIG_SEARCH_PLACES = [
29+
'config',
30+
'config.json',
31+
'config.yaml',
32+
'config.yml',
33+
'config.js',
34+
'config.cjs'
35+
]
36+
2737
function Linter (opts) {
2838
if (!(this instanceof Linter)) return new Linter(opts)
2939
if (!opts) opts = {}
@@ -152,7 +162,19 @@ Linter.prototype.parseOpts = function (opts) {
152162
let rootPath = null
153163

154164
// try default search places up to ~
155-
const explorerRes = cosmiconfigSync(self.cmd).search(opts.cwd)
165+
let explorerRes = cosmiconfigSync(self.cmd).search(opts.cwd)
166+
167+
// Fallback to XDG config base dir if no config is found
168+
if (!explorerRes && xdgBasedir.config) {
169+
explorerRes = cosmiconfigSync(self.cmd, {
170+
searchPlaces: XDG_CONFIG_SEARCH_PLACES,
171+
// Only search the specific config dir
172+
stopDir: path.join(xdgBasedir.config)
173+
}).search(
174+
// ie. ~/.config/standard/config.js
175+
path.join(xdgBasedir.config, self.cmd)
176+
)
177+
}
156178

157179
if (opts.usePackageJson || opts.useGitIgnore) {
158180
packageOpts = explorerRes ? explorerRes.config : {}

test/api.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const eslint = require('eslint')
2-
const Linter = require('../').linter
31
const path = require('path')
2+
const eslint = require('eslint')
43
const test = require('tape')
54

5+
let Linter = require('../').linter
6+
67
function getStandard () {
78
return new Linter({
89
cmd: 'pocketlint',
@@ -74,3 +75,20 @@ test('api: parseOpts -- load config from rc file', function (t) {
7475
t.deepEqual(opts.globals, undefined)
7576
t.deepEqual(opts.eslintConfig.globals, ['foorc'])
7677
})
78+
79+
test('api: parseOpts -- load config from XDG config base dir', function (t) {
80+
process.env.XDG_CONFIG_HOME = path.join(__dirname, 'lib', '.xdgconfig')
81+
82+
delete require.cache['xdg-basedir']
83+
delete require.cache['../']
84+
85+
// re-require to ensure env variable is used
86+
Linter = require('../').linter
87+
88+
t.plan(2)
89+
const standard = getStandard()
90+
const opts = standard.parseOpts()
91+
92+
t.deepEqual(opts.globals, undefined)
93+
t.deepEqual(opts.eslintConfig.globals, ['xdgrc'])
94+
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// used for testing loading a config from XDG config directory
2+
module.exports = {
3+
globals: ['xdgrc']
4+
}

0 commit comments

Comments
 (0)