Repository navigation
Expand file tree
/
Copy pathcheck-versions.mjs
More file actions
317 lines (286 loc) · 11.4 KB
/
Copy pathcheck-versions.mjs
File metadata and controls
317 lines (286 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env node
/**
* Version-consistency guard.
*
* `packages/core/package.json` is the single source of truth for the version.
* Everything else that names one must agree with it.
*
* This repository publishes **one** package, and it must stay that way: the
* whole reason Impulse is not a package inside `inertia` is that Inertia's
* guard pins every package to core's version and requires an
* `@rootnative/inertia` peer on each adapter (see README.md).
* Neither rule appears here, and Impulse must never join that lockstep.
* Check 1 below is what makes a second public package a visible decision
* rather than a quiet one.
*
* Checks:
* 1. Single package — exactly one non-private package exists, and it is
* `core`. A second one is not forbidden outright, but
* it changes what "release" means, so it fails here
* until this script is taught the new shape.
* 2. Changelog — `packages/core/CHANGELOG.md` has a
* `## [<version>]` section. Shipping a release with no
* entry is the failure this catches.
* 3. Link refs — every `## [x.y.z]` heading has a matching link
* definition, and `[unreleased]` compares from the
* newest released tag.
* 4. Status lines — the `> **Status:** `x.y.z`` line in README.md and
* packages/core/README.md, the "Impulse is at" line of
* docs/docs/roadmap.md, and the `Status:` line of
* docs/static/llms.txt name the current version.
* The package README is in the list because it ships
* in the npm tarball, so a stale one is published.
* 5. Version form — no `v`-prefixed version strings (`v0.2`, `v1.0.0`)
* in any published surface. Versions are always bare
* three-digit semver. URLs are exempt (the SemVer spec
* link is `.../spec/v2.0.0.html`).
*
* Usage:
* node scripts/check-versions.mjs # verify, non-zero exit on drift
* node scripts/check-versions.mjs --fix # repair what is mechanically fixable
*
* `--fix` rewrites link footers and status-line version tokens. It will not
* invent a CHANGELOG entry — a release note is editorial, so a missing
* section is always reported for a human to write.
*/
import { readdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const here = dirname(fileURLToPath(import.meta.url))
const repoRoot = resolve(here, '..')
const packagesDir = join(repoRoot, 'packages')
const FIX = process.argv.includes('--fix')
const REPO_URL = 'https://github.com/rootnative/impulse'
/**
* Git tags are cut as `core@<version>`. The package name is not in the tag
* because the directory is what the release workflow publishes from, and the
* label stays stable if the package is ever renamed.
*/
const tagFor = (version) => `core@${version}`
const README_STATUS_RE = /^(> \*\*Status:\*\* `)([^`]+)(`)/m
/**
* Lines that must name the current version, each in the shape its file uses.
* `build:llms` copies `docs/static/llms.txt` into the package and reads the
* roadmap page into `llms-full.txt`, so `--fix` must run before it.
*/
const STATUS_LINES = [
{ path: 'README.md', re: README_STATUS_RE, shape: '> **Status:** `x.y.z`' },
{
path: join('packages', 'core', 'README.md'),
re: README_STATUS_RE,
shape: '> **Status:** `x.y.z`',
},
{
path: join('docs', 'docs', 'roadmap.md'),
re: /^(Impulse is at `)([^`]+)(`)/m,
shape: 'Impulse is at `x.y.z`',
},
{
path: join('docs', 'static', 'llms.txt'),
re: /^(Status: )(\S+?)(\.\s)/m,
shape: 'Status: x.y.z.',
},
]
/** Roots scanned by check 5. Directories are walked for the listed extensions. */
const FORM_SCAN_ROOTS = [
{ path: 'README.md' },
{ path: 'packages', exts: ['.md', '.txt', '.ts', '.tsx', '.cjs'] },
{ path: 'example', exts: ['.ts', '.tsx'] },
]
const FORM_SCAN_IGNORE = new Set(['node_modules', 'dist', 'build', '.expo'])
/** `v` immediately followed by a version number — `v0.2`, `v1.0.0`, `v0.0.0-alpha.0`. */
const V_PREFIX_RE = /\bv\d+\.\d+(?:\.\d+)?(?:-[\w.]+)?/g
/** Stripped before scanning: a `v`-prefixed version inside a URL is somebody else's. */
const URL_RE = /https?:\/\/\S+/g
const problems = []
const fixes = []
const fail = (msg) => problems.push(msg)
const fixed = (msg) => fixes.push(msg)
// ── 1. One public package, and it is `core` ─────────────────────────────────
const publicDirs = readdirSync(packagesDir, { withFileTypes: true })
.filter((d) => d.isDirectory())
.map((d) => d.name)
.filter((name) => {
try {
return (
JSON.parse(
readFileSync(join(packagesDir, name, 'package.json'), 'utf8'),
).private !== true
)
} catch {
return false
}
})
.sort()
if (!publicDirs.includes('core')) {
console.error(
'[check-versions] no public `core` package found under packages/',
)
process.exit(1)
}
const extras = publicDirs.filter((d) => d !== 'core')
if (extras.length > 0) {
fail(
`packages/: ${extras.join(', ')} ${extras.length === 1 ? 'is' : 'are'} public ` +
`alongside core. This repository ships one package. Adding a second one is a ` +
`release-shape decision — decide whether it versions with core or on its own, ` +
`record it in RELEASING.md, and teach this script the answer. Do not copy ` +
`Inertia's lockstep: that coupling is what Impulse exists to avoid.`,
)
}
const readPkg = (dir) =>
JSON.parse(readFileSync(join(packagesDir, dir, 'package.json'), 'utf8'))
const VERSION = readPkg('core').version
const RELEASE_TAG = tagFor(VERSION)
// ── 2 + 3. CHANGELOG section and link footer ────────────────────────────────
/**
* Rebuild the link-reference footer from the changelog's own `## [x.y.z]`
* headings. `[unreleased]` compares the newest released tag against HEAD;
* every other heading resolves to its release tag. Headings are emitted
* newest-first, matching the body order.
*/
function buildFooter(versions) {
const lines = []
if (versions.length > 0) {
lines.push(
`[unreleased]: ${REPO_URL}/compare/${tagFor(versions[0])}...HEAD`,
)
} else {
// Nothing released yet: [unreleased] is the whole history.
lines.push(`[unreleased]: ${REPO_URL}/commits/main`)
}
for (const v of versions) {
lines.push(`[${v}]: ${REPO_URL}/releases/tag/${tagFor(v)}`)
}
return lines.join('\n')
}
const changelogRel = 'packages/core/CHANGELOG.md'
const changelogAbs = join(packagesDir, 'core', 'CHANGELOG.md')
let raw
try {
raw = readFileSync(changelogAbs, 'utf8')
} catch {
raw = null
fail(`${changelogRel}: missing.`)
}
if (raw !== null) {
// Released-version headings, in document order (newest first by convention).
const versions = [...raw.matchAll(/^## \[(\d[^\]]*)\]/gm)].map((m) => m[1])
// A prerelease needs its section too. The release workflow bumps the version
// and then runs this check, so the section a human wrote before dispatching
// it is the only release note the version will ever get.
if (!versions.includes(VERSION)) {
fail(
`${changelogRel}: no "## [${VERSION}]" section for the version in ` +
`packages/core/package.json. Write the release note by hand before the ` +
`release — --fix will not invent one.`,
)
}
// Split body from the trailing block of link definitions.
const footerRe = /(?:^\[[^\]]+\]:[^\n]*\n?)+$/m
const match = raw.match(footerRe)
const expectedFooter = buildFooter(versions)
const currentFooter = match ? match[0].trimEnd() : ''
if (currentFooter !== expectedFooter) {
if (FIX) {
const body = match ? raw.slice(0, match.index) : raw
writeFileSync(changelogAbs, `${body.trimEnd()}\n\n${expectedFooter}\n`)
fixed(`${changelogRel}: rebuilt link-reference footer.`)
} else {
const missing = versions.filter(
(v) =>
!currentFooter.includes(`\n[${v}]:`) &&
!currentFooter.startsWith(`[${v}]:`),
)
fail(
`${changelogRel}: link footer is stale` +
(missing.length ? ` (no definition for ${missing.join(', ')})` : '') +
`. Run \`pnpm run check:versions:fix\`.`,
)
}
}
}
// ── 4. Status lines ─────────────────────────────────────────────────────────
for (const { path: rel, re, shape } of STATUS_LINES) {
const abs = join(repoRoot, rel)
let text
try {
text = readFileSync(abs, 'utf8')
} catch {
fail(`${rel}: missing.`)
continue
}
const match = text.match(re)
if (!match) {
fail(
`${rel}: no "${shape}" line found. The guard keys on that exact ` +
`shape — keep it, or update STATUS_LINES in scripts/check-versions.mjs.`,
)
continue
}
if (match[2] !== VERSION) {
if (FIX) {
writeFileSync(abs, text.replace(re, `$1${VERSION}$3`))
fixed(`${rel}: status version ${match[2]} → ${VERSION}.`)
} else {
fail(
`${rel}: status line says \`${match[2]}\`, expected \`${VERSION}\`. ` +
`Run \`pnpm run check:versions:fix\`.`,
)
}
}
}
// ── 5. Version form: no `v`-prefixed versions in published surfaces ─────────
/** Every file under `root` with a listed extension, recursively. */
function collectFiles(root, exts) {
const abs = join(repoRoot, root)
let entries
try {
entries = readdirSync(abs, { withFileTypes: true })
} catch {
return []
}
const out = []
for (const entry of entries) {
if (FORM_SCAN_IGNORE.has(entry.name)) continue
const rel = join(root, entry.name)
if (entry.isDirectory()) out.push(...collectFiles(rel, exts))
else if (exts.some((ext) => entry.name.endsWith(ext))) out.push(rel)
}
return out
}
for (const { path: root, exts } of FORM_SCAN_ROOTS) {
const files = exts ? collectFiles(root, exts) : [root]
for (const rel of files) {
let text
try {
text = readFileSync(join(repoRoot, rel), 'utf8')
} catch {
continue
}
// Blank URLs out rather than dropping them, so line numbers survive.
const scannable = text.replace(URL_RE, (m) => ' '.repeat(m.length))
scannable.split('\n').forEach((line, i) => {
for (const hit of line.matchAll(V_PREFIX_RE)) {
fail(
`${rel}:${i + 1}: "${hit[0]}" — versions are written as bare three-digit ` +
`semver, never \`v\`-prefixed. Drop the \`v\`.`,
)
}
})
}
}
// ── Report ──────────────────────────────────────────────────────────────────
for (const f of fixes) console.log(`[check-versions] fixed ${f}`)
for (const p of problems) console.error(`[check-versions] ERROR ${p}`)
if (problems.length > 0) {
console.error(
`\n[check-versions] ${problems.length} problem(s) against core version ${VERSION} ` +
`(release tag ${RELEASE_TAG}).`,
)
process.exit(1)
}
console.log(
`[check-versions] ok — @rootnative/impulse at ${VERSION} (release tag ${RELEASE_TAG})` +
`${fixes.length ? `, ${fixes.length} fix(es) applied` : ''}.`,
)