Skip to content

Commit 25dddb2

Browse files
committed
refactor: use vscode-find-up
1 parent 7a75949 commit 25dddb2

6 files changed

Lines changed: 24 additions & 91 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
"vite-tsconfig-paths": "catalog:test",
239239
"vitest": "catalog:test",
240240
"vscode-ext-gen": "catalog:dev",
241+
"vscode-find-up": "catalog:inline",
241242
"yaml": "catalog:inline"
242243
}
243244
}

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ catalogs:
1919
perfect-debounce: ^2.1.0
2020
reactive-vscode: ^1.0.0-beta.2
2121
semver: ^7.7.4
22+
vscode-find-up: ^0.1.1
2223
yaml: ^2.8.2
2324
test:
2425
jest-mock-vscode: ^4.11.0

src/commands/open-file-in-npmx.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { PACKAGE_JSON_BASENAME } from '#constants'
22
import { logger } from '#state'
33
import { npmxFileUrl } from '#utils/links'
4-
import { findNearestFile, resolvePackageJson } from '#utils/resolve'
4+
import { resolvePackageJson } from '#utils/resolve'
55
import { env, Uri, window } from 'vscode'
6+
import { findUp } from 'vscode-find-up'
67

78
export async function openFileInNpmx(fileUri?: Uri) {
89
const textEditor = window.activeTextEditor
@@ -23,7 +24,9 @@ export async function openFileInNpmx(fileUri?: Uri) {
2324
}
2425

2526
// Find the associated package manifest and the relative path to the given file.
26-
const pkgJsonUri = await findNearestFile(PACKAGE_JSON_BASENAME, uri, (u) => u.path.endsWith('/node_modules'))
27+
const pkgJsonUri = await findUp(PACKAGE_JSON_BASENAME, {
28+
cwd: uri,
29+
})
2730
const manifest = pkgJsonUri ? await resolvePackageJson(pkgJsonUri) : undefined
2831
if (!pkgJsonUri || !manifest) {
2932
logger.warn(`Could not resolve npmx url: ${uri.toString()}`)

src/utils/resolve.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,5 @@
1-
import { Uri, workspace } from 'vscode'
2-
3-
export function* walkAncestors(start: Uri, shouldStop?: (uri: Uri) => boolean): Generator<Uri, void, void> {
4-
let current = start
5-
while (true) {
6-
yield current
7-
8-
if (shouldStop?.(current))
9-
return
10-
11-
const parent = Uri.joinPath(current, '..')
12-
if (parent.toString() === current.toString())
13-
return
14-
15-
current = parent
16-
}
17-
}
18-
19-
export async function findNearestFile(filename: string, start: Uri, shouldStop?: (uri: Uri) => boolean): Promise<Uri | undefined> {
20-
for (const dir of walkAncestors(start, shouldStop)) {
21-
const fileUri = Uri.joinPath(dir, filename)
22-
try {
23-
await workspace.fs.stat(fileUri)
24-
return fileUri
25-
} catch {
26-
continue
27-
}
28-
}
29-
}
1+
import type { Uri } from 'vscode'
2+
import { workspace } from 'vscode'
303

314
/** A parsed `package.json` manifest file. */
325
interface PackageManifest {

tests/utils/resolve.test.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)