-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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): change stderr to not respect --quiet * fix(cli): change stdout to stderr in isTerminal * fix(tests): add STD(OUT/ERR)_IS_TTY env variables these variables can be set to force output on the specified chanel as if it were connected to a terminal. macro.testCLI can now take an argument of the following form: isTerminal: { stderr: false, stdout: false }, * fix(cli): improve output of CLI when not a tty * test: added tests for --verson and --help * test: add tests for --quiet * test: include isTerminal in snapshot * test: add tests for TTY detection and integration * test: typo, stderr --> stdout * fix(cli): exit code while sort is number of failed The exit code is now the number of files which failed when sorting. On a successful run, this will be 0 and exit w/ success, but if any errors occur, this will be incremented. In check mode, this is fails + unsorted. * fix: wrap file operation in try catch Wraps fs calls in trycatch to not throw and to count failures. Also better messages and script usage * docs: document changes to tty-based output * fix(test): compatability w/ node v14 * fix: compatability with node 12 * test: add tests for improper usage * test: support for node 12&14 in error test * refactor: remove extra changes to improve diff * revert: remove all tty detection * refactor: update to meet upstream * fix: fixes error reporting with quiet * fix: bad merge * style: prettier * fix: fixes permissions on cli.js * typo * refactor: improve exit code handling, and set 2 on error * feat: added summary at end of tool run * fix: better show that output on error, is an error * refactor: cleaner logic * refactor: pass `files` to `constructor` * refactor: save `matchedFilesCount` to status * fix: remove `0 files`, use `1 file` instead of `1 files` * refactor: extract `Reporter` --------- Co-authored-by: Keith Cirkel <[email protected]> Co-authored-by: fisker Cheung <[email protected]>
- Loading branch information
1 parent
7be9d3a
commit 6b1c114
Showing
6 changed files
with
330 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
const getFilesCountText = (count) => (count === 1 ? '1 file' : `${count} files`) | ||
|
||
class Reporter { | ||
#hasPrinted = false | ||
#options | ||
#status | ||
#logger | ||
|
||
constructor(files, options) { | ||
this.#options = options | ||
this.#status = { | ||
matchedFilesCount: files.length, | ||
failedFilesCount: 0, | ||
wellSortedFilesCount: 0, | ||
changedFilesCount: 0, | ||
} | ||
|
||
this.#logger = options.shouldBeQuiet | ||
? { log() {}, error() {} } | ||
: { | ||
log: (...args) => { | ||
this.#hasPrinted = true | ||
console.log(...args) | ||
}, | ||
error: (...args) => { | ||
this.#hasPrinted = true | ||
console.error(...args) | ||
}, | ||
} | ||
} | ||
|
||
// The file is well-sorted | ||
reportNotChanged(/* file */) { | ||
this.#status.wellSortedFilesCount++ | ||
} | ||
|
||
reportChanged(file) { | ||
this.#status.changedFilesCount++ | ||
this.#logger.log(this.#options.isCheck ? `${file}` : `${file} is sorted!`) | ||
} | ||
|
||
reportFailed(file, error) { | ||
this.#status.failedFilesCount++ | ||
|
||
console.error('Error on: ' + file) | ||
this.#logger.error(error.message) | ||
} | ||
|
||
printSummary() { | ||
const { | ||
matchedFilesCount, | ||
failedFilesCount, | ||
changedFilesCount, | ||
wellSortedFilesCount, | ||
} = this.#status | ||
|
||
if (matchedFilesCount === 0) { | ||
console.error('No matching files.') | ||
process.exitCode = 2 | ||
return | ||
} | ||
|
||
const { isCheck, isQuiet } = this.#options | ||
|
||
if (isCheck && changedFilesCount) { | ||
process.exitCode = 1 | ||
} | ||
|
||
if (failedFilesCount) { | ||
process.exitCode = 2 | ||
} | ||
|
||
if (isQuiet) { | ||
return | ||
} | ||
|
||
const { log } = this.#logger | ||
|
||
// Print an empty line. | ||
if (this.#hasPrinted) { | ||
log() | ||
} | ||
|
||
// Matched files | ||
log('Found %s.', getFilesCountText(matchedFilesCount)) | ||
|
||
// Failed files | ||
if (failedFilesCount) { | ||
log( | ||
'%s could not be %s.', | ||
getFilesCountText(failedFilesCount), | ||
isCheck ? 'checked' : 'sorted', | ||
) | ||
} | ||
|
||
// Changed files | ||
if (changedFilesCount) { | ||
if (isCheck) { | ||
log( | ||
'%s %s not sorted.', | ||
getFilesCountText(changedFilesCount), | ||
changedFilesCount === 1 ? 'was' : 'were', | ||
) | ||
} else { | ||
log('%s successfully sorted.', getFilesCountText(changedFilesCount)) | ||
} | ||
} | ||
|
||
// Well-sorted files | ||
if (wellSortedFilesCount) { | ||
log( | ||
'%s %s already sorted.', | ||
getFilesCountText(wellSortedFilesCount), | ||
wellSortedFilesCount === 1 ? 'was' : 'were', | ||
) | ||
} | ||
} | ||
} | ||
|
||
export default Reporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.