Skip to content

Commit

Permalink
feat: added support for ignore pattern to cli (#329)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
spamshaker and Michał Grzegorzewski authored Nov 23, 2024
1 parent 57aebe2 commit 0bf1155
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ CLI also supports multi file paths or [`glob`](https://github.com/sindresorhus/g
$ sort-package-json "my-package/package.json" "other-package/package.json"

$ sort-package-json "package.json" "packages/*/package.json"

$ sort-package-json "package.json" "packages/*/package.json" --ignore "packages/one-package"
```

#### `--check` flag
Expand Down
26 changes: 21 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ If file/glob is omitted, './package.json' file will be processed.
-c, --check Check if files are sorted
-q, --quiet Don't output success messages
-h, --help Display this help
-i, --ignore An array of glob patterns to ignore
-v, --version Display the package version
--stdin Read package.json from stdin
`,
Expand All @@ -43,8 +44,8 @@ function sortPackageJsonFile(file, reporter, isCheck) {
reporter.reportChanged(file)
}

function sortPackageJsonFiles(patterns, options) {
const files = globSync(patterns)
function sortPackageJsonFiles(patterns, { ignore, ...options }) {
const files = globSync(patterns, { ignore })
const reporter = new Reporter(files, options)
const { isCheck } = options

Expand All @@ -55,7 +56,6 @@ function sortPackageJsonFiles(patterns, options) {
reporter.reportFailed(file, error)
}
}

reporter.printSummary()
}

Expand All @@ -64,7 +64,10 @@ async function sortPackageJsonFromStdin() {
}

function run() {
const cliArguments = process.argv.slice(2)
const cliArguments = process.argv
.slice(2)
.map((arg) => arg.split('='))
.flat()

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

const patterns = []
const ignore = []
let isCheck = false
let shouldBeQuiet = false

let lastArg
for (const argument of cliArguments) {
if (lastArg === '--ignore' || lastArg === '-i') {
ignore.push(argument)
lastArg = undefined
continue
}
if (argument === '--check' || argument === '-c') {
isCheck = true
} else if (argument === '--quiet' || argument === '-q') {
shouldBeQuiet = true
} else if (argument === '--ignore' || argument === '-i') {
lastArg = argument
} else {
patterns.push(argument)
}
Expand All @@ -102,7 +114,11 @@ function run() {
patterns[0] = 'package.json'
}

sortPackageJsonFiles(patterns, { isCheck, shouldBeQuiet })
if (!ignore.length) {
ignore[0] = 'node_modules'
}

sortPackageJsonFiles(patterns, { ignore, isCheck, shouldBeQuiet })
}

run()
5 changes: 5 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,8 @@ test('run `cli --stdin` with input from stdin with \\r\\n', macro.testCLI, {
message: 'The line feed should be CRLF in output',
stdin: `{\r\n "description": "Description",\r\n "name": "Name"\r\n}\r\n`,
})

test('run `cli --ignore=abc`', macro.testCLI, {
args: ['--ignore=abc'],
message: 'Should not fail on adding ignore pattern',
})
22 changes: 22 additions & 0 deletions tests/snapshots/cli.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Generated by [AVA](https://avajs.dev).
-c, --check Check if files are sorted␊
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-i, --ignore An array of glob patterns to ignore␊
-v, --version Display the package version␊
--stdin Read package.json from stdin␊
Expand Down Expand Up @@ -52,6 +53,7 @@ Generated by [AVA](https://avajs.dev).
-c, --check Check if files are sorted␊
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-i, --ignore An array of glob patterns to ignore␊
-v, --version Display the package version␊
--stdin Read package.json from stdin␊
Expand Down Expand Up @@ -79,6 +81,7 @@ Generated by [AVA](https://avajs.dev).
-c, --check Check if files are sorted␊
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-i, --ignore An array of glob patterns to ignore␊
-v, --version Display the package version␊
--stdin Read package.json from stdin␊
Expand Down Expand Up @@ -107,6 +110,7 @@ Generated by [AVA](https://avajs.dev).
-c, --check Check if files are sorted␊
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-i, --ignore An array of glob patterns to ignore␊
-v, --version Display the package version␊
--stdin Read package.json from stdin␊
Expand Down Expand Up @@ -205,6 +209,7 @@ Generated by [AVA](https://avajs.dev).
-c, --check Check if files are sorted␊
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-i, --ignore An array of glob patterns to ignore␊
-v, --version Display the package version␊
--stdin Read package.json from stdin␊
Expand Down Expand Up @@ -1409,3 +1414,20 @@ Generated by [AVA](https://avajs.dev).
`,
},
}

## run `cli --ignore=abc`

> Should not fail on adding ignore pattern
{
args: [
'--ignore=abc',
],
fixtures: [],
result: {
errorCode: 2,
stderr: `No matching files.␊
`,
stdout: '',
},
}
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.
Binary file modified tests/snapshots/eslint.js.snap
Binary file not shown.
Binary file modified tests/snapshots/fields.js.snap
Binary file not shown.
Binary file modified tests/snapshots/main.js.snap
Binary file not shown.
Binary file modified tests/snapshots/prettier.js.snap
Binary file not shown.

0 comments on commit 0bf1155

Please sign in to comment.