diff --git a/docs/user-guide/usage/cli.md b/docs/user-guide/usage/cli.md index d4c8a4bd22..4420b2800a 100644 --- a/docs/user-guide/usage/cli.md +++ b/docs/user-guide/usage/cli.md @@ -36,6 +36,10 @@ Print the configuration for the given path. stylelint outputs the configuration Only register violations for rules with an "error"-level severity (ignore "warning"-level). +### `--stdin` + +Accept stdin input even if it is empty. + ### `--version, -v` Show the currently installed version of stylelint. diff --git a/lib/__tests__/cli.test.js b/lib/__tests__/cli.test.js index f306d86656..4b4075326a 100644 --- a/lib/__tests__/cli.test.js +++ b/lib/__tests__/cli.test.js @@ -17,6 +17,7 @@ describe('buildCLI', () => { quiet: false, reportInvalidScopeDisables: false, reportNeedlessDisables: false, + stdin: false, version: false, }); }); @@ -124,6 +125,10 @@ describe('buildCLI', () => { expect(buildCLI(['--rd']).flags.reportNeedlessDisables).toBe(true); }); + it('flags.stdin', () => { + expect(buildCLI(['--stdin']).flags.stdin).toBe(true); + }); + it('flags.stdinFilename', () => { expect(buildCLI(['--stdin-filename=foo.css']).flags.stdinFilename).toBe('foo.css'); }); diff --git a/lib/cli.js b/lib/cli.js index 20b8f4a571..d5edf9bfa0 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -36,6 +36,7 @@ const EXIT_CODE_ERROR = 2; * @property {string} [ignorePattern] * @property {string} [noColor] * @property {string} [outputFile] + * @property {boolean} [stdin] * @property {string} [stdinFilename] * @property {boolean} [reportNeedlessDisables] * @property {boolean} [reportInvalidScopeDisables] @@ -145,6 +146,10 @@ const meowOptions = { Module name or path to a JS file exporting a PostCSS-compatible syntax. + --stdin + + Accept stdin input even if it is empty. + --stdin-filename A filename to assign stdin input. @@ -296,6 +301,9 @@ const meowOptions = { alias: 'rd', type: 'boolean', }, + stdin: { + type: 'boolean', + }, stdinFilename: { type: 'string', }, @@ -464,7 +472,7 @@ module.exports = (argv) => { .catch(handleError); } - if (!options.files && !options.code) { + if (!options.files && !options.code && !cli.flags.stdin) { cli.showHelp(); return; diff --git a/system-tests/cli/__snapshots__/cli.test.js.snap b/system-tests/cli/__snapshots__/cli.test.js.snap index 1b84d4e4a9..8daa701743 100644 --- a/system-tests/cli/__snapshots__/cli.test.js.snap +++ b/system-tests/cli/__snapshots__/cli.test.js.snap @@ -62,6 +62,10 @@ exports[`CLI --help 1`] = ` Module name or path to a JS file exporting a PostCSS-compatible syntax. + --stdin + + Accept stdin input even if it is empty. + --stdin-filename A filename to assign stdin input. diff --git a/system-tests/cli/cli.test.js b/system-tests/cli/cli.test.js index 384fc13ed5..aca5c32e83 100644 --- a/system-tests/cli/cli.test.js +++ b/system-tests/cli/cli.test.js @@ -77,6 +77,7 @@ describe('CLI', () => { { rules: { 'block-no-empty': [true], + 'no-empty-source': [true], }, }, null, @@ -104,4 +105,16 @@ describe('CLI', () => { expect.stringContaining('Unexpected empty block'), ); }); + + it('--stdin', async () => { + await cli(['--stdin', '--config', path.join(__dirname, 'config.json')]); + + expect(process.exitCode).toBe(2); + + expect(process.stdout.write).toHaveBeenCalledTimes(1); + expect(process.stdout.write).toHaveBeenNthCalledWith( + 1, + expect.stringContaining('Unexpected empty source'), + ); + }); }); diff --git a/system-tests/cli/config.json b/system-tests/cli/config.json index 0eb6e7c4de..095e2d2028 100644 --- a/system-tests/cli/config.json +++ b/system-tests/cli/config.json @@ -1,5 +1,6 @@ { "rules": { - "block-no-empty": true + "block-no-empty": true, + "no-empty-source": true } }