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
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,37 @@ For example, when passing the `src/` directory and the `extensions` option is

You can disable these default ignores by setting the `noDefaultExensions` option to `true`.

### Configuration Formats

This documentation shows examples of adding configuration to `standard-engine` via the `package.json`. Alternatively, a configuration file can be used. The configuration can be provided as follows:

```js
[
'package.json',
`.pocketlintrc`,
`.pocketlintrc.json`,
`.pocketlintrc.yaml`,
`.pocketlintrc.yml`,
`.pocketlintrc.js`,
`.pocketlintrc.cjs`,
`pocketlint.config.js`,
`pocketlint.config.cjs`,
]
```

Configuration is searched for in the current directory. If configuration is not found, the parent directory is then searched, and so on until the user's home directory is reached. If configuration is still not found, the `XDG_CONFIG_HOME` is then searched for confguration. The configuration in the XDG config directory can be provided as follows:

```js
[
'pocketlint/config',
'pocketlint/config.json',
'pocketlint/config.yaml',
'pocketlint/config.yml',
'pocketlint/config.js',
'pocketlint/config.cjs'
]
```

### Ignoring Files

The paths `node_modules/**`, `*.min.js`, `coverage/**`, hidden files/folders
Expand Down
35 changes: 30 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ module.exports.linter = Linter

const os = require('os')
const path = require('path')
const pkgConf = require('pkg-conf')
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 @@ -151,9 +161,24 @@ Linter.prototype.parseOpts = function (opts) {
let packageOpts = {}
let rootPath = null

// try default search places up to ~
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 = pkgConf.sync(self.cmd, { cwd: opts.cwd })
const packageJsonPath = pkgConf.filepath(packageOpts)
packageOpts = explorerRes ? explorerRes.config : {}
const packageJsonPath = explorerRes && explorerRes.filepath
if (packageJsonPath) rootPath = path.dirname(packageJsonPath)
}

Expand Down Expand Up @@ -202,7 +227,7 @@ Linter.prototype.parseOpts = function (opts) {
if (self.customParseOpts) {
let rootDir
if (opts.usePackageJson) {
const filePath = pkgConf.filepath(packageOpts)
const filePath = explorerRes.filepath
rootDir = filePath ? path.dirname(filePath) : opts.cwd
} else {
rootDir = opts.cwd
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"test": "standard && tape test/clone.js test/api.js"
},
"dependencies": {
"cosmiconfig": "^7.0.0",
"get-stdin": "^8.0.0",
"minimist": "^1.2.5",
"pkg-conf": "^3.1.0",
"xdg-basedir": "^4.0.0"
},
"devDependencies": {
Expand Down
41 changes: 39 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',
Expand All @@ -12,6 +13,17 @@ function getStandard () {
})
}

function getStandardRcConfig () {
return new Linter({
// start in dir containing rc file
cwd: path.resolve(__dirname, 'lib'),
cmd: 'pocketlint',
version: '0.0.0',
eslint: eslint,
eslintConfig: require('../tmp/standard/options').eslintConfig
})
}

test('api: lintFiles', function (t) {
t.plan(3)
const standard = getStandard()
Expand Down Expand Up @@ -55,3 +67,28 @@ test('api: parseOpts -- avoid self.eslintConfig global mutation', function (t) {
t.deepEqual(opts.globals, ['what'])
t.deepEqual(standard.eslintConfig.globals, [])
})

test('api: parseOpts -- load config from rc file', function (t) {
t.plan(2)
const standard = getStandardRcConfig()
const opts = standard.parseOpts()
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'])
})
3 changes: 3 additions & 0 deletions test/lib/.pocketlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
globals: ['foorc']
}
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']
}