Skip to content

Commit 0bf1155

Browse files
spamshakerMichał Grzegorzewski
andauthored
feat: added support for ignore pattern to cli (#329)
* feat: added support for ignore pattern to cli - defaults to node_modules resolves #328 * feat: added support for ignore pattern to cli - CR Fixes resolves #328 * feat: added support for ignore pattern to cli - Fix snapshots resolves #328 * feat: added support for ignore pattern to cli - Little fix for ordering of parameters resolves #328 --------- Co-authored-by: Michał Grzegorzewski <michal@ria-develop.pl>
1 parent 57aebe2 commit 0bf1155

9 files changed

Lines changed: 50 additions & 5 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ CLI also supports multi file paths or [`glob`](https://github.com/sindresorhus/g
5959
$ sort-package-json "my-package/package.json" "other-package/package.json"
6060

6161
$ sort-package-json "package.json" "packages/*/package.json"
62+
63+
$ sort-package-json "package.json" "packages/*/package.json" --ignore "packages/one-package"
6264
```
6365

6466
#### `--check` flag

cli.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ If file/glob is omitted, './package.json' file will be processed.
2323
-c, --check Check if files are sorted
2424
-q, --quiet Don't output success messages
2525
-h, --help Display this help
26+
-i, --ignore An array of glob patterns to ignore
2627
-v, --version Display the package version
2728
--stdin Read package.json from stdin
2829
`,
@@ -43,8 +44,8 @@ function sortPackageJsonFile(file, reporter, isCheck) {
4344
reporter.reportChanged(file)
4445
}
4546

46-
function sortPackageJsonFiles(patterns, options) {
47-
const files = globSync(patterns)
47+
function sortPackageJsonFiles(patterns, { ignore, ...options }) {
48+
const files = globSync(patterns, { ignore })
4849
const reporter = new Reporter(files, options)
4950
const { isCheck } = options
5051

@@ -55,7 +56,6 @@ function sortPackageJsonFiles(patterns, options) {
5556
reporter.reportFailed(file, error)
5657
}
5758
}
58-
5959
reporter.printSummary()
6060
}
6161

@@ -64,7 +64,10 @@ async function sortPackageJsonFromStdin() {
6464
}
6565

6666
function run() {
67-
const cliArguments = process.argv.slice(2)
67+
const cliArguments = process.argv
68+
.slice(2)
69+
.map((arg) => arg.split('='))
70+
.flat()
6871

6972
if (
7073
cliArguments.some((argument) => argument === '--help' || argument === '-h')
@@ -85,14 +88,23 @@ function run() {
8588
}
8689

8790
const patterns = []
91+
const ignore = []
8892
let isCheck = false
8993
let shouldBeQuiet = false
9094

95+
let lastArg
9196
for (const argument of cliArguments) {
97+
if (lastArg === '--ignore' || lastArg === '-i') {
98+
ignore.push(argument)
99+
lastArg = undefined
100+
continue
101+
}
92102
if (argument === '--check' || argument === '-c') {
93103
isCheck = true
94104
} else if (argument === '--quiet' || argument === '-q') {
95105
shouldBeQuiet = true
106+
} else if (argument === '--ignore' || argument === '-i') {
107+
lastArg = argument
96108
} else {
97109
patterns.push(argument)
98110
}
@@ -102,7 +114,11 @@ function run() {
102114
patterns[0] = 'package.json'
103115
}
104116

105-
sortPackageJsonFiles(patterns, { isCheck, shouldBeQuiet })
117+
if (!ignore.length) {
118+
ignore[0] = 'node_modules'
119+
}
120+
121+
sortPackageJsonFiles(patterns, { ignore, isCheck, shouldBeQuiet })
106122
}
107123

108124
run()

tests/cli.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,8 @@ test('run `cli --stdin` with input from stdin with \\r\\n', macro.testCLI, {
500500
message: 'The line feed should be CRLF in output',
501501
stdin: `{\r\n "description": "Description",\r\n "name": "Name"\r\n}\r\n`,
502502
})
503+
504+
test('run `cli --ignore=abc`', macro.testCLI, {
505+
args: ['--ignore=abc'],
506+
message: 'Should not fail on adding ignore pattern',
507+
})

tests/snapshots/cli.js.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Generated by [AVA](https://avajs.dev).
2424
-c, --check Check if files are sorted␊
2525
-q, --quiet Don't output success messages␊
2626
-h, --help Display this help␊
27+
-i, --ignore An array of glob patterns to ignore␊
2728
-v, --version Display the package version␊
2829
--stdin Read package.json from stdin␊
2930
@@ -52,6 +53,7 @@ Generated by [AVA](https://avajs.dev).
5253
-c, --check Check if files are sorted␊
5354
-q, --quiet Don't output success messages␊
5455
-h, --help Display this help␊
56+
-i, --ignore An array of glob patterns to ignore␊
5557
-v, --version Display the package version␊
5658
--stdin Read package.json from stdin␊
5759
@@ -79,6 +81,7 @@ Generated by [AVA](https://avajs.dev).
7981
-c, --check Check if files are sorted␊
8082
-q, --quiet Don't output success messages␊
8183
-h, --help Display this help␊
84+
-i, --ignore An array of glob patterns to ignore␊
8285
-v, --version Display the package version␊
8386
--stdin Read package.json from stdin␊
8487
@@ -107,6 +110,7 @@ Generated by [AVA](https://avajs.dev).
107110
-c, --check Check if files are sorted␊
108111
-q, --quiet Don't output success messages␊
109112
-h, --help Display this help␊
113+
-i, --ignore An array of glob patterns to ignore␊
110114
-v, --version Display the package version␊
111115
--stdin Read package.json from stdin␊
112116
@@ -205,6 +209,7 @@ Generated by [AVA](https://avajs.dev).
205209
-c, --check Check if files are sorted␊
206210
-q, --quiet Don't output success messages␊
207211
-h, --help Display this help␊
212+
-i, --ignore An array of glob patterns to ignore␊
208213
-v, --version Display the package version␊
209214
--stdin Read package.json from stdin␊
210215
@@ -1409,3 +1414,20 @@ Generated by [AVA](https://avajs.dev).
14091414
`,
14101415
},
14111416
}
1417+
1418+
## run `cli --ignore=abc`
1419+
1420+
> Should not fail on adding ignore pattern
1421+
1422+
{
1423+
args: [
1424+
'--ignore=abc',
1425+
],
1426+
fixtures: [],
1427+
result: {
1428+
errorCode: 2,
1429+
stderr: `No matching files.␊
1430+
`,
1431+
stdout: '',
1432+
},
1433+
}

tests/snapshots/cli.js.snap

129 Bytes
Binary file not shown.

tests/snapshots/eslint.js.snap

-20 Bytes
Binary file not shown.

tests/snapshots/fields.js.snap

0 Bytes
Binary file not shown.

tests/snapshots/main.js.snap

-7 Bytes
Binary file not shown.

tests/snapshots/prettier.js.snap

8 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)