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

Change configuration loading to cosmiconfig #420

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 20 additions & 1 deletion lib/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const os = require('os');
const path = require('path');
const arrify = require('arrify');
const {cosmiconfigSync, defaultLoaders} = require('cosmiconfig');
const mergeWith = require('lodash/mergeWith');
const multimatch = require('multimatch');
const pathExists = require('path-exists');
Expand Down Expand Up @@ -155,14 +156,32 @@ const normalizeOptions = options => {
return options;
};

const loadPackageConfig = options => {
const switchBackCwd = process.cwd();
const moduleName = 'xo';
// Yaml is disabled
const searchPlaces = ['package.json',
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.js`,
`${moduleName}.config.js`];
const loaders = {noExt: defaultLoaders['.json']};
// Change working directory for load config
process.chdir(options.cwd);
Copy link
Contributor

@pvdlg pvdlg Feb 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing the working directory directory, pass options.cwd to comisconfig in option searchFrom: https://github.com/davidtheclark/cosmiconfig#searchfrom

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I didn't saw this option before.

const searchResult = cosmiconfigSync(moduleName, {searchPlaces, loaders}).search();
// Switch back to last working directory
process.chdir(switchBackCwd);
return searchResult ? searchResult.config : {};
};

const mergeWithPackageConfig = options => {
options = {
cwd: process.cwd(),
...options
};

options.cwd = path.resolve(options.cwd);
const config = pkgConf.sync('xo', {cwd: options.cwd, skipOnFalse: true});
const config = loadPackageConfig(options);
const engines = pkgConf.sync('engines', {cwd: options.cwd});

return {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
],
"dependencies": {
"arrify": "^2.0.1",
"cosmiconfig": "^6.0.0",
"debug": "^4.1.0",
"eslint": "^6.4.0",
"eslint-config-prettier": "^6.3.0",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/config_file/.xorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"esnext": true,
"space": true
}
7 changes: 7 additions & 0 deletions test/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,10 @@ test('mergeWithPackageConfig: XO engine options false supersede package.json\'s'
const expected = {nodeVersion: false, cwd};
t.deepEqual(result, expected);
});

test('mergeWithPackageConfig: read config from file `.xorc`', t => {
const cwd = path.resolve('fixtures', 'config_file');
const result = manager.mergeWithPackageConfig({cwd, nodeVersion: false});
const expected = {esnext: true, space: true, nodeVersion: false, cwd};
t.deepEqual(result, expected);
});