diff --git a/README.md b/README.md index def0f22..b43c153 100644 --- a/README.md +++ b/README.md @@ -69,9 +69,9 @@ First, you will always need to provide two paths to the tool. The first is the _ If installed as a dependency, you can `require('eslint-compare-config')` and get access to a number of functions: -- `getConfig(leftPath, rightPath)` - puts a file in each of the target directories whichs uses `eslint` APIs to load the configuration for a file in that directory, runs them, then deletes them. +- `getConfig(path)` - puts a file in each of the target directories whichs uses `eslint` APIs to load the configuration for a file in that directory, runs them, then deletes them. - `getDifferences(left, right)` - given two configs, produces an object describing all of their differences. The same thing you get when you use the `--json` option -- `getLiteralConfig(leftPath, rightPath)` - loads the target files using `require()`, thus supporting only JavaScript and JSON file formats +- `getLiteralConfig(path)` - loads the target files using `require()`, thus supporting only JavaScript and JSON file formats - `getScore(differences)` - given `getDifferences()` output, returns a similarity score 1-100. - `normalizeConfig(config)` - removes disabled rules, and turns numeric toggles into string (1 = 'warning', 2 = 'error') - `renderDifferences(differences)` - given `getDifferences()` output, returns string with human-readable comparison (including ANSI color codes) diff --git a/src/cli.js b/src/cli.js index ac63b39..8409f38 100644 --- a/src/cli.js +++ b/src/cli.js @@ -3,6 +3,7 @@ 'use strict'; var dashdash = require('dashdash'); +var _ = require('lodash'); var getConfig = require('./get_config'); var getLiteralConfig = require('./get_literal_config'); @@ -60,11 +61,13 @@ function showHelp(parsed, parser) { } function loadConfig(literal, left, right) { - var config = literal ? getLiteralConfig(left, right) : getConfig(left, right); + var get = literal ? getLiteralConfig : getConfig; + var leftConfig = _.omit(get(left), ['globals']); + var rightConfig = _.omit(get(right), ['globals']); return { - left: normalizeConfig(config.left), - right: normalizeConfig(config.right), + left: normalizeConfig(leftConfig), + right: normalizeConfig(rightConfig), }; } diff --git a/src/get_config.js b/src/get_config.js index 96e855d..183168d 100644 --- a/src/get_config.js +++ b/src/get_config.js @@ -6,25 +6,12 @@ var path = require('path'); var fs = require('fs'); var childProcess = require('child_process'); -var _ = require('lodash'); - var script = fs.readFileSync(path.join(__dirname, '_get_config.js')).toString(); -function getConfig(leftPath, rightPath) { - var leftDir = path.resolve(leftPath); - var rightDir = path.resolve(rightPath); - - var left = getConfigForDir(leftDir); - var right = getConfigForDir(rightDir); - - return { - left: _.omit(left, ['globals']), - right: _.omit(right, ['globals']), - }; -} +function getConfig(startPath) { + startPath = path.resolve(startPath); -function getConfigForDir(startPath) { var stat = fs.statSync(startPath); var dir = stat.isDirectory() ? startPath : path.dirname(startPath); var filename = '__get_config.js'; diff --git a/src/get_literal_config.js b/src/get_literal_config.js index 1161bed..92996fc 100644 --- a/src/get_literal_config.js +++ b/src/get_literal_config.js @@ -5,14 +5,8 @@ var path = require('path'); -function getLiteralConfig(leftPath, rightPath) { - var left = require(path.resolve(leftPath)); - var right = require(path.resolve(rightPath)); - - return { - left: left, - right: right, - }; +function getLiteralConfig(target) { + return require(path.resolve(target)); } module.exports = getLiteralConfig; diff --git a/test/integration/test_get_config.js b/test/integration/test_get_config.js index 95ea52f..991785c 100644 --- a/test/integration/test_get_config.js +++ b/test/integration/test_get_config.js @@ -12,7 +12,10 @@ describe('integration/getConfig', function() { var left = 'test/integration/left'; var right = 'test/integration/right'; - var actual = getConfig(left, right); + var actual = { + left: getConfig(left), + right: getConfig(right), + }; expect(actual).to.have.all.keys('left', 'right'); diff --git a/test/integration/test_get_literal_config.js b/test/integration/test_get_literal_config.js index cf2e6ca..c5be078 100644 --- a/test/integration/test_get_literal_config.js +++ b/test/integration/test_get_literal_config.js @@ -12,7 +12,10 @@ describe('integration/getLiteralConfig', function() { var left = 'test/integration/left/.eslintrc.js'; var right = 'test/integration/right/.eslintrc.js'; - var actual = getConfig(left, right); + var actual = { + left: getConfig(left), + right: getConfig(right), + }; expect(actual).to.have.all.keys('left', 'right');