Skip to content

refactor: replace read-pkg with native fs.readFile + JSON.parse#4742

Merged
escapedcat merged 1 commit into
masterfrom
chore/remove-read-pkg
May 2, 2026
Merged

refactor: replace read-pkg with native fs.readFile + JSON.parse#4742
escapedcat merged 1 commit into
masterfrom
chore/remove-read-pkg

Conversation

@escapedcat

Copy link
Copy Markdown
Member

read-pkg is used in one place (@packages/utils/pkg-check.js, an internal dev tool). Replacing it with a 3-line helper using node:fs/promises.readFile + JSON.parse lets us drop the dep without losing functionality. Closes #4673 as superseded.

read-pkg is used in one place (@packages/utils/pkg-check.js, an
internal dev tool). Replacing it with a 3-line helper using
node:fs/promises.readFile + JSON.parse lets us drop the dep without
losing functionality. Closes #4673 as superseded.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@qodo-code-review

Copy link
Copy Markdown

Review Summary by Qodo

Remove read-pkg dependency with native fs implementation

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Replace read-pkg dependency with native Node.js fs/promises API
• Add inline readPkg() helper using readFile() and JSON.parse()
• Remove read-pkg from package.json dependencies
• Simplify package.json call from object to string parameter
Diagram
flowchart LR
  A["read-pkg dependency"] -->|"Replace with"| B["readFile + JSON.parse"]
  B -->|"Implement as"| C["readPkg helper function"]
  C -->|"Update call in"| D["main function"]
  D -->|"Remove from"| E["package.json dependencies"]
Loading

Grey Divider

File Changes

1. @packages/utils/pkg-check.js ✨ Enhancement +7/-2

Replace read-pkg with native fs implementation

• Replace read-pkg import with readFile from node:fs/promises
• Add new readPkg(cwd) async helper function that reads and parses package.json
• Update main() function call from readPkg({ cwd }) to readPkg(cwd)
• Maintain same functionality with native Node.js APIs

@packages/utils/pkg-check.js


2. @packages/utils/package.json Dependencies +0/-1

Remove read-pkg dependency

• Remove read-pkg from dependencies list
• Reduce external dependency count by one

@packages/utils/package.json


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented May 2, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0)

Grey Divider


Remediation recommended

1. Opaque JSON parse errors 🐞 Bug ◔ Observability
Description
readPkg() calls JSON.parse() directly, so parse failures don’t include which package.json path was
being read. The top-level promise .catch rethrows the error without adding context, making
diagnostics harder.
Code

@packages/utils/pkg-check.js[R213-216]

+async function readPkg(cwd) {
+	const data = await readFile(path.join(cwd, "package.json"), "utf8");
+	return JSON.parse(data);
+}
Evidence
The new helper parses package.json via JSON.parse without wrapping errors or annotating them with
the file path; any resulting SyntaxError is propagated up to the top-level .catch which rethrows
as-is.

@packages/utils/pkg-check.js[213-216]
@packages/utils/pkg-check.js[124-141]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`readPkg(cwd)` reads and parses `package.json` with `JSON.parse(data)` directly. If parsing fails, the thrown error message typically won’t identify which file path was being parsed, and the top-level `.catch()` rethrows without adding that context.

### Issue Context
This script is a CLI utility (`pkg-check.js`). When it fails due to malformed JSON, the error should clearly identify the exact `package.json` file path to speed up debugging.

### Fix Focus Areas
- @packages/utils/pkg-check.js[213-216]
- @packages/utils/pkg-check.js[124-141]

### Suggested change
Wrap the parse in a `try/catch` and rethrow an error that includes the resolved `package.json` path (optionally using the `cause` option).

Example:
```js
async function readPkg(cwd) {
 const file = path.join(cwd, "package.json");
 const data = await readFile(file, "utf8");
 try {
   return JSON.parse(data);
 } catch (err) {
   // preserve stack/cause but add file context
   throw new Error(`Failed to parse ${file}: ${err.message}`, { cause: err });
 }
}
```

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Refactors the internal @commitlint/utils dev tool (pkg-check) to stop using read-pkg by reading package.json via native fs/promises.readFile and JSON.parse, allowing removal of the direct read-pkg dependency (superseding #4673).

Changes:

  • Replace read-pkg usage in @packages/utils/pkg-check.js with a small local readPkg(cwd) helper using readFile + JSON.parse.
  • Remove read-pkg from @packages/utils dependencies.
  • Update yarn.lock to drop the removed direct dependency subtree.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
@packages/utils/pkg-check.js Replaces read-pkg call sites with a local JSON reader helper.
@packages/utils/package.json Removes the direct read-pkg dependency from this workspace package.
yarn.lock Removes lockfile entries associated with the deleted direct dependency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread @packages/utils/pkg-check.js
@escapedcat escapedcat merged commit 071a3b7 into master May 2, 2026
19 checks passed
@escapedcat escapedcat deleted the chore/remove-read-pkg branch May 2, 2026 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants