Skip to content

Commit

Permalink
Add --globby-options flag (#6437)
Browse files Browse the repository at this point in the history
Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
  • Loading branch information
sidverma32 and ybiquitous authored Nov 11, 2022
1 parent d91bb5b commit b54083c
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-moles-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": minor
---

Added: `--globby-options` flag
4 changes: 4 additions & 0 deletions docs/user-guide/usage/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ Automatically fix, where possible, problems reported by rules. [More info](optio

Specify the formatter to format your results. [More info](options.md#formatter).

## `--globbyOptions, --go`

Options in JSON format passed to [globby](https://github.com/sindresorhus/globby). [More info](options.md#globbyoptions).

### `--ignore-disables, --id`

Ignore `stylelint-disable` (e.g. `/* stylelint-disable block-no-empty */`) comments. [More info](options.md#ignoredisables).
Expand Down
6 changes: 6 additions & 0 deletions docs/user-guide/usage/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ CLI flags: `--disable-default-ignores, --di`

Disable the default ignores. Stylelint will not automatically ignore the contents of `node_modules`.

## `globbyOptions`

CLI flags: `--globby-options, --go`

Options passed to [globby](https://github.com/sindresorhus/globby). [More info](node-api.md#globbyoptions).

## `ignorePath`

CLI flags: `--ignore-path, -i`
Expand Down
4 changes: 4 additions & 0 deletions lib/__tests__/__snapshots__/cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,9 @@ exports[`CLI --help 1`] = `
--allow-empty-input, --aei
When glob pattern matches no files, the process will exit without throwing an error.
--globby-options, --go
Options in JSON format passed to globby.
"
`;
28 changes: 28 additions & 0 deletions lib/__tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ describe('buildCLI', () => {
expect(buildCLI(['--cache-location=foo']).flags.cacheLocation).toBe('foo');
});

it('flags.globbyOptions', () => {
expect(buildCLI(['--globby-options={"dot":true}']).flags.globbyOptions).toBe('{"dot":true}');
expect(buildCLI(['--go={"dot":true}']).flags.globbyOptions).toBe('{"dot":true}');
});

it('flags.cacheStrategy', () => {
expect(buildCLI(['--cache-strategy=content']).flags.cacheStrategy).toBe('content');
expect(buildCLI(['--cache-strategy=metadata']).flags.cacheStrategy).toBe('metadata');
Expand Down Expand Up @@ -374,4 +379,27 @@ describe('CLI', () => {

expect(output).toBe('Custom formatter reports 1 warning(s).');
});

it('output a message when wrong --globby-options provided', async () => {
await cli(['--globby-options=wrong']);

expect(process.exitCode).toBe(2);
expect(process.stdout.write).toHaveBeenCalledTimes(0);
expect(process.stderr.write).toHaveBeenCalledTimes(1);
expect(stripAnsi(process.stderr.write.mock.calls[0][0])).toContain(
'Invalid option "--globby-options". The value "wrong" is not valid JSON object.',
);
});

it('--globby-options', async () => {
await cli([
'--globby-options={"dot":true}',
'--config',
fixturesPath('config-block-no-empty.json'),
fixturesPath('globby-options'),
]);

expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenCalledWith(expect.stringMatching(/block-no-empty/));
});
});
1 change: 1 addition & 0 deletions lib/__tests__/fixtures/globby-options/.dot/empty-block.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a {}
50 changes: 50 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { EOL } = require('os');
const meow = require('meow');
const path = require('path');
const { red, dim } = require('picocolors');
const { isPlainObject } = require('./utils/validateTypes');

const checkInvalidCLIOptions = require('./utils/checkInvalidCLIOptions');
const getFormatterOptionsText = require('./utils/getFormatterOptionsText');
Expand Down Expand Up @@ -47,6 +48,7 @@ const EXIT_CODE_ERROR = 2;
* @property {string} [syntax]
* @property {string} [version]
* @property {boolean} [allowEmptyInput]
* @property {string} [globbyOptions]
*/

/**
Expand All @@ -72,6 +74,7 @@ const EXIT_CODE_ERROR = 2;
* @property {boolean} [quiet]
* @property {any} [printConfig]
* @property {boolean} [fix]
* @property {Record<string, unknown>} [globbyOptions]
* @property {boolean} [ignoreDisables]
* @property {any} [ignorePath]
* @property {string} [outputFile]
Expand Down Expand Up @@ -236,6 +239,10 @@ const meowOptions = {
--allow-empty-input, --aei
When glob pattern matches no files, the process will exit without throwing an error.
--globby-options, --go
Options in JSON format passed to globby.
`,
flags: {
allowEmptyInput: {
Expand Down Expand Up @@ -337,6 +344,10 @@ const meowOptions = {
alias: 'v',
type: 'boolean',
},
globbyOptions: {
alias: 'go',
type: 'string',
},
},
};

Expand Down Expand Up @@ -397,6 +408,21 @@ module.exports = async (argv) => {
: path.resolve(process.cwd(), cli.flags.configBasedir);
}

if (cli.flags.globbyOptions) {
try {
optionsBase.globbyOptions = await parseGlobbyOptions(cli.flags.globbyOptions);
} catch (error) {
if (typeof error === 'string') {
process.stderr.write(`${error}${EOL}`);
process.exitCode = EXIT_CODE_ERROR;

return;
}

throw error;
}
}

if (cli.flags.stdinFilename) {
optionsBase.codeFilename = cli.flags.stdinFilename;
}
Expand Down Expand Up @@ -542,6 +568,30 @@ function handleError(err) {
process.exitCode = exitCode;
}

/**
* @param {string} value
* @returns {Promise<Record<string, unknown>>}
*/
function parseGlobbyOptions(value) {
const errorMessage = () =>
`Invalid option ${red('"--globby-options"')}.` +
` The value ${red(`"${value}"`)} is not valid JSON object.`;

let options;

try {
options = JSON.parse(value);
} catch (_) {
return Promise.reject(errorMessage());
}

if (isPlainObject(options)) {
return Promise.resolve(options);
}

return Promise.reject(errorMessage());
}

/**
* @param {string[]} argv
* @returns {CLIOptions}
Expand Down

0 comments on commit b54083c

Please sign in to comment.