Skip to content

Commit

Permalink
feat(API): Simplify getConfig and getLiteralConfig to take one path
Browse files Browse the repository at this point in the history
BREAKING CHANGE: getConfig and getLiteralConfig previously always took
two parameters, one for each config to be compared. Now takes just one
path, loading one config at a time.
  • Loading branch information
scottnonnenberg committed Jun 20, 2016
1 parent c2350af commit 9c46df4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict';

var dashdash = require('dashdash');
var _ = require('lodash');

var getConfig = require('./get_config');
var getLiteralConfig = require('./get_literal_config');
Expand Down Expand Up @@ -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),
};
}

Expand Down
17 changes: 2 additions & 15 deletions src/get_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
10 changes: 2 additions & 8 deletions src/get_literal_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 4 additions & 1 deletion test/integration/test_get_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
5 changes: 4 additions & 1 deletion test/integration/test_get_literal_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit 9c46df4

Please sign in to comment.