diff --git a/README.md b/README.md index 2ffc0b8..ed7703e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cli.js b/cli.js index 91e63d6..ef3c9b1 100755 --- a/cli.js +++ b/cli.js @@ -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 `, @@ -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 @@ -55,7 +56,6 @@ function sortPackageJsonFiles(patterns, options) { reporter.reportFailed(file, error) } } - reporter.printSummary() } @@ -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') @@ -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) } @@ -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() diff --git a/tests/cli.js b/tests/cli.js index d68ee29..a4234d1 100644 --- a/tests/cli.js +++ b/tests/cli.js @@ -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', +}) diff --git a/tests/snapshots/cli.js.md b/tests/snapshots/cli.js.md index 8fcaed1..160f177 100644 --- a/tests/snapshots/cli.js.md +++ b/tests/snapshots/cli.js.md @@ -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␊ ␊ @@ -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␊ ␊ @@ -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␊ ␊ @@ -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␊ ␊ @@ -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␊ ␊ @@ -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: '', + }, + } diff --git a/tests/snapshots/cli.js.snap b/tests/snapshots/cli.js.snap index 87200f1..077c58d 100644 Binary files a/tests/snapshots/cli.js.snap and b/tests/snapshots/cli.js.snap differ diff --git a/tests/snapshots/eslint.js.snap b/tests/snapshots/eslint.js.snap index 4d7a898..4ecd658 100644 Binary files a/tests/snapshots/eslint.js.snap and b/tests/snapshots/eslint.js.snap differ diff --git a/tests/snapshots/fields.js.snap b/tests/snapshots/fields.js.snap index 7c58082..1f4b8d8 100644 Binary files a/tests/snapshots/fields.js.snap and b/tests/snapshots/fields.js.snap differ diff --git a/tests/snapshots/main.js.snap b/tests/snapshots/main.js.snap index 99888a0..6d8165d 100644 Binary files a/tests/snapshots/main.js.snap and b/tests/snapshots/main.js.snap differ diff --git a/tests/snapshots/prettier.js.snap b/tests/snapshots/prettier.js.snap index 4561efa..7ae48bc 100644 Binary files a/tests/snapshots/prettier.js.snap and b/tests/snapshots/prettier.js.snap differ