Skip to content

Commit

Permalink
feat(cli): add --quiet / -q option (#281)
Browse files Browse the repository at this point in the history
* feat(cli): add --quiet / -q option

Stops any successful output. Errors are still outputted.

* refactor(cli): create output functions

This commit creates stdout and stderr functions for outputting with
respect to isQuiet and adds support for differing (easier to parse)
messages if not connected to a TTY (eg. a pipe or process substitution)

Additionally changes the 'No matching files.' message to output on stderr
rather than on stdout as it was, which makes sense because this output
represents erroneous usage.

* fix(cli): ensure --quiet isn't used as a pattern

--help and --version don't need to be checked because they
exit the program before reaching this point.

* fix(cli): add --quiet to the help

* fix(cli): change stderr to not respect --quiet

* fix(cli): change stdout to stderr in isTerminal

* refactor(cli): simplify output functions

* refactor(cli): remove call to array.filter

* test: add tests for version, help, and quiet
  • Loading branch information
aarondill committed Jan 30, 2023
1 parent dc1ad5a commit f268402
Show file tree
Hide file tree
Showing 5 changed files with 999 additions and 60 deletions.
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
23 changes: 16 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ 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)

const stdout = isQuiet ? () => {} : console.log
const stderr = console.error

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

Expand All @@ -20,6 +26,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 +42,9 @@ if (isVersion) {
process.exit(0)
}

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

if (!patterns.length) {
patterns[0] = 'package.json'
Expand All @@ -44,7 +53,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 +66,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

0 comments on commit f268402

Please sign in to comment.