Skip to content

Commit

Permalink
refactor: extract Reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 1, 2023
1 parent b22d474 commit 972be64
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 119 deletions.
119 changes: 1 addition & 118 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { globbySync } from 'globby'
import fs from 'node:fs'
import sortPackageJson from './index.js'
import Reporter from './reporter.js'

function showVersion() {
const { name, version } = JSON.parse(
Expand All @@ -26,124 +27,6 @@ If file/glob is omitted, './package.json' file will be processed.
)
}

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',
)
}
}
}

function sortPackageJsonFile(file, reporter, isCheck) {
const original = fs.readFileSync(file, 'utf8')
const sorted = sortPackageJson(original)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"files": [
"index.js",
"index.d.ts",
"cli.js"
"cli.js",
"reporter.js"
],
"scripts": {
"lint": "eslint .",
Expand Down
120 changes: 120 additions & 0 deletions reporter.js
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

0 comments on commit 972be64

Please sign in to comment.