Skip to content

Comments

refactor(index): use path.dirname over regex to resolve bin path#548

Open
Fdawgs wants to merge 2 commits intomainfrom
refactor/path
Open

refactor(index): use path.dirname over regex to resolve bin path#548
Fdawgs wants to merge 2 commits intomainfrom
refactor/path

Conversation

@Fdawgs
Copy link
Owner

@Fdawgs Fdawgs commented Feb 24, 2026

Purpose-built path utility should be much more robust than my regex!

Checklist

Copilot AI review requested due to automatic review settings February 24, 2026 18:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors how the UnRTF binary installation directory is derived from which/where output by using Node’s path.dirname() instead of a custom regex, aiming for more robust path handling.

Changes:

  • Import dirname from node:path.
  • Remove the cached UNRTF_PATH_REG regex used to parse which/where output.
  • Compute #unrtfPath via dirname() of trimmed which/where stdout, and adjust the win32 fallback condition accordingly.

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

Comment on lines +219 to 224
])
.stdout.toString()
.trim();
if (which) {
this.#unrtfPath = dirname(which);
}
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

if (which) { this.#unrtfPath = dirname(which); } will also treat any non-empty diagnostic message on stdout as a path (e.g., some where/which failure outputs), resulting in dirname(...) === "." and a misleading later "Unable to determine UnRTF version" error. Consider gating this assignment on spawnSync(...).status === 0 and/or validating that the selected line is an absolute/expected executable path before setting #unrtfPath.

Copilot uses AI. Check for mistakes.
Comment on lines 217 to +227
const which = spawnSync(platform === "win32" ? "where" : "which", [
"unrtf",
]).stdout.toString();
const unrtfPath = UNRTF_PATH_REG.exec(which)?.[1];

if (unrtfPath) {
this.#unrtfPath = unrtfPath;
])
.stdout.toString()
.trim();
if (which) {
this.#unrtfPath = dirname(which);
}

/* istanbul ignore next: requires specific OS */
if (platform === "win32" && !unrtfPath) {
if (platform === "win32" && !this.#unrtfPath) {
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

The constructor logic now depends on parsing where/which output with trimming + dirname(), but the existing tests only cover empty stdout and a single-path stdout. Add a unit test that mocks win32 where returning multiple lines and/or a non-empty "not found" message, to ensure #unrtfPath is set (or remains empty to trigger the win32 fallback) correctly.

Copilot uses AI. Check for mistakes.
Comment on lines 217 to 224
const which = spawnSync(platform === "win32" ? "where" : "which", [
"unrtf",
]).stdout.toString();
const unrtfPath = UNRTF_PATH_REG.exec(which)?.[1];

if (unrtfPath) {
this.#unrtfPath = unrtfPath;
])
.stdout.toString()
.trim();
if (which) {
this.#unrtfPath = dirname(which);
}
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

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

where on Windows can return multiple matching paths (one per line). Calling dirname() on the raw multi-line string will produce an invalid path and can prevent the win32 fallback from running. Consider splitting stdout by newlines and selecting the first valid path (or the first line that ends with unrtf/unrtf.exe) before applying dirname().

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant