Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add --quiet / -q option #281

Merged
merged 10 commits into from
Jan 30, 2023
9 changes: 9 additions & 0 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
39 changes: 32 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,31 @@ const isCheckFlag = (argument) => argument === '--check' || argument === '-c'
const isHelpFlag = (argument) => argument === '--help' || argument === '-h'
const isVersionFlag = (argument) =>
argument === '--version' || argument === '-v'
const isQuietFlag = (argument) => argument === '--quiet' || argument === '-q'

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

function stdout(outputIfTTY = '', alwaysOutput = outputIfTTY) {
if (isQuiet) return
const isTerminal = !!process.stdout.isTTY
if (isTerminal) {
console.log(outputIfTTY)
} else if (alwaysOutput !== null) {
console.log(alwaysOutput)
}
}
aarondill marked this conversation as resolved.
Show resolved Hide resolved

function stderr(outputIfTTY = '', alwaysOutput = outputIfTTY) {
const isTerminal = !!process.stderr.isTTY
if (isTerminal) {
console.error(outputIfTTY)
} else if (alwaysOutput !== null) {
console.error(alwaysOutput)
}
}

const isHelp = cliArguments.some(isHelpFlag)
const isVersion = cliArguments.some(isVersionFlag)

Expand All @@ -20,6 +42,7 @@ Sort npm package.json files. Default: ./package.json
Strings passed as files are parsed as globs.

-c, --check check if FILES are sorted
-q, --quiet don't output success messages
-h, --help display this help and exit
-v, --version display the version and exit
`,
Expand All @@ -35,7 +58,9 @@ if (isVersion) {
process.exit(0)
}

const patterns = cliArguments.filter((argument) => !isCheckFlag(argument))
const patterns = cliArguments
.filter((argument) => !isCheckFlag(argument))
.filter((argument) => !isQuietFlag(argument))
aarondill marked this conversation as resolved.
Show resolved Hide resolved

if (!patterns.length) {
patterns[0] = 'package.json'
Expand All @@ -44,7 +69,7 @@ if (!patterns.length) {
const files = globbySync(patterns)

if (files.length === 0) {
console.log('No matching files.')
stderr('No matching files.')
process.exit(1)
}

Expand All @@ -57,24 +82,24 @@ files.forEach((file) => {
if (sorted !== packageJson) {
if (isCheck) {
notSortedFiles++
console.log(file)
stdout(file)
} else {
fs.writeFileSync(file, sorted, 'utf8')
console.log(`${file} is sorted!`)
stdout(`${file} is sorted!`)
}
}
})

if (isCheck) {
console.log()
stdout()
if (notSortedFiles) {
console.log(
stdout(
notSortedFiles === 1
? `${notSortedFiles} of ${files.length} matched file is not sorted.`
: `${notSortedFiles} of ${files.length} matched files are not sorted.`,
)
} else {
console.log(
stdout(
files.length === 1
? `${files.length} matched file is sorted.`
: `${files.length} matched files are sorted.`,
Expand Down
8 changes: 4 additions & 4 deletions tests/snapshots/cli.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ Generated by [AVA](https://avajs.dev).
],
result: {
errorCode: 1,
stderr: '',
stdout: `No matching files.␊
stderr: `No matching files.␊
`,
stdout: '',
},
}

Expand Down Expand Up @@ -421,9 +421,9 @@ Generated by [AVA](https://avajs.dev).
],
result: {
errorCode: 1,
stderr: '',
stdout: `No matching files.␊
stderr: `No matching files.␊
`,
stdout: '',
},
}

Expand Down
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.