From e81298631ebb7439f99417fbe4456663b95948e9 Mon Sep 17 00:00:00 2001 From: Tyler Hall <0tillathehun0@gmail.com> Date: Tue, 5 Mar 2019 22:22:30 -0500 Subject: [PATCH] feat: add option to configure standard via rc config file --- index.js | 8 +++++--- package.json | 4 ++-- test/api.js | 19 +++++++++++++++++++ test/lib/.pocketlintrc.js | 3 +++ 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 test/lib/.pocketlintrc.js diff --git a/index.js b/index.js index 4a940d8..1686e38 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ module.exports.linter = Linter var deglob = require('deglob') var os = require('os') var path = require('path') -var pkgConf = require('pkg-conf') +var { cosmiconfigSync } = require('cosmiconfig') var HOME_OR_TMP = os.homedir() || os.tmpdir() @@ -151,8 +151,10 @@ Linter.prototype.parseOpts = function (opts) { ? opts.usePackageJson : true + var explorerRes = cosmiconfigSync(self.cmd).search(opts.cwd) + var packageOpts = usePackageJson - ? pkgConf.sync(self.cmd, { cwd: opts.cwd }) + ? explorerRes ? explorerRes.config : {} : {} if (!opts.ignore) opts.ignore = [] @@ -175,7 +177,7 @@ Linter.prototype.parseOpts = function (opts) { if (self.customParseOpts) { var rootDir if (usePackageJson) { - var filePath = pkgConf.filepath(packageOpts) + var filePath = explorerRes && explorerRes.filepath rootDir = filePath ? path.dirname(filePath) : opts.cwd } else { rootDir = opts.cwd diff --git a/package.json b/package.json index 8c46306..a12c8c2 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,10 @@ "url": "https://github.com/flet/standard-engine/issues" }, "dependencies": { + "cosmiconfig": "^6.0.0", "deglob": "^4.0.0", "get-stdin": "^7.0.0", - "minimist": "^1.1.0", - "pkg-conf": "^3.1.0" + "minimist": "^1.1.0" }, "devDependencies": { "babel-eslint": "^10.0.2", diff --git a/test/api.js b/test/api.js index 6df5630..6d59e1f 100644 --- a/test/api.js +++ b/test/api.js @@ -1,3 +1,4 @@ +var path = require('path') var eslint = require('eslint') var Linter = require('../').linter var test = require('tape') @@ -11,6 +12,16 @@ function getStandard () { }) } +function getStandardRcConfig () { + return new Linter({ + 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) var standard = getStandard() @@ -54,3 +65,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) + var standard = getStandardRcConfig() + var opts = standard.parseOpts() + t.deepEqual(opts.globals, undefined) + t.deepEqual(opts.eslintConfig.globals, ['foorc']) +}) diff --git a/test/lib/.pocketlintrc.js b/test/lib/.pocketlintrc.js new file mode 100644 index 0000000..61ed106 --- /dev/null +++ b/test/lib/.pocketlintrc.js @@ -0,0 +1,3 @@ +module.exports = { + globals: ['foorc'] +} \ No newline at end of file