Skip to content

Commit

Permalink
Add --stdin flag (#4594)
Browse files Browse the repository at this point in the history
  • Loading branch information
alypeng committed Feb 13, 2020
1 parent 8c25249 commit 12c22cb
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/user-guide/usage/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions lib/__tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('buildCLI', () => {
quiet: false,
reportInvalidScopeDisables: false,
reportNeedlessDisables: false,
stdin: false,
version: false,
});
});
Expand Down Expand Up @@ -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');
});
Expand Down
10 changes: 9 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -296,6 +301,9 @@ const meowOptions = {
alias: 'rd',
type: 'boolean',
},
stdin: {
type: 'boolean',
},
stdinFilename: {
type: 'string',
},
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions system-tests/cli/__snapshots__/cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions system-tests/cli/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('CLI', () => {
{
rules: {
'block-no-empty': [true],
'no-empty-source': [true],
},
},
null,
Expand Down Expand Up @@ -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'),
);
});
});
3 changes: 2 additions & 1 deletion system-tests/cli/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rules": {
"block-no-empty": true
"block-no-empty": true,
"no-empty-source": true
}
}

0 comments on commit 12c22cb

Please sign in to comment.