Skip to content

Commit

Permalink
feat: add option to configure standard via rc config file
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Dec 13, 2020
1 parent b695a02 commit 2f34b98
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module.exports.linter = Linter

const os = require('os')
const path = require('path')
const pkgConf = require('pkg-conf')
const fs = require('fs')
const { cosmiconfigSync } = require('cosmiconfig')

const CACHE_HOME = require('xdg-basedir').cache || os.tmpdir()

Expand Down Expand Up @@ -151,9 +151,12 @@ Linter.prototype.parseOpts = function (opts) {
let packageOpts = {}
let rootPath = null

// try default search places up to ~
const explorerRes = cosmiconfigSync(self.cmd).search(opts.cwd)

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 +205,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
19 changes: 19 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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 +66,11 @@ 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'])
})
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']
}

0 comments on commit 2f34b98

Please sign in to comment.