|
| 1 | +import findUp from "find-up"; |
| 2 | +import path from "node:path"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import os from "node:os"; |
| 5 | + |
1 | 6 | /** |
2 | 7 | * Checks whether the given input is already an array, and if it isn't, wraps it in one. |
3 | 8 | * |
|
7 | 12 | export function arrayify<T = unknown>(maybeArray: T | T[]): T[] { |
8 | 13 | return Array.isArray(maybeArray) ? maybeArray : [maybeArray]; |
9 | 14 | } |
| 15 | + |
| 16 | +type PackageJson = Record<string, unknown>; |
| 17 | + |
| 18 | +/** |
| 19 | + * Get the closes package.json from a given starting point upwards. |
| 20 | + * This handles a few edge cases: |
| 21 | + * * Check if a given file package.json appears to be an actual NPM package.json file |
| 22 | + * * Stop at the home dir, to avoid looking too deeply |
| 23 | + */ |
| 24 | +export function getPackageJson({ cwd, stopAt }: { cwd?: string; stopAt?: string } = {}): |
| 25 | + | PackageJson |
| 26 | + | undefined { |
| 27 | + return lookupPackageJson(cwd ?? process.cwd(), path.normalize(stopAt ?? os.homedir())); |
| 28 | +} |
| 29 | + |
| 30 | +export function parseMajorVersion(version: string): number | undefined { |
| 31 | + // if it has a `v` prefix, remove it |
| 32 | + if (version.startsWith("v")) { |
| 33 | + version = version.slice(1); |
| 34 | + } |
| 35 | + |
| 36 | + // First, try simple lookup of exact, ~ and ^ versions |
| 37 | + const regex = /^[\^~]?(\d+)(\.\d+)?(\.\d+)?(-.+)?/; |
| 38 | + |
| 39 | + const match = version.match(regex); |
| 40 | + if (match) { |
| 41 | + return parseInt(match[1] as string, 10); |
| 42 | + } |
| 43 | + |
| 44 | + // Try to parse e.g. 1.x |
| 45 | + const coerced = parseInt(version, 10); |
| 46 | + if (!Number.isNaN(coerced)) { |
| 47 | + return coerced; |
| 48 | + } |
| 49 | + |
| 50 | + // Match <= and >= ranges. |
| 51 | + const gteLteRegex = /^[<>]=\s*(\d+)(\.\d+)?(\.\d+)?(-.+)?/; |
| 52 | + const gteLteMatch = version.match(gteLteRegex); |
| 53 | + if (gteLteMatch) { |
| 54 | + return parseInt(gteLteMatch[1] as string, 10); |
| 55 | + } |
| 56 | + |
| 57 | + // match < ranges |
| 58 | + const ltRegex = /^<\s*(\d+)(\.\d+)?(\.\d+)?(-.+)?/; |
| 59 | + const ltMatch = version.match(ltRegex); |
| 60 | + if (ltMatch) { |
| 61 | + // Two scenarios: |
| 62 | + // a) < 2.0.0 --> return 1 |
| 63 | + // b) < 2.1.0 --> return 2 |
| 64 | + |
| 65 | + const major = parseInt(ltMatch[1] as string, 10); |
| 66 | + |
| 67 | + if ( |
| 68 | + // minor version > 0 |
| 69 | + (typeof ltMatch[2] === "string" && parseInt(ltMatch[2].slice(1), 10) > 0) || |
| 70 | + // patch version > 0 |
| 71 | + (typeof ltMatch[3] === "string" && parseInt(ltMatch[3].slice(1), 10) > 0) |
| 72 | + ) { |
| 73 | + return major; |
| 74 | + } |
| 75 | + |
| 76 | + return major - 1; |
| 77 | + } |
| 78 | + |
| 79 | + // match > ranges |
| 80 | + const gtRegex = /^>\s*(\d+)(\.\d+)?(\.\d+)?(-.+)?/; |
| 81 | + const gtMatch = version.match(gtRegex); |
| 82 | + if (gtMatch) { |
| 83 | + // We always return the version here, even though it _may_ be incorrect |
| 84 | + // E.g. if given > 2.0.0, it should be 2 if there exists any 2.x.x version, else 3 |
| 85 | + // Since there is no way for us to know this, we're going to assume any kind of patch/feature release probably exists |
| 86 | + return parseInt(gtMatch[1] as string, 10); |
| 87 | + } |
| 88 | + return undefined; |
| 89 | +} |
| 90 | + |
| 91 | +// This is an explicit list of packages where we want to include the (major) version number. |
| 92 | +const PACKAGES_TO_INCLUDE_VERSION = [ |
| 93 | + "react", |
| 94 | + "@angular/core", |
| 95 | + "vue", |
| 96 | + "ember-source", |
| 97 | + "svelte", |
| 98 | + "@sveltejs/kit", |
| 99 | + "webpack", |
| 100 | + "vite", |
| 101 | + "gatsby", |
| 102 | + "next", |
| 103 | + "remix", |
| 104 | + "rollup", |
| 105 | + "esbuild", |
| 106 | +]; |
| 107 | + |
| 108 | +export function getDependencies(packageJson: PackageJson): { |
| 109 | + deps: string[]; |
| 110 | + depsVersions: Record<string, number>; |
| 111 | +} { |
| 112 | + const dependencies: Record<string, string> = Object.assign( |
| 113 | + {}, |
| 114 | + packageJson["devDependencies"] ?? {}, |
| 115 | + packageJson["dependencies"] ?? {} |
| 116 | + ); |
| 117 | + |
| 118 | + const deps = Object.keys(dependencies).sort(); |
| 119 | + |
| 120 | + const depsVersions: Record<string, number> = deps.reduce((depsVersions, depName) => { |
| 121 | + if (PACKAGES_TO_INCLUDE_VERSION.includes(depName)) { |
| 122 | + const version = dependencies[depName] as string; |
| 123 | + const majorVersion = parseMajorVersion(version); |
| 124 | + if (majorVersion) { |
| 125 | + depsVersions[depName] = majorVersion; |
| 126 | + } |
| 127 | + } |
| 128 | + return depsVersions; |
| 129 | + }, {} as Record<string, number>); |
| 130 | + |
| 131 | + return { deps, depsVersions }; |
| 132 | +} |
| 133 | + |
| 134 | +function lookupPackageJson(cwd: string, stopAt: string): PackageJson | undefined { |
| 135 | + const jsonPath = findUp.sync( |
| 136 | + (dirName) => { |
| 137 | + // Stop if we reach this dir |
| 138 | + if (path.normalize(dirName) === stopAt) { |
| 139 | + return findUp.stop; |
| 140 | + } |
| 141 | + |
| 142 | + return findUp.sync.exists(dirName + "/package.json") ? "package.json" : undefined; |
| 143 | + }, |
| 144 | + { cwd } |
| 145 | + ); |
| 146 | + |
| 147 | + if (!jsonPath) { |
| 148 | + return undefined; |
| 149 | + } |
| 150 | + |
| 151 | + try { |
| 152 | + const jsonStr = fs.readFileSync(jsonPath, "utf8"); |
| 153 | + const json = JSON.parse(jsonStr) as PackageJson; |
| 154 | + |
| 155 | + // Ensure it is an actual package.json |
| 156 | + // This is very much not bulletproof, but should be good enough |
| 157 | + if ("name" in json || "private" in json) { |
| 158 | + return json; |
| 159 | + } |
| 160 | + } catch (error) { |
| 161 | + // Ignore and walk up |
| 162 | + } |
| 163 | + |
| 164 | + // Continue up the tree, if we find a fitting package.json |
| 165 | + const newCwd = path.dirname(path.resolve(jsonPath + "/..")); |
| 166 | + return lookupPackageJson(newCwd, stopAt); |
| 167 | +} |
0 commit comments