Skip to content

Commit

Permalink
refactor: make CLI async
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 2, 2023
1 parent c6352d4 commit 502f6f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
31 changes: 16 additions & 15 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node
import { globbySync } from 'globby'
import fs from 'node:fs'
import { globbyStream } from 'globby'
import fs from 'node:fs/promises'
import sortPackageJson from './index.js'
import Reporter from './reporter.js'

function showVersion() {
async function showVersion() {
const { name, version } = JSON.parse(
fs.readFileSync(new URL('package.json', import.meta.url)),
await fs.readFile(new URL('package.json', import.meta.url)),
)

console.log(`${name} ${version}`)
Expand All @@ -27,28 +27,29 @@ If file/glob is omitted, './package.json' file will be processed.
)
}

function sortPackageJsonFile(file, reporter, isCheck) {
const original = fs.readFileSync(file, 'utf8')
async function sortPackageJsonFile(file, reporter, isCheck) {
const original = await fs.readFile(file, 'utf8')
const sorted = sortPackageJson(original)
if (sorted === original) {
return reporter.reportNotChanged(file)
}

if (!isCheck) {
fs.writeFileSync(file, sorted)
await fs.writeFile(file, sorted)
}

reporter.reportChanged(file)
}

function sortPackageJsonFiles(patterns, options) {
const files = globbySync(patterns)
const reporter = new Reporter(files, options)
async function sortPackageJsonFiles(patterns, options) {
const reporter = new Reporter(options)
const { isCheck } = options

for (const file of files) {
for await (const file of globbyStream(patterns)) {
reporter.reportFound(file)

try {
sortPackageJsonFile(file, reporter, isCheck)
await sortPackageJsonFile(file, reporter, isCheck)
} catch (error) {
reporter.reportFailed(file, error)
}
Expand All @@ -57,7 +58,7 @@ function sortPackageJsonFiles(patterns, options) {
reporter.printSummary()
}

function run() {
async function run() {
const cliArguments = process.argv.slice(2)

if (
Expand Down Expand Up @@ -92,7 +93,7 @@ function run() {
patterns[0] = 'package.json'
}

sortPackageJsonFiles(patterns, { isCheck, shouldBeQuiet })
await sortPackageJsonFiles(patterns, { isCheck, shouldBeQuiet })
}

run()
await run()
8 changes: 6 additions & 2 deletions reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class Reporter {
#status
#logger

constructor(files, options) {
constructor(options) {
this.#options = options
this.#status = {
matchedFilesCount: files.length,
matchedFilesCount: 0,
failedFilesCount: 0,
wellSortedFilesCount: 0,
changedFilesCount: 0,
Expand All @@ -29,6 +29,10 @@ class Reporter {
}
}

reportFound(/* file */) {
this.#status.matchedFilesCount++
}

// The file is well-sorted
reportNotChanged(/* file */) {
this.#status.wellSortedFilesCount++
Expand Down

0 comments on commit 502f6f3

Please sign in to comment.