Skip to content

Commit

Permalink
Add --print-config option
Browse files Browse the repository at this point in the history
Co-authored-by: teehemkay <tmk@exidia.com>
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk and teehemkay committed Apr 6, 2021
1 parent c886ce5 commit c1bc545
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cli-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const cli = meow(`
--cwd=<dir> Working directory for files
--stdin Validate/fix code from stdin
--stdin-filename Specify a filename for the --stdin option
--print-config Print the ESLint configuration for the given file
Examples
$ xo
Expand All @@ -41,6 +42,7 @@ const cli = meow(`
$ xo --plugin=react
$ xo --plugin=html --extension=html
$ echo 'const x=true' | xo --stdin --fix
$ xo --print-config=index.js
Tips
- Add XO to your project with \`npm init xo\`.
Expand Down Expand Up @@ -103,6 +105,9 @@ const cli = meow(`
cwd: {
type: 'string'
},
printConfig: {
type: 'string'
},
stdin: {
type: 'boolean'
},
Expand Down Expand Up @@ -174,7 +179,21 @@ if (options.nodeVersion) {
}

(async () => {
if (options.stdin) {
if (options.printConfig) {
if (input.length > 0) {
console.error('The `print-config` option must be used with exactly one file name');
process.exit(1);
}

if (options.stdin) {
console.error('The `print-config` option is not supported on stdin');
process.exit(1);
}

options.filename = options.printConfig;
const config = xo.getConfig(options);
console.log(JSON.stringify(config, null, '\t'));
} else if (options.stdin) {
const stdin = await getStdin();

if (options.stdinFilename) {
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ const globFiles = async (patterns, {ignores, extensions, cwd}) => (
{ignore: ignores, gitignore: true, cwd}
)).filter(file => extensions.includes(path.extname(file).slice(1))).map(file => path.resolve(cwd, file));

const getConfig = options => {
const {options: foundOptions, prettierOptions} = mergeWithFileConfig(normalizeOptions(options));
options = buildConfig(foundOptions, prettierOptions);
const engine = new eslint.CLIEngine(options);
return engine.getConfigForFile(options.filename);
};

const lintText = (string, options) => {
const {options: foundOptions, prettierOptions} = mergeWithFileConfig(normalizeOptions(options));
options = buildConfig(foundOptions, prettierOptions);
Expand Down Expand Up @@ -121,6 +128,7 @@ module.exports = {
getFormatter: eslint.CLIEngine.getFormatter,
getErrorResults: eslint.CLIEngine.getErrorResults,
outputFixes: eslint.CLIEngine.outputFixes,
getConfig,
lintText,
lintFiles
};
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ $ xo --help
--cwd=<dir> Working directory for files
--stdin Validate/fix code from stdin
--stdin-filename Specify a filename for the --stdin option
--print-config Print the ESLint configuration for the given file
Examples
$ xo
Expand All @@ -82,6 +83,7 @@ $ xo --help
$ xo --plugin=react
$ xo --plugin=html --extension=html
$ echo 'const x=true' | xo --stdin --fix
$ xo --print-config=index.js
Tips
- Add XO to your project with `npm init xo`.
Expand Down
18 changes: 18 additions & 0 deletions test/cli-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,21 @@ test('extension option', async t => {
t.is(reports.length, 1);
t.true(reports[0].filePath.endsWith('.unknown'));
});

test('invalid print-config option with stdin', async t => {
const msg = 'The `print-config` option is not supported on stdin';
try {
await main(['--print-config', 'x.js', '--stdin'], {input: 'console.log()\n'});
} catch (error) {
t.is(error.stderr.trim(), msg);
}
});

test('print-config option requires a single filename', async t => {
const msg = 'The `print-config` option must be used with exactly one file name';
try {
await main(['--print-config', 'x.js', 'y.js']);
} catch (error) {
t.is(error.stderr.trim(), msg);
}
});
26 changes: 26 additions & 0 deletions test/print-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import path from 'path';
import test from 'ava';
import execa from 'execa';
import tempWrite from 'temp-write';
import fn from '..';

process.chdir(__dirname);

const main = (arguments_, options) => execa(path.join(__dirname, '../cli-main.js'), arguments_, options);

const hasUnicornPlugin = config => config.plugins.includes('unicorn');
const hasPrintConfigGlobal = config => Object.keys(config.globals).includes('printConfig');

test('getConfig', async t => {
const filepath = await tempWrite('console.log()\n', 'x.js');
const options = {filename: filepath, globals: ['printConfig']};
const result = fn.getConfig(options);
t.true(hasUnicornPlugin(result) && hasPrintConfigGlobal(result));
});

test('print-config option', async t => {
const filepath = await tempWrite('console.log()\n', 'x.js');
const {stdout} = await main(['--global=printConfig', '--print-config', filepath]);
const result = JSON.parse(stdout);
t.true(hasUnicornPlugin(result) && hasPrintConfigGlobal(result));
});

0 comments on commit c1bc545

Please sign in to comment.