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

Merged
merged 3 commits into from
Mar 4, 2025
Merged
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
32 changes: 17 additions & 15 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env node
import fs from 'node:fs'
import fs from 'node:fs/promises'
import streamConsumers from 'node:stream/consumers'
import { parseArgs } from 'node:util'
import { globSync } from 'tinyglobby'
import { glob } from 'tinyglobby'
import sortPackageJson from './index.js'
import Reporter from './reporter.js'
import packageJson from './package.json' with { type: 'json' }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I move this to showVersion?


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

console.log(`${name} ${version}`)
}
Expand Down Expand Up @@ -57,33 +56,36 @@ function parseCliArguments() {
return { options, patterns }
}

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, { ignore, ...options }) {
const files = globSync(patterns, { ignore })
async function sortPackageJsonFiles(patterns, { ignore, ...options }) {
const files = await glob(patterns, { ignore })
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original implmention I want to use globStream from globby, but tinyglobby doesn't support.

SuperchupuDev/tinyglobby#24


const reporter = new Reporter(files, options)
const reporter = new Reporter(options)
const { isCheck } = options

for (const file of files) {
reporter.reportFound(file)

try {
sortPackageJsonFile(file, reporter, isCheck)
await sortPackageJsonFile(file, reporter, isCheck)
} catch (error) {
reporter.reportFailed(file, error)
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still running serially, hard to make test pass if switch to parallel, I guess we can revist later.


reporter.printSummary()
}

Expand All @@ -93,7 +95,7 @@ async function sortPackageJsonFromStdin() {
)
}

function run() {
async function run() {
let options, patterns
try {
;({ options, patterns } = parseCliArguments())
Expand Down Expand Up @@ -121,11 +123,11 @@ function run() {
return sortPackageJsonFromStdin()
}

sortPackageJsonFiles(patterns, {
await sortPackageJsonFiles(patterns, {
ignore: options.ignore,
isCheck: options.check,
shouldBeQuiet: options.quiet,
})
}

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