Skip to content

Commit b54083c

Browse files
Add --globby-options flag (#6437)
Co-authored-by: Masafumi Koba <[email protected]>
1 parent d91bb5b commit b54083c

File tree

7 files changed

+98
-0
lines changed

7 files changed

+98
-0
lines changed

.changeset/lemon-moles-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"stylelint": minor
3+
---
4+
5+
Added: `--globby-options` flag

docs/user-guide/usage/cli.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ Automatically fix, where possible, problems reported by rules. [More info](optio
6868

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

71+
## `--globbyOptions, --go`
72+
73+
Options in JSON format passed to [globby](https://github.com/sindresorhus/globby). [More info](options.md#globbyoptions).
74+
7175
### `--ignore-disables, --id`
7276

7377
Ignore `stylelint-disable` (e.g. `/* stylelint-disable block-no-empty */`) comments. [More info](options.md#ignoredisables).

docs/user-guide/usage/options.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ CLI flags: `--disable-default-ignores, --di`
139139

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

142+
## `globbyOptions`
143+
144+
CLI flags: `--globby-options, --go`
145+
146+
Options passed to [globby](https://github.com/sindresorhus/globby). [More info](node-api.md#globbyoptions).
147+
142148
## `ignorePath`
143149

144150
CLI flags: `--ignore-path, -i`

lib/__tests__/__snapshots__/cli.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,9 @@ exports[`CLI --help 1`] = `
149149
--allow-empty-input, --aei
150150
151151
When glob pattern matches no files, the process will exit without throwing an error.
152+
153+
--globby-options, --go
154+
155+
Options in JSON format passed to globby.
152156
"
153157
`;

lib/__tests__/cli.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ describe('buildCLI', () => {
5050
expect(buildCLI(['--cache-location=foo']).flags.cacheLocation).toBe('foo');
5151
});
5252

53+
it('flags.globbyOptions', () => {
54+
expect(buildCLI(['--globby-options={"dot":true}']).flags.globbyOptions).toBe('{"dot":true}');
55+
expect(buildCLI(['--go={"dot":true}']).flags.globbyOptions).toBe('{"dot":true}');
56+
});
57+
5358
it('flags.cacheStrategy', () => {
5459
expect(buildCLI(['--cache-strategy=content']).flags.cacheStrategy).toBe('content');
5560
expect(buildCLI(['--cache-strategy=metadata']).flags.cacheStrategy).toBe('metadata');
@@ -374,4 +379,27 @@ describe('CLI', () => {
374379

375380
expect(output).toBe('Custom formatter reports 1 warning(s).');
376381
});
382+
383+
it('output a message when wrong --globby-options provided', async () => {
384+
await cli(['--globby-options=wrong']);
385+
386+
expect(process.exitCode).toBe(2);
387+
expect(process.stdout.write).toHaveBeenCalledTimes(0);
388+
expect(process.stderr.write).toHaveBeenCalledTimes(1);
389+
expect(stripAnsi(process.stderr.write.mock.calls[0][0])).toContain(
390+
'Invalid option "--globby-options". The value "wrong" is not valid JSON object.',
391+
);
392+
});
393+
394+
it('--globby-options', async () => {
395+
await cli([
396+
'--globby-options={"dot":true}',
397+
'--config',
398+
fixturesPath('config-block-no-empty.json'),
399+
fixturesPath('globby-options'),
400+
]);
401+
402+
expect(process.stdout.write).toHaveBeenCalledTimes(1);
403+
expect(process.stdout.write).toHaveBeenCalledWith(expect.stringMatching(/block-no-empty/));
404+
});
377405
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a {}

lib/cli.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { EOL } = require('os');
44
const meow = require('meow');
55
const path = require('path');
66
const { red, dim } = require('picocolors');
7+
const { isPlainObject } = require('./utils/validateTypes');
78

89
const checkInvalidCLIOptions = require('./utils/checkInvalidCLIOptions');
910
const getFormatterOptionsText = require('./utils/getFormatterOptionsText');
@@ -47,6 +48,7 @@ const EXIT_CODE_ERROR = 2;
4748
* @property {string} [syntax]
4849
* @property {string} [version]
4950
* @property {boolean} [allowEmptyInput]
51+
* @property {string} [globbyOptions]
5052
*/
5153

5254
/**
@@ -72,6 +74,7 @@ const EXIT_CODE_ERROR = 2;
7274
* @property {boolean} [quiet]
7375
* @property {any} [printConfig]
7476
* @property {boolean} [fix]
77+
* @property {Record<string, unknown>} [globbyOptions]
7578
* @property {boolean} [ignoreDisables]
7679
* @property {any} [ignorePath]
7780
* @property {string} [outputFile]
@@ -236,6 +239,10 @@ const meowOptions = {
236239
--allow-empty-input, --aei
237240
238241
When glob pattern matches no files, the process will exit without throwing an error.
242+
243+
--globby-options, --go
244+
245+
Options in JSON format passed to globby.
239246
`,
240247
flags: {
241248
allowEmptyInput: {
@@ -337,6 +344,10 @@ const meowOptions = {
337344
alias: 'v',
338345
type: 'boolean',
339346
},
347+
globbyOptions: {
348+
alias: 'go',
349+
type: 'string',
350+
},
340351
},
341352
};
342353

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

411+
if (cli.flags.globbyOptions) {
412+
try {
413+
optionsBase.globbyOptions = await parseGlobbyOptions(cli.flags.globbyOptions);
414+
} catch (error) {
415+
if (typeof error === 'string') {
416+
process.stderr.write(`${error}${EOL}`);
417+
process.exitCode = EXIT_CODE_ERROR;
418+
419+
return;
420+
}
421+
422+
throw error;
423+
}
424+
}
425+
400426
if (cli.flags.stdinFilename) {
401427
optionsBase.codeFilename = cli.flags.stdinFilename;
402428
}
@@ -542,6 +568,30 @@ function handleError(err) {
542568
process.exitCode = exitCode;
543569
}
544570

571+
/**
572+
* @param {string} value
573+
* @returns {Promise<Record<string, unknown>>}
574+
*/
575+
function parseGlobbyOptions(value) {
576+
const errorMessage = () =>
577+
`Invalid option ${red('"--globby-options"')}.` +
578+
` The value ${red(`"${value}"`)} is not valid JSON object.`;
579+
580+
let options;
581+
582+
try {
583+
options = JSON.parse(value);
584+
} catch (_) {
585+
return Promise.reject(errorMessage());
586+
}
587+
588+
if (isPlainObject(options)) {
589+
return Promise.resolve(options);
590+
}
591+
592+
return Promise.reject(errorMessage());
593+
}
594+
545595
/**
546596
* @param {string[]} argv
547597
* @returns {CLIOptions}

0 commit comments

Comments
 (0)