|
| 1 | +import { readPackage } from "@rnx-kit/tools-node"; |
| 2 | +import { getWorkspacesInfoSync } from "@rnx-kit/tools-workspaces"; |
| 3 | +import path from "node:path"; |
| 4 | +import type { PackageInfo } from "./types"; |
| 5 | + |
| 6 | +class PackageInfoCache { |
| 7 | + private workspaceInfo; |
| 8 | + private byName = new Map<string, PackageInfo>(); |
| 9 | + private byRoot = new Map<string, PackageInfo>(); |
| 10 | + private loadedWorkspaces = false; |
| 11 | + private rootPath; |
| 12 | + |
| 13 | + constructor() { |
| 14 | + this.workspaceInfo = getWorkspacesInfoSync(); |
| 15 | + this.rootPath = this.workspaceInfo.getRoot(); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @returns the root package info |
| 20 | + */ |
| 21 | + getRootInfo(): PackageInfo { |
| 22 | + return this.getByPath(this.rootPath); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @param pkgPath path to the package.json or the root of the package |
| 27 | + * @returns loaded PackageInfo for the package, or throws an exception if not found |
| 28 | + */ |
| 29 | + getByPath(pkgPath: string): PackageInfo { |
| 30 | + const [root, manifestPath] = this.getPackagePaths(pkgPath); |
| 31 | + |
| 32 | + if (!this.byRoot.has(root)) { |
| 33 | + // it's not in the cache so load it |
| 34 | + const manifest = readPackage(manifestPath); |
| 35 | + const workspace = this.isWorkspace(root); |
| 36 | + |
| 37 | + // it's not a valid package if the manifest can't be laoded |
| 38 | + if (manifest) { |
| 39 | + // create a new entry and cache it |
| 40 | + this.store({ name: manifest.name, root, manifest, workspace }); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + const result = this.byRoot.get(root); |
| 45 | + if (!result) { |
| 46 | + throw new Error(`No package.json found at ${pkgPath}`); |
| 47 | + } |
| 48 | + return result; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param name the package to load, only valid for workspaces |
| 53 | + * @param loadWorkspacesIfNotFound do the expensive workspace load if not found |
| 54 | + * @returns a package info object for the workspace (if it exists) |
| 55 | + */ |
| 56 | + getWorkspace(name: string, loadWorkspacesIfNotFound?: boolean) { |
| 57 | + const cachedResult = this.byName.get(name); |
| 58 | + if (cachedResult || !loadWorkspacesIfNotFound) { |
| 59 | + return cachedResult; |
| 60 | + } |
| 61 | + |
| 62 | + // load the workspaces if we haven't already which will populate the name cache |
| 63 | + this.loadWorkspaces(); |
| 64 | + return this.byName.get(name); |
| 65 | + } |
| 66 | + |
| 67 | + /** ensure the workspaces are fully loaded */ |
| 68 | + private loadWorkspaces() { |
| 69 | + if (!this.loadedWorkspaces) { |
| 70 | + const packagePaths = this.workspaceInfo?.findPackagesSync() ?? []; |
| 71 | + for (const packagePath of packagePaths) { |
| 72 | + getPackageInfoFromPath(packagePath); |
| 73 | + } |
| 74 | + this.loadedWorkspaces = true; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** return the root package path and the package.json path for a path that could be either */ |
| 79 | + private getPackagePaths(pkgPath: string): [string, string] { |
| 80 | + const isPkgPath = path.basename(pkgPath).toLowerCase() === "package.json"; |
| 81 | + return isPkgPath |
| 82 | + ? [path.dirname(pkgPath), pkgPath] |
| 83 | + : [pkgPath, path.join(pkgPath, "package.json")]; |
| 84 | + } |
| 85 | + |
| 86 | + /** set the package info into the caches as appropriate */ |
| 87 | + private store(pkgInfo: PackageInfo) { |
| 88 | + if (pkgInfo.workspace) { |
| 89 | + this.byName.set(pkgInfo.name, pkgInfo); |
| 90 | + } |
| 91 | + this.byRoot.set(pkgInfo.root, pkgInfo); |
| 92 | + } |
| 93 | + |
| 94 | + /** check if this package is a workspace */ |
| 95 | + private isWorkspace(root: string) { |
| 96 | + return Boolean(this.workspaceInfo?.isWorkspace(root)); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +let cache: PackageInfoCache | undefined = undefined; |
| 101 | +function ensureCache() { |
| 102 | + if (!cache) { |
| 103 | + cache = new PackageInfoCache(); |
| 104 | + } |
| 105 | + return cache; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Looks up a package info by path, loading it if necessary |
| 110 | + * |
| 111 | + * @param pkgPath path to the package.json or the root of the package |
| 112 | + * @returns (potentially cached) package info for this package |
| 113 | + */ |
| 114 | +export function getPackageInfoFromPath(pkgPath: string): PackageInfo { |
| 115 | + return ensureCache().getByPath(pkgPath); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Load the package info by name, loading the project workspaces if necessary |
| 120 | + * |
| 121 | + * @param name name of the package to load |
| 122 | + * @param loadWorkspacesIfNotFound if the package is not in the cache load the workspaces (potentially expensive) |
| 123 | + * @returns the package info for the package |
| 124 | + */ |
| 125 | +export function getPackageInfoFromWorkspaces( |
| 126 | + name: string, |
| 127 | + loadWorkspacesIfNotFound?: boolean |
| 128 | +) { |
| 129 | + return ensureCache().getWorkspace(name, loadWorkspacesIfNotFound); |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * @returns the root package info |
| 134 | + */ |
| 135 | +export function getRootPackageInfo(): PackageInfo { |
| 136 | + return ensureCache().getRootInfo(); |
| 137 | +} |
0 commit comments