Skip to content

Commit

Permalink
feat(cli): add --quiet / -q option
Browse files Browse the repository at this point in the history
Stops any successful output. Errors are still outputted.
  • Loading branch information
aarondill committed Jan 21, 2023
1 parent 3b32e04 commit 209a3bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ $ sort-package-json "**/package.json" --check
# 2 of 5 matched files are not sorted.
```

#### `--quiet` flag

In order to silence any successful output, you can run CLI with the `--quiet` flag (or `-q`). This will stop the CLI from outputting if it runs successfully, but will still display errors if they occur. Exit codes will not change.

```bash
$ sort-package-json "**/package.json" --check --quiet
$ sort-package-json "**/package.json" --quiet
```

## API

### Install
Expand Down Expand Up @@ -137,7 +146,7 @@ If an array, sort keys in ordering of `options.sortOrder`.
```js
const sorted = sortPackageJson(packageJsonObject, {
sortOrder: ['version']
sortOrder: ['version'],
})

console.log(Object.keys(sorted))
Expand All @@ -153,7 +162,7 @@ If a function, sort fields by [Array#sort(options.sortOrder)](https://developer.
const sorted = sortPackageJson(packageJsonObject, {
sortOrder(left, right) {
return left.localeCompare(right)
}
},
})

console.log(Object.keys(sorted))
Expand Down Expand Up @@ -235,4 +244,3 @@ A lot of people who ask for configuration cite the use case that they simply don
### What?! Why would you want to do this?!
Well, it's nice to have the keys of a package.json in a well sorted order. Almost everyone would agree having "name" at the top of a package.json is sensible (rather than sorted alphabetically or somewhere silly like the bottom), so why not the rest of the package.json?
6 changes: 5 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env node
import fs from 'node:fs'
import { globbySync } from 'globby'
import fs from 'node:fs'
import sortPackageJson from './index.js'

const isCheckFlag = (argument) => argument === '--check' || argument === '-c'
const isQuietFlag = (argument) => argument === '--quiet' || argument === '-q'

const cliArguments = process.argv.slice(2)
const isCheck = cliArguments.some(isCheckFlag)
const isQuiet = cliArguments.some(isQuietFlag)

const patterns = cliArguments.filter((argument) => !isCheckFlag(argument))

Expand All @@ -21,6 +23,8 @@ if (files.length === 0) {
process.exit(1)
}

if (isQuiet) console.log = function () {}

let notSortedFiles = 0

files.forEach((file) => {
Expand Down

0 comments on commit 209a3bd

Please sign in to comment.