|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { PatchstackError, type PackageEntry } from '../types.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Parses yarn.lock (yarn classic v1 and yarn berry v2+) without a YAML |
| 7 | + * dependency. Both generations share the same block structure — a top-level |
| 8 | + * mapping of comma-separated descriptor lists to a block containing a |
| 9 | + * `version` field — so we walk them with the same scanner and only branch on |
| 10 | + * the `version` syntax (`version "x"` for v1, `version: x` for berry). |
| 11 | + * |
| 12 | + * Direct vs transitive can't be derived from yarn.lock alone (yarn does not |
| 13 | + * record an importer manifest the way pnpm v9 does), so we cross-reference |
| 14 | + * the sibling `package.json` when present. |
| 15 | + */ |
| 16 | +export async function parseYarnLockfile(lockfilePath: string): Promise<PackageEntry[]> { |
| 17 | + let raw: string; |
| 18 | + try { |
| 19 | + raw = await readFile(lockfilePath, 'utf8'); |
| 20 | + } catch (cause) { |
| 21 | + throw new PatchstackError( |
| 22 | + `Could not read lockfile at ${lockfilePath}`, |
| 23 | + 'LOCKFILE_NOT_FOUND', |
| 24 | + cause, |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + const blocks = parseBlocks(raw); |
| 29 | + if (blocks.length === 0) { |
| 30 | + throw new PatchstackError( |
| 31 | + `Lockfile at ${lockfilePath} contains no package entries`, |
| 32 | + 'LOCKFILE_PARSE_ERROR', |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + const directNames = await readDirectDepNames(path.dirname(lockfilePath)); |
| 37 | + |
| 38 | + const entries: PackageEntry[] = []; |
| 39 | + const seen = new Set<string>(); |
| 40 | + for (const block of blocks) { |
| 41 | + if (block.version.length === 0 || block.names.size === 0) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + for (const name of block.names) { |
| 45 | + const dedupKey = `${name}@${block.version}`; |
| 46 | + if (seen.has(dedupKey)) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + seen.add(dedupKey); |
| 50 | + entries.push({ |
| 51 | + name, |
| 52 | + version: block.version, |
| 53 | + direct: directNames.has(name), |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return entries; |
| 59 | +} |
| 60 | + |
| 61 | +interface Block { |
| 62 | + names: Set<string>; |
| 63 | + version: string; |
| 64 | +} |
| 65 | + |
| 66 | +function parseBlocks(raw: string): Block[] { |
| 67 | + const lines = raw.split(/\r?\n/); |
| 68 | + const blocks: Block[] = []; |
| 69 | + let current: Block | null = null; |
| 70 | + |
| 71 | + const finalize = () => { |
| 72 | + if (current !== null && current.version.length > 0 && current.names.size > 0) { |
| 73 | + blocks.push(current); |
| 74 | + } |
| 75 | + current = null; |
| 76 | + }; |
| 77 | + |
| 78 | + for (const line of lines) { |
| 79 | + const trimmed = line.trim(); |
| 80 | + if (trimmed.length === 0 || trimmed.startsWith('#')) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + const indent = countLeadingSpaces(line); |
| 85 | + |
| 86 | + if (indent === 0) { |
| 87 | + finalize(); |
| 88 | + if (!trimmed.endsWith(':')) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + // `__metadata:` (yarn berry header) has no `@` in any descriptor and |
| 92 | + // produces an empty names set, so it's naturally skipped on finalize. |
| 93 | + const keyLine = trimmed.slice(0, -1); |
| 94 | + const names = new Set<string>(); |
| 95 | + for (const spec of splitDescriptors(keyLine)) { |
| 96 | + const name = extractName(spec); |
| 97 | + if (name !== null) { |
| 98 | + names.add(name); |
| 99 | + } |
| 100 | + } |
| 101 | + current = { names, version: '' }; |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + if (current === null) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + const version = parseVersionField(trimmed); |
| 110 | + if (version !== null) { |
| 111 | + current.version = version; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + finalize(); |
| 116 | + return blocks; |
| 117 | +} |
| 118 | + |
| 119 | +function countLeadingSpaces(line: string): number { |
| 120 | + let i = 0; |
| 121 | + while (i < line.length && line[i] === ' ') { |
| 122 | + i++; |
| 123 | + } |
| 124 | + return i; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Splits a yarn descriptor key list on top-level commas. yarn quotes any |
| 129 | + * descriptor that contains characters needing escaping, so we respect quotes |
| 130 | + * while splitting to avoid breaking on commas inside (rare in practice but |
| 131 | + * cheap to handle). |
| 132 | + */ |
| 133 | +export function splitDescriptors(keyLine: string): string[] { |
| 134 | + const parts: string[] = []; |
| 135 | + let current = ''; |
| 136 | + let quote: '"' | "'" | null = null; |
| 137 | + |
| 138 | + for (let i = 0; i < keyLine.length; i++) { |
| 139 | + const c = keyLine[i]; |
| 140 | + if (quote !== null) { |
| 141 | + current += c; |
| 142 | + if (c === quote) { |
| 143 | + quote = null; |
| 144 | + } |
| 145 | + continue; |
| 146 | + } |
| 147 | + if (c === '"' || c === "'") { |
| 148 | + quote = c; |
| 149 | + current += c; |
| 150 | + continue; |
| 151 | + } |
| 152 | + if (c === ',') { |
| 153 | + const piece = current.trim(); |
| 154 | + if (piece.length > 0) { |
| 155 | + parts.push(piece); |
| 156 | + } |
| 157 | + current = ''; |
| 158 | + continue; |
| 159 | + } |
| 160 | + current += c; |
| 161 | + } |
| 162 | + const tail = current.trim(); |
| 163 | + if (tail.length > 0) { |
| 164 | + parts.push(tail); |
| 165 | + } |
| 166 | + return parts; |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * Extracts the package name from a yarn descriptor like `axios@^1.6.0`, |
| 171 | + * `"@scope/pkg@^2.1.0"`, or `"@scope/pkg@npm:2.1.0"`. The descriptor's |
| 172 | + * range portion is discarded — we only need the name, since the resolved |
| 173 | + * version comes from the `version` field of the block. |
| 174 | + */ |
| 175 | +export function extractName(rawSpec: string): string | null { |
| 176 | + let s = rawSpec.trim(); |
| 177 | + if (s.length === 0) { |
| 178 | + return null; |
| 179 | + } |
| 180 | + if ( |
| 181 | + (s.startsWith('"') && s.endsWith('"')) || |
| 182 | + (s.startsWith("'") && s.endsWith("'")) |
| 183 | + ) { |
| 184 | + s = s.slice(1, -1); |
| 185 | + } |
| 186 | + // Position-0 `@` belongs to a scope, so we want the last `@` after it. |
| 187 | + const atIdx = s.lastIndexOf('@'); |
| 188 | + if (atIdx <= 0) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + const name = s.slice(0, atIdx); |
| 192 | + return name.length > 0 ? name : null; |
| 193 | +} |
| 194 | + |
| 195 | +function parseVersionField(content: string): string | null { |
| 196 | + if (!content.startsWith('version')) { |
| 197 | + return null; |
| 198 | + } |
| 199 | + const after = content.slice('version'.length); |
| 200 | + // yarn v1: `version "1.2.3"` (whitespace then quoted) |
| 201 | + // yarn berry: `version: 1.2.3` or `version: "1.2.3"` |
| 202 | + const firstChar = after.charAt(0); |
| 203 | + if (firstChar !== ' ' && firstChar !== '\t' && firstChar !== ':') { |
| 204 | + return null; |
| 205 | + } |
| 206 | + let rest = firstChar === ':' ? after.slice(1) : after; |
| 207 | + rest = rest.trim(); |
| 208 | + if (rest.length === 0) { |
| 209 | + return null; |
| 210 | + } |
| 211 | + if ( |
| 212 | + (rest.startsWith('"') && rest.endsWith('"')) || |
| 213 | + (rest.startsWith("'") && rest.endsWith("'")) |
| 214 | + ) { |
| 215 | + rest = rest.slice(1, -1); |
| 216 | + } |
| 217 | + return rest.length > 0 ? rest : null; |
| 218 | +} |
| 219 | + |
| 220 | +async function readDirectDepNames(cwd: string): Promise<Set<string>> { |
| 221 | + const names = new Set<string>(); |
| 222 | + let raw: string; |
| 223 | + try { |
| 224 | + raw = await readFile(path.join(cwd, 'package.json'), 'utf8'); |
| 225 | + } catch { |
| 226 | + return names; |
| 227 | + } |
| 228 | + |
| 229 | + let parsed: unknown; |
| 230 | + try { |
| 231 | + parsed = JSON.parse(raw); |
| 232 | + } catch { |
| 233 | + return names; |
| 234 | + } |
| 235 | + |
| 236 | + if (typeof parsed !== 'object' || parsed === null) { |
| 237 | + return names; |
| 238 | + } |
| 239 | + const obj = parsed as Record<string, unknown>; |
| 240 | + |
| 241 | + for (const field of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) { |
| 242 | + const section = obj[field]; |
| 243 | + if (typeof section !== 'object' || section === null) { |
| 244 | + continue; |
| 245 | + } |
| 246 | + for (const name of Object.keys(section)) { |
| 247 | + names.add(name); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + return names; |
| 252 | +} |
0 commit comments