Skip to content

Commit

Permalink
feat: fallback to XDG config directory
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Dec 12, 2020
1 parent 632fef0 commit 3df4e33
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
26 changes: 24 additions & 2 deletions index.js
Expand Up @@ -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',
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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 : {}
Expand Down
22 changes: 20 additions & 2 deletions 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',
Expand Down Expand Up @@ -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'])
})
4 changes: 4 additions & 0 deletions test/lib/.xdgconfig/pocketlint/config.js
@@ -0,0 +1,4 @@
// used for testing loading a config from XDG config directory
module.exports = {
globals: ['xdgrc']
}

0 comments on commit 3df4e33

Please sign in to comment.