-
-
Notifications
You must be signed in to change notification settings - Fork 2
refactor(index): use path.dirname over regex to resolve bin path
#548
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
|
|
||
| const { spawn, spawnSync } = require("node:child_process"); | ||
| const { open } = require("node:fs/promises"); | ||
| const { normalize, resolve: pathResolve } = require("node:path"); | ||
| const { dirname, normalize, resolve: pathResolve } = require("node:path"); | ||
| const { platform } = require("node:process"); | ||
| const freeze = require("ice-barrage"); | ||
| const { gt, lt } = require("semver"); | ||
|
|
@@ -19,7 +19,6 @@ const RTF_MAGIC_BUFFER = Buffer.from(RTF_MAGIC_NUMBER); | |
| const RTF_MAGIC_NUMBER_LENGTH = RTF_MAGIC_NUMBER.length; | ||
|
|
||
| // Cache immutable regex as they are expensive to create and garbage collect | ||
| const UNRTF_PATH_REG = /(.+)unrtf/u; | ||
| // UnRTF version output is inconsistent between versions but always starts with the semantic version number | ||
| const UNRTF_VERSION_REG = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/u; | ||
|
|
||
|
|
@@ -218,15 +217,15 @@ class UnRTF { | |
| /* istanbul ignore next: requires specific OS */ | ||
| 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); | ||
| } | ||
|
Comment on lines
218
to
225
|
||
|
|
||
| /* istanbul ignore next: requires specific OS */ | ||
| if (platform === "win32" && !unrtfPath) { | ||
| if (platform === "win32" && !this.#unrtfPath) { | ||
|
Comment on lines
218
to
+228
|
||
| try { | ||
| // @ts-ignore: Optional dependency | ||
| // eslint-disable-next-line n/global-require -- Conditional require | ||
|
|
||
There was a problem hiding this comment.
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., somewhere/whichfailure outputs), resulting indirname(...) === "."and a misleading later "Unable to determine UnRTF version" error. Consider gating this assignment onspawnSync(...).status === 0and/or validating that the selected line is an absolute/expected executable path before setting#unrtfPath.