|
| 1 | +import fs from 'fs' |
| 2 | +import { parse as parseYaml } from 'yaml' |
| 3 | + |
| 4 | +type DepEntry = { specifier?: string; version?: string } |
| 5 | + |
| 6 | +type LockRoot = { |
| 7 | + dependencies?: Record<string, DepEntry> |
| 8 | + devDependencies?: Record<string, DepEntry> |
| 9 | + optionalDependencies?: Record<string, DepEntry> |
| 10 | +} |
| 11 | + |
| 12 | +function shortSha (hex: string) { |
| 13 | + return hex.slice(0, 7) |
| 14 | +} |
| 15 | + |
| 16 | +function pinLine (name: string, value: string) { |
| 17 | + return `${name}: ${value}` |
| 18 | +} |
| 19 | + |
| 20 | +/** pnpm `version` field for git deps: https://codeload.github.com/org/repo/tar.gz/<40hex>(peer...) */ |
| 21 | +function displayPin (name: string, specifier: string | undefined, versionField: string | undefined) { |
| 22 | + const spec = specifier ?? '' |
| 23 | + const ver = versionField ?? '' |
| 24 | + |
| 25 | + const tarballSha = ver.match(/tar\.gz\/([a-f0-9]{40})/i) |
| 26 | + if (tarballSha) { |
| 27 | + return pinLine(name, shortSha(tarballSha[1])) |
| 28 | + } |
| 29 | + |
| 30 | + const plain = ver.replace(/\(.*/, '').trim() |
| 31 | + if (plain && !plain.startsWith('http') && !plain.startsWith('link:')) { |
| 32 | + return pinLine(name, plain) |
| 33 | + } |
| 34 | + |
| 35 | + if (plain.startsWith('link:')) { |
| 36 | + return pinLine(name, plain) |
| 37 | + } |
| 38 | + |
| 39 | + const spec40 = spec.match(/#([a-f0-9]{40})\s*$/i) |
| 40 | + if (spec40) { |
| 41 | + return pinLine(name, shortSha(spec40[1])) |
| 42 | + } |
| 43 | + |
| 44 | + if (spec.startsWith('github:')) { |
| 45 | + const hashPart = spec.split('#')[1] |
| 46 | + if (hashPart && /^[a-f0-9]{40}$/i.test(hashPart)) { |
| 47 | + return pinLine(name, shortSha(hashPart)) |
| 48 | + } |
| 49 | + const ref = hashPart || spec.replace(/^github:[^/]+\/[^#]+#?/, '') |
| 50 | + return pinLine(name, ref || spec) |
| 51 | + } |
| 52 | + |
| 53 | + return pinLine(name, spec || ver || '?') |
| 54 | +} |
| 55 | + |
| 56 | +export function resolveWatermarkPackagePins (lockfilePath: string, names: string[]): string[] { |
| 57 | + const doc = parseYaml(fs.readFileSync(lockfilePath, 'utf8')) as { |
| 58 | + importers?: Record<string, LockRoot> |
| 59 | + } |
| 60 | + const root = doc.importers?.['.'] |
| 61 | + if (!root) { |
| 62 | + return names.map((n) => pinLine(n, '?')) |
| 63 | + } |
| 64 | + |
| 65 | + const merged: Record<string, DepEntry> = { |
| 66 | + ...root.dependencies, |
| 67 | + ...root.devDependencies, |
| 68 | + ...root.optionalDependencies, |
| 69 | + } |
| 70 | + |
| 71 | + return names.map((name) => { |
| 72 | + const entry = merged[name] |
| 73 | + if (!entry) return pinLine(name, '?') |
| 74 | + return displayPin(name, entry.specifier, entry.version) |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +/** Reads `watermarkPackages`, appends resolved lines to `watermark`, removes `watermarkPackages`. */ |
| 79 | +export function applyWatermarkPackagesToConfig ( |
| 80 | + configJson: Record<string, unknown>, |
| 81 | + lockfilePath: string |
| 82 | +) { |
| 83 | + const pkgs = configJson.watermarkPackages |
| 84 | + if (!Array.isArray(pkgs) || pkgs.length === 0) return |
| 85 | + |
| 86 | + const names = pkgs.filter((x): x is string => typeof x === 'string') |
| 87 | + delete configJson.watermarkPackages |
| 88 | + |
| 89 | + if (names.length === 0) return |
| 90 | + |
| 91 | + try { |
| 92 | + const lines = resolveWatermarkPackagePins(lockfilePath, names) |
| 93 | + const extra = lines.join('\n') |
| 94 | + const existing = typeof configJson.watermark === 'string' ? configJson.watermark.trim() : '' |
| 95 | + configJson.watermark = [existing, extra].filter(Boolean).join('\n') |
| 96 | + } catch (err) { |
| 97 | + console.warn('watermarkPackages: failed to read pnpm-lock.yaml:', err) |
| 98 | + } |
| 99 | +} |
0 commit comments