|
| 1 | +// Re-pins tag-pinned GitHub file links when a package releases. Runs from a package directory as |
| 2 | +// part of its `version` lifecycle (cwd = the package, manifest already carries the new version). |
| 3 | +// |
| 4 | +// Model: absolute links into this repo are pinned to release tags per the AGENTS.md link policy. |
| 5 | +// Links that should track the latest release of a package are, by construction, pinned to that |
| 6 | +// package's newest existing tag — this script rewrites exactly those, repo-wide, to the tag the |
| 7 | +// running release is about to create. Links pinned to older tags are deliberately historical |
| 8 | +// (changelogs, migration guides, old threads) and are never touched. |
| 9 | +// |
| 10 | +// Every rewritten link is verified against the working tree, which is what the new tag will |
| 11 | +// contain. A path that no longer exists fails the version step loudly so a release can never |
| 12 | +// re-pin links onto targets that do not resolve; re-pin such links to an old tag by hand (making |
| 13 | +// them historical) or fix their paths, then rerun. Paths that lived at the repo root before the |
| 14 | +// monorepo migration are remapped into the package directory automatically. |
| 15 | + |
| 16 | +import { execFileSync } from 'node:child_process' |
| 17 | +import fs from 'node:fs' |
| 18 | +import path from 'node:path' |
| 19 | +import { fileURLToPath } from 'node:url' |
| 20 | + |
| 21 | +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') |
| 22 | +const packageDir = process.cwd() |
| 23 | + |
| 24 | +// Tags without a package prefix (v3.6.0 and earlier) are the pre-monorepo history and belong to tailwind-merge, mirroring the fallback in the release-commenter action. |
| 25 | +const LEGACY_TAG_OWNER = 'tailwind-merge' |
| 26 | + |
| 27 | +const manifest = JSON.parse(fs.readFileSync(path.join(packageDir, 'package.json'), 'utf8')) |
| 28 | +const newTag = `${manifest.name}@${manifest.version}` |
| 29 | + |
| 30 | +const previousTag = findPreviousTag(manifest.name) |
| 31 | +if (!previousTag) { |
| 32 | + console.log(`[update-pinned-links] No previous release tag for ${manifest.name} — nothing to re-pin.`) |
| 33 | + process.exit(0) |
| 34 | +} |
| 35 | + |
| 36 | +console.log(`[update-pinned-links] Re-pinning links from ${previousTag} to ${newTag}`) |
| 37 | + |
| 38 | +// Matches the pinned-link forms the repo uses: github.com blob/raw/tree URLs and raw.githubusercontent.com URLs. The path capture stops at characters that end a URL in Markdown or code; fragments and queries stay in place untouched. |
| 39 | +const linkRegex = new RegExp( |
| 40 | + `(https?://(?:www\\.)?(?:github\\.com/dcastil/tailwind-merge/(?:blob|raw|tree)|raw\\.githubusercontent\\.com/dcastil/tailwind-merge))/${escapeRegex(previousTag)}/([^\\s)\\]"'\`<>*\\\\#?]*)`, |
| 41 | + 'g', |
| 42 | +) |
| 43 | + |
| 44 | +const trackedFiles = findFilesMentioning(previousTag) |
| 45 | + |
| 46 | +// Two passes so a failure leaves the working tree untouched: first compute every replacement and collect unresolvable links, then write only when the whole run is clean. |
| 47 | +const unresolvedLinks = [] |
| 48 | +const pendingWrites = [] |
| 49 | + |
| 50 | +for (const file of trackedFiles) { |
| 51 | + const filePath = path.join(repoRoot, file) |
| 52 | + const content = fs.readFileSync(filePath, 'utf8') |
| 53 | + |
| 54 | + const nextContent = content.replace(linkRegex, (match, base, linkedPath) => { |
| 55 | + const resolvedPath = resolveLinkedPath(linkedPath) |
| 56 | + if (resolvedPath === null) { |
| 57 | + unresolvedLinks.push(`${file}: ${match}`) |
| 58 | + return match |
| 59 | + } |
| 60 | + return `${base}/${newTag}/${resolvedPath}` |
| 61 | + }) |
| 62 | + |
| 63 | + if (nextContent !== content) { |
| 64 | + pendingWrites.push({ file, filePath, nextContent }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +if (unresolvedLinks.length > 0) { |
| 69 | + console.error( |
| 70 | + `[update-pinned-links] ${unresolvedLinks.length} link(s) point at paths that do not exist in the tree the new tag will contain. Fix the paths or re-pin them to an old tag by hand, then rerun:\n${unresolvedLinks.join('\n')}`, |
| 71 | + ) |
| 72 | + process.exit(1) |
| 73 | +} |
| 74 | + |
| 75 | +for (const { file, filePath, nextContent } of pendingWrites) { |
| 76 | + fs.writeFileSync(filePath, nextContent) |
| 77 | + execFileSync('git', ['add', file], { cwd: repoRoot }) |
| 78 | + console.log(`[update-pinned-links] Updated ${file}`) |
| 79 | +} |
| 80 | + |
| 81 | +console.log( |
| 82 | + pendingWrites.length > 0 |
| 83 | + ? `[update-pinned-links] Re-pinned links in ${pendingWrites.length} file(s).` |
| 84 | + : '[update-pinned-links] No links needed re-pinning.', |
| 85 | +) |
| 86 | + |
| 87 | +/** |
| 88 | + * Lists tracked text files mentioning the previous tag, as a cheap pre-filter for the link rewrite. git grep exits 1 when nothing matches, which is a normal outcome here. |
| 89 | + * |
| 90 | + * @param {string} tag |
| 91 | + * @returns {string[]} |
| 92 | + */ |
| 93 | +function findFilesMentioning(tag) { |
| 94 | + try { |
| 95 | + return execFileSync('git', ['grep', '-lI', '--fixed-strings', `/${tag}/`], { |
| 96 | + cwd: repoRoot, |
| 97 | + encoding: 'utf8', |
| 98 | + }) |
| 99 | + .split('\n') |
| 100 | + .filter(Boolean) |
| 101 | + } catch (error) { |
| 102 | + if (error && typeof error === 'object' && error.status === 1) return [] |
| 103 | + throw error |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Finds the newest existing release tag of the given package by semver, including the un-prefixed legacy tags for the package that owns the pre-monorepo history. The tag for the running release does not exist yet (npm creates it after the version scripts), so the newest existing tag is the previous release. |
| 109 | + * |
| 110 | + * @param {string} packageName |
| 111 | + * @returns {string | null} |
| 112 | + */ |
| 113 | +function findPreviousTag(packageName) { |
| 114 | + const allTags = execFileSync('git', ['tag', '--list'], { cwd: repoRoot, encoding: 'utf8' }) |
| 115 | + .split('\n') |
| 116 | + .filter(Boolean) |
| 117 | + |
| 118 | + const versionByTag = new Map() |
| 119 | + for (const tag of allTags) { |
| 120 | + let versionText = null |
| 121 | + if (tag.startsWith(`${packageName}@`)) { |
| 122 | + versionText = tag.slice(packageName.length + 1) |
| 123 | + } else if (packageName === LEGACY_TAG_OWNER && /^v\d/.test(tag)) { |
| 124 | + versionText = tag.slice(1) |
| 125 | + } |
| 126 | + if (!versionText) continue |
| 127 | + |
| 128 | + const match = versionText.match(/^(\d+)\.(\d+)\.(\d+)$/) |
| 129 | + if (!match) continue |
| 130 | + |
| 131 | + versionByTag.set(tag, match.slice(1, 4).map(Number)) |
| 132 | + } |
| 133 | + |
| 134 | + let best = null |
| 135 | + for (const [tag, version] of versionByTag) { |
| 136 | + if (!best || compareVersions(version, best.version) > 0) { |
| 137 | + best = { tag, version } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return best?.tag ?? null |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * @param {number[]} a |
| 146 | + * @param {number[]} b |
| 147 | + * @returns {number} |
| 148 | + */ |
| 149 | +function compareVersions(a, b) { |
| 150 | + for (let index = 0; index < 3; index += 1) { |
| 151 | + if (a[index] !== b[index]) return a[index] - b[index] |
| 152 | + } |
| 153 | + return 0 |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * Validates a linked path against the working tree, remapping pre-monorepo root paths into the package directory when the file moved there. Returns null when the path resolves nowhere. |
| 158 | + * |
| 159 | + * @param {string} linkedPath |
| 160 | + * @returns {string | null} |
| 161 | + */ |
| 162 | +function resolveLinkedPath(linkedPath) { |
| 163 | + const normalizedPath = linkedPath.replace(/\/+$/, '') |
| 164 | + if (normalizedPath === '') return '' |
| 165 | + if (fs.existsSync(path.join(repoRoot, normalizedPath))) return normalizedPath |
| 166 | + |
| 167 | + const packageDirRelative = path.relative(repoRoot, packageDir) |
| 168 | + const remappedPath = `${packageDirRelative}/${normalizedPath}` |
| 169 | + if (fs.existsSync(path.join(repoRoot, remappedPath))) return remappedPath |
| 170 | + |
| 171 | + return null |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * @param {string} value |
| 176 | + * @returns {string} |
| 177 | + */ |
| 178 | +function escapeRegex(value) { |
| 179 | + return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
| 180 | +} |
0 commit comments