Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to configure standard via rc config file #194

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: fallback to XDG config directory
  • Loading branch information
TillaTheHun0 committed Dec 13, 2020
commit 51469dab0bc2445f451d313d6776be9df783fd0d
26 changes: 24 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 : {}
22 changes: 20 additions & 2 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -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[require.resolve('xdg-basedir')]
delete require.cache[require.resolve('../')]

// 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// used for testing loading a config from XDG config directory
module.exports = {
globals: ['xdgrc']
}