Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions bin/release
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,9 @@ published || folded=$(pnpm exec cli-dev changelog fold --land --version "$versio
[ -z "$folded" ] || echo "$folded"
rerun_if_changed "$@"

# Two sessions preparing the same number is normal; the second takes the next free one.
latest=$(npm view "$package" version --prefer-online 2>/dev/null || true)
versions=$(npm view "$package" versions --json --prefer-online 2>/dev/null || true)
next=$(pnpm exec cli-dev next-version "$version" "$latest" "$versions")
fragments=$(find changes -maxdepth 1 -type f -name '*.md' ! -name README.md 2>/dev/null | wc -l)
if [ "$next" != "$version" ]; then
taken=""
if published; then taken=1; why="$version is already on npm"; else why="$version is not above npm's latest $latest"; fi
next=$(pnpm exec cli-dev changelog level --next "$next")
echo "$why — publishing $next instead"
# A version npm already has keeps its changelog section; the unreleased one becomes the new version.
# Raises the version from $1 to $2 in package.json, src/version.ts and the CHANGELOG heading. With $3 set, npm has
# $1, so its section stays and the unreleased one becomes $2.
renumber() {
node -e 'const fs = require("fs"); const [o, n, taken] = process.argv.slice(1)
const swap = (file, from, to) => {
const text = fs.readFileSync(file, "utf8")
Expand All @@ -146,10 +138,30 @@ if [ "$next" != "$version" ]; then
const log = fs.readFileSync("CHANGELOG.md", "utf8")
if (taken && log.includes("\n## Unreleased\n")) swap("CHANGELOG.md", "\n## Unreleased\n", `\n## ${n} — ${today}\n`)
else if (taken) swap("CHANGELOG.md", "\n## ", `\n## ${n} — ${today}\n\n## `)
else swap("CHANGELOG.md", `\n## ${o} — `, `\n## ${n} — `)' "$version" "$next" "$taken"
else swap("CHANGELOG.md", `\n## ${o} — `, `\n## ${n} — `)' "$1" "$2" "$3"
}

# Two sessions preparing the same number is normal; the second takes the next free one.
latest=$(npm view "$package" version --prefer-online 2>/dev/null || true)
versions=$(npm view "$package" versions --json --prefer-online 2>/dev/null || true)
next=$(pnpm exec cli-dev next-version "$version" "$latest" "$versions")
fragments=$(find changes -maxdepth 1 -type f -name '*.md' ! -name README.md 2>/dev/null | wc -l)
if [ "$next" != "$version" ]; then
taken=""
if published; then taken=1; why="$version is already on npm"; else why="$version is not above npm's latest $latest"; fi
renumber "$version" "$next" "$taken"
pnpm exec cli-dev changelog fragments --collect "$next"
body="bin/release: $why. Only the number changes: package.json, src/version.ts, the CHANGELOG heading, and the
changes/ fragments collected under it.
# next-version keeps the level of the version it raises; the section, with the fragments now in it, may need more:
# an Added is a minor.
levelled=$(pnpm exec cli-dev changelog level --next "$next")
if [ "$levelled" != "$next" ]; then
levelled=$(pnpm exec cli-dev next-version "$levelled" "$latest" "$versions")
renumber "$next" "$levelled" ""
next=$levelled
fi
echo "$why — publishing $next instead"
body="bin/release: $why. Only the number changes, to the level the changelog needs: package.json, src/version.ts,
the CHANGELOG heading, and the changes/ fragments collected under it.

## Conformance

Expand All @@ -158,10 +170,20 @@ The release pull request of $version recorded the checks; this one changes only
rerun_if_changed "$@"
version=$next
elif [ "$fragments" -gt 0 ]; then
# The version was dated by hand; fragments merged since then still belong to it.
# The version was dated by hand; fragments merged since then still belong to it, and may need a higher level.
pnpm exec cli-dev changelog fragments --collect "$version"
land "release/$version-changes" "chore(changelog): collect changes/ into $version" \
"bin/release: the changes/ fragments merged since $version was dated, collected into its section."
levelled=$(pnpm exec cli-dev changelog level --next "$version")
if [ "$levelled" != "$version" ]; then
levelled=$(pnpm exec cli-dev next-version "$levelled" "$latest" "$versions")
renumber "$version" "$levelled" ""
echo "the changes/ fragments need $levelled — publishing it instead of $version"
land "release/$levelled" "chore(release): $levelled" \
"bin/release: the changes/ fragments merged since $version was dated need $levelled, collected into its section."
version=$levelled
else
land "release/$version-changes" "chore(changelog): collect changes/ into $version" \
"bin/release: the changes/ fragments merged since $version was dated, collected into its section."
fi
rerun_if_changed "$@"
fi
top=$(sed -n '/^## /{p;q;}' CHANGELOG.md)
Expand Down
11 changes: 11 additions & 0 deletions changes/fix-release-level-after-fragments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Fixed

- **`bin/release` picks the version after it collects `changes/`.** When npm already had the version, it chose the
next one from the changelog before the fragments were in it, so a fragment with `Added` still gave a patch, and
`version:check` then refused the release pull request (wordpress-cli 0.1.3, which had to be 0.2.0). The fragments
are now collected first, and an `Added` raises the release to a minor. A version dated by hand is raised the same
way when the fragments merged since need more. `scripts/release.test.ts` checks both and is kept up to date by
`upgrade`, as `bin/release` is. A CLI gets this with its next `upgrade`.
- **The template builds on cli-core 0.718.0 and cli-base 0.126.1**, so `pnpm audit:commands` passes the family audit
again: the published CLIs still name an unknown profile twice, and cli-base 0.126.1 lists that as a shared
difference.
1 change: 1 addition & 0 deletions contracts/help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Works with:
notion Notion pages and databases as local text, searchable with your other WireCat data
ydisk Yandex Disk folders as local text, searchable with your other WireCat data
wordpress WordPress posts and pages, searchable with your other WireCat data
linear Linear issues and projects, searchable with your other WireCat data

Next: clidev commands every command, by section
clidev help options output, offline, timeouts, dry run
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@
"kit:docs": "cspell lint --config cspell.json --no-progress --no-summary kit/*.md SKILL.md && rumdl check --config config/wirecat/rumdl.toml kit/*.md SKILL.md"
},
"dependencies": {
"@wirecat/cli-base": "0.125.2",
"@wirecat/cli-core": "0.710.0",
"@wirecat/cli-base": "0.126.1",
"@wirecat/cli-core": "0.718.0",
"commander": "15.0.0",
"valibot": "1.5.0",
"yaml": "2.9.1"
Expand Down
24 changes: 12 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions scripts/release.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { execFileSync, spawnSync } from "node:child_process"
import { chmodSync, cpSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { afterEach, expect, it } from "vitest"

const root = join(import.meta.dirname, "..")
const devBin = join(root, "node_modules/@wirecat/cli-base/dist/dev/bin.js")
const dirs: string[] = []
afterEach(() => {
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true })
})

const git = (cwd: string, ...args: string[]) => execFileSync("git", args, { cwd, encoding: "utf8" })

const stub = (dir: string, name: string, body: string) => {
writeFileSync(join(dir, name), `#!/bin/sh\n${body}\n`)
chmodSync(join(dir, name), 0o755)
}

// npm has 0.1.2, so bin/release raises the version through a pull request. gh refuses to open it, which stops the run
// right after the release branch is pushed, before anything could publish.
const raised = (fragment: string, dated = "") => {
const work = mkdtempSync(join(tmpdir(), "release-level-"))
dirs.push(work)
const origin = join(work, "origin.git")
const repo = join(work, "repo")
const stubs = join(work, "bin")
mkdirSync(stubs)
git(work, "init", "-q", "--bare", "-b", "main", origin)
git(work, "clone", "-q", origin, repo)
mkdirSync(join(repo, "bin"))
mkdirSync(join(repo, "config/wirecat"), { recursive: true })
cpSync(join(root, "bin/release"), join(repo, "bin/release"))
for (const file of ["release-lock.sh", "release-lock-info.mjs"])
cpSync(join(root, "config/wirecat", file), join(repo, "config/wirecat", file))
mkdirSync(join(repo, "kit"))
writeFileSync(join(repo, "kit/.keep"), "")
mkdirSync(join(repo, "src"))
mkdirSync(join(repo, "changes"))
writeFileSync(
join(repo, "package.json"),
`${JSON.stringify({ name: "@example/acme-cli", version: dated ? "0.1.3" : "0.1.2" }, null, 2)}\n`,
)
writeFileSync(join(repo, "src/version.ts"), `export const VERSION = "${dated ? "0.1.3" : "0.1.2"}"\n`)
writeFileSync(
join(repo, "CHANGELOG.md"),
`# Changelog\n\n${dated}## 0.1.2 — 19.11.2026\n\n### Fixed\n\n- **One.** A fix.\n`,
)
writeFileSync(join(repo, "changes/feat-pages.md"), fragment)
git(repo, "add", "-A")
git(repo, "-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "-m", "init")
git(repo, "tag", "v0.1.2")
git(repo, "push", "-q", "origin", "main", "v0.1.2")

stub(
stubs,
"pnpm",
`[ "$1" = install ] && exit 0\n[ "$1 $2" = "exec cli-dev" ] && shift 2 && exec node "${devBin}" "$@"\nexit 1`,
)
stub(
stubs,
"npm",
'case "$*" in\n *" versions "*) echo \'["0.1.2"]\' ;;\n "view @example/acme-cli version "*|"view @example/acme-cli@0.1.2 version "*) echo 0.1.2 ;;\n *) exit 1 ;;\nesac',
)
stub(stubs, "gh", "exit 1")
const run = spawnSync("sh", ["bin/release"], {
cwd: repo,
encoding: "utf8",
env: {
...process.env,
PATH: `${stubs}:${process.env.PATH}`,
XDG_RUNTIME_DIR: work,
GIT_AUTHOR_NAME: "t",
GIT_AUTHOR_EMAIL: "t@example.com",
GIT_COMMITTER_NAME: "t",
GIT_COMMITTER_EMAIL: "t@example.com",
},
})
const branches = git(origin, "branch", "--format=%(refname:short)").split("\n").filter(Boolean)
return { run, branches, show: (branch: string, file: string) => git(origin, "show", `${branch}:${file}`) }
}

it("raises a taken version to the level its changes/ fragments need: an Added is a minor", () => {
const { run, branches, show } = raised("### Added\n\n- **Pages.** Pages are written.\n")
expect(run.stdout).toContain("0.1.2 is already on npm — publishing 0.2.0 instead")
expect(branches).toContain("release/0.2.0")
expect(JSON.parse(show("release/0.2.0", "package.json")).version).toBe("0.2.0")
expect(show("release/0.2.0", "src/version.ts")).toContain('VERSION = "0.2.0"')
expect(show("release/0.2.0", "CHANGELOG.md")).toMatch(/## 0\.2\.0 — [\d.]+\n\n### Added\n\n- \*\*Pages\.\*\*/)
})

it("keeps a patch for fragments that only fix", () => {
const { run, branches } = raised("### Fixed\n\n- **Two.** Another fix.\n")
expect(run.stdout).toContain("publishing 0.1.3 instead")
expect(branches).toContain("release/0.1.3")
})

it("raises a version dated by hand when the fragments merged since need more", () => {
const { run, branches, show } = raised(
"### Added\n\n- **Pages.** Pages are written.\n",
"## 0.1.3 — 20.11.2026\n\n### Fixed\n\n- **Two.** Another fix.\n\n",
)
expect(run.stdout).toContain("the changes/ fragments need 0.2.0 — publishing it instead of 0.1.3")
expect(branches).toContain("release/0.2.0")
expect(JSON.parse(show("release/0.2.0", "package.json")).version).toBe("0.2.0")
expect(show("release/0.2.0", "CHANGELOG.md")).toMatch(/## 0\.2\.0 — 20\.11\.2026\n\n### Added[\s\S]*### Fixed/)
})
1 change: 1 addition & 0 deletions template.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"docs/dev/TESTING.md",
"lefthook.yml",
"scripts/one-cli-base.ts",
"scripts/release.test.ts",
"scripts/sandboxed.ts",
"src/test-timeouts.test.ts",
"tsconfig.json",
Expand Down
Loading