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

refactor: make CLI async #286

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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