|
| 1 | +#!/usr/bin/env node |
| 2 | +// Checks the pending changesets before `changeset version` consumes them: |
| 3 | +// - each file parses, with changesets' own parser |
| 4 | +// - each released package exists in the workspace and is published |
| 5 | +// - a major release includes every published package depending on it: dependencies are |
| 6 | +// npm aliases (workspace:@yolean/x@^), which changesets doesn't bump, and a dependent |
| 7 | +// left out stays published against the previous major |
| 8 | +import { execFileSync } from 'node:child_process'; |
| 9 | +import fs from 'node:fs'; |
| 10 | +import readChangesets from '@changesets/read'; |
| 11 | + |
| 12 | +const read = readChangesets.default ?? readChangesets; |
| 13 | +const DEP_FIELDS = ['dependencies', 'peerDependencies', 'optionalDependencies']; |
| 14 | + |
| 15 | +const packages = execFileSync('git', ['ls-files', '*package.json'], { encoding: 'utf8' }) |
| 16 | + .split('\n') |
| 17 | + .filter((f) => f && !f.includes('node_modules')) |
| 18 | + .map((f) => ({ file: f, ...JSON.parse(fs.readFileSync(f, 'utf8')) })) |
| 19 | + .filter((p) => p.name); |
| 20 | +const byName = new Map(packages.map((p) => [p.name, p])); |
| 21 | + |
| 22 | +/** The workspace package a dependency spec points at, if any */ |
| 23 | +function workspaceTarget(key, spec) { |
| 24 | + if (typeof spec !== 'string' || !spec.startsWith('workspace:')) return null; |
| 25 | + const m = /^workspace:((?:@[^/@]+\/)?[^@]+)@/.exec(spec); |
| 26 | + return m ? m[1] : key; |
| 27 | +} |
| 28 | + |
| 29 | +const parseFailed = (e) => { |
| 30 | + console.error(`changeset check failed, a .changeset file doesn't parse:\n ${e.message}`); |
| 31 | + process.exit(1); |
| 32 | +}; |
| 33 | +// @changesets/read leaves a parse error as an unhandled rejection |
| 34 | +process.on('unhandledRejection', parseFailed); |
| 35 | +const changesets = await read(process.cwd()).catch(parseFailed); |
| 36 | +const problems = []; |
| 37 | +/** @type {Map<string, string>} */ |
| 38 | +const releases = new Map(); |
| 39 | +for (const cs of changesets) { |
| 40 | + for (const r of cs.releases) { |
| 41 | + const pkg = byName.get(r.name); |
| 42 | + if (!pkg) problems.push(`.changeset/${cs.id}.md: ${r.name} is not a workspace package`); |
| 43 | + else if (pkg.private) problems.push(`.changeset/${cs.id}.md: ${r.name} is private`); |
| 44 | + const rank = ['patch', 'minor', 'major']; |
| 45 | + if (rank.indexOf(r.type) > rank.indexOf(releases.get(r.name) ?? 'none')) |
| 46 | + releases.set(r.name, r.type); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +for (const [name, type] of releases) { |
| 51 | + if (type !== 'major') continue; |
| 52 | + for (const p of packages) { |
| 53 | + if (p.private || releases.has(p.name)) continue; |
| 54 | + const deps = DEP_FIELDS.flatMap((f) => Object.entries(p[f] ?? {})); |
| 55 | + if (deps.some(([k, v]) => workspaceTarget(k, v) === name)) { |
| 56 | + problems.push(`${name} has a major release, add ${p.name} (${p.file}) to the changeset too`); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +if (problems.length) { |
| 62 | + console.error(`changeset check failed:\n ${problems.join('\n ')}`); |
| 63 | + process.exit(1); |
| 64 | +} |
| 65 | +console.log( |
| 66 | + `changeset check: ${changesets.length} changesets, ${releases.size} packages${releases.size ? `: ${[...releases].map(([n, t]) => `${n} ${t}`).join(', ')}` : ''}` |
| 67 | +); |
0 commit comments