|
| 1 | +import { Uri, workspace } from 'vscode' |
| 2 | + |
| 3 | +/** |
| 4 | + * Resolves the relative path of a file within its package. |
| 5 | + * |
| 6 | + * @param uri The URI of the file to resolve. |
| 7 | + * @returns A promise that resolves to the package manifest and relative path, |
| 8 | + * or `undefined` if not found. |
| 9 | + */ |
| 10 | +export async function resolvePackageRelativePath(uri: Uri): Promise<{ manifest: PackageManifest, relativePath: string } | undefined> { |
| 11 | + const result = await findPackageJson(uri) |
| 12 | + if (!result) |
| 13 | + return undefined |
| 14 | + |
| 15 | + const { uri: pkgUri, manifest } = result |
| 16 | + const relativePath = uri.path.slice(pkgUri.path.lastIndexOf('/') + 1) |
| 17 | + |
| 18 | + return { manifest, relativePath } |
| 19 | +} |
| 20 | + |
| 21 | +/** A parsed `package.json` manifest file. */ |
| 22 | +interface PackageManifest { |
| 23 | + /** Package name. */ |
| 24 | + name: string |
| 25 | + |
| 26 | + /** Package version specifier. */ |
| 27 | + version: string |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Finds the nearest package.json file by searching upwards from the given URI. |
| 32 | + * |
| 33 | + * @param file The URI to start the search from. |
| 34 | + * @returns The URI and parsed content of the package.json, or `undefined` if |
| 35 | + * not found. |
| 36 | + */ |
| 37 | +async function findPackageJson(file: Uri): Promise<{ uri: Uri, manifest: PackageManifest } | undefined> { |
| 38 | + // Start from the directory, so we don't look for |
| 39 | + // `node_modules/foo/bar.js/package.json` |
| 40 | + const startDir = Uri.joinPath(file, '..') |
| 41 | + |
| 42 | + for (const dir of walkAncestors(startDir)) { |
| 43 | + const pkgUri = Uri.joinPath(dir, 'package.json') |
| 44 | + |
| 45 | + let pkg: Partial<PackageManifest> | undefined |
| 46 | + try { |
| 47 | + const content = await workspace.fs.readFile(pkgUri) |
| 48 | + pkg = JSON.parse(new TextDecoder().decode(content)) as Partial<PackageManifest> |
| 49 | + } catch { |
| 50 | + continue |
| 51 | + } |
| 52 | + |
| 53 | + if (isValidManifest(pkg)) { |
| 54 | + return { |
| 55 | + uri: pkgUri, |
| 56 | + manifest: pkg, |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return undefined |
| 62 | +} |
| 63 | + |
| 64 | +function* walkAncestors(start: Uri): Generator<Uri, void, void> { |
| 65 | + let currentUri = start |
| 66 | + while (true) { |
| 67 | + yield currentUri |
| 68 | + |
| 69 | + if (currentUri.path.endsWith('/node_modules')) |
| 70 | + return |
| 71 | + |
| 72 | + const parentUri = Uri.joinPath(currentUri, '..') |
| 73 | + if (parentUri.toString() === currentUri.toString()) |
| 74 | + return |
| 75 | + |
| 76 | + currentUri = parentUri |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Check for valid package manifest, as it might be a manifest which just |
| 82 | + * configures a setting without really being a package (such as |
| 83 | + * `{sideEffects: false}`). |
| 84 | + */ |
| 85 | +function isValidManifest(json: Partial<PackageManifest>): json is PackageManifest { |
| 86 | + return Boolean(json && json.name && json.version) |
| 87 | +} |
0 commit comments