Skip to content

Commit

Permalink
feat: added support for ignore pattern to cli
Browse files Browse the repository at this point in the history
- defaults to node_modules

resolves #328
  • Loading branch information
Michał Grzegorzewski committed Nov 23, 2024
1 parent 57aebe2 commit 174d431
Show file tree
Hide file tree
Showing 9 changed files with 59 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
35 changes: 30 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,28 +44,40 @@ function sortPackageJsonFile(file, reporter, isCheck) {
reporter.reportChanged(file)
}

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

performance.mark('sorting')
for (const file of files) {
try {
sortPackageJsonFile(file, reporter, isCheck)
} catch (error) {
reporter.reportFailed(file, error)
}
}

performance.mark('finish')
reporter.printSummary()
if (process.env.DEBUG) {
console.log([
performance.measure('Glob Search', 'start', 'sorting'),
performance.measure('Sorting', 'sorting', 'finish'),
performance.measure('Total', 'start', 'finish'),
])
}
}

async function sortPackageJsonFromStdin() {
process.stdout.write(sortPackageJson(await getStdin()))
}

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 +98,22 @@ 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)
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 +123,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`

> Ignore pattern should be recognized
{
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 174d431

Please sign in to comment.