-
-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add utils for PR-driven release #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
09cf675
feat: add utils for PR-driven release
sapphi-red 6dc95ad
chore: fix
sapphi-red 39f64fe
chore: fix
sapphi-red a636fea
chore: update
sapphi-red 3261da0
chore: update README
sapphi-red c6fc7bf
chore: add release-pr action
sapphi-red File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import type { | ||
| detectReleaseCommit as detectReleaseCommitDef, | ||
| getReleaseTag as getReleaseTagDef, | ||
| isReleaseCommitSubject as isReleaseCommitSubjectDef, | ||
| } from "./types.d.ts"; | ||
|
|
||
| export const getReleaseTag: typeof getReleaseTagDef = (pkg, version, defaultPackage) => { | ||
| return pkg === defaultPackage ? `v${version}` : `${pkg}@${version}`; | ||
| }; | ||
|
|
||
| export const isReleaseCommitSubject: typeof isReleaseCommitSubjectDef = (subject, tag) => { | ||
| const expected = `release: ${tag}`; | ||
| return ( | ||
| subject === expected || | ||
| (subject.startsWith(expected) && /^ \(#\d+\)$/.test(subject.slice(expected.length))) | ||
| ); | ||
| }; | ||
|
|
||
| export const detectReleaseCommit: typeof detectReleaseCommitDef = ({ | ||
| subject, | ||
| packages, | ||
| defaultPackage, | ||
| getPkgDir = (pkg) => `packages/${pkg}`, | ||
| toTag = (pkg, version) => getReleaseTag(pkg, version, defaultPackage), | ||
| }) => { | ||
| for (const pkg of packages) { | ||
| const pkgPath = path.resolve(getPkgDir(pkg), "package.json"); | ||
| const packageJson = JSON.parse(fs.readFileSync(pkgPath, "utf-8")) as { | ||
| version?: unknown; | ||
| }; | ||
| if (typeof packageJson.version !== "string") { | ||
| throw new Error(`Package ${JSON.stringify(pkg)} does not have a valid version`); | ||
| } | ||
|
|
||
| const version = packageJson.version; | ||
| const tag = toTag(pkg, version); | ||
| if (isReleaseCommitSubject(subject, tag)) return { pkg, version, tag }; | ||
| } | ||
| return undefined; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| export { publish } from "./publish.ts"; | ||
| export { getPublishTag, publish, validatePublishVersion } from "./publish.ts"; | ||
| export { release } from "./release.ts"; | ||
| export { generateChangelog } from "./changelog.ts"; | ||
| export { extractChangelogEntry, generateChangelog } from "./changelog.ts"; | ||
| export { detectReleaseCommit, getReleaseTag, isReleaseCommitSubject } from "./detectRelease.ts"; | ||
| export { prepareRelease } from "./prepare.ts"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import semver from "semver"; | ||
| import type { ReleaseType } from "semver"; | ||
| import { getReleaseTag } from "./detectRelease.ts"; | ||
| import { validatePublishVersion } from "./publish.ts"; | ||
| import type { prepareRelease as prepareReleaseDef } from "./types.d.ts"; | ||
| import { updateVersion } from "./utils.ts"; | ||
|
|
||
| function resolveVersion(currentVersion: string, release: string, preid: string): string { | ||
| if (semver.valid(release)) return release; | ||
|
|
||
| const releaseType = | ||
| release === "next" ? (semver.prerelease(currentVersion) ? "prerelease" : "patch") : release; | ||
| if (!semver.RELEASE_TYPES.includes(releaseType as ReleaseType)) { | ||
| throw new Error(`Invalid Version: ${release}`); | ||
| } | ||
|
|
||
| let version = semver.inc(currentVersion, releaseType as ReleaseType, preid); | ||
| if (!version) throw new Error(`Invalid Version: ${release}`); | ||
|
|
||
| const prerelease = semver.prerelease(version); | ||
| if (releaseType.startsWith("pre") && prerelease?.[0] === preid && prerelease[1] === 0) { | ||
| version = semver.inc(version, "prerelease", preid)!; | ||
| } | ||
| return version; | ||
| } | ||
|
|
||
| export const prepareRelease: typeof prepareReleaseDef = async ({ | ||
| packages, | ||
| pkg, | ||
| release, | ||
| preid, | ||
| getPkgDir = (pkgName) => `packages/${pkgName}`, | ||
| toTag = (pkgName, version) => getReleaseTag(pkgName, version), | ||
| generateChangelog, | ||
| }) => { | ||
| if (!pkg || (packages && !packages.includes(pkg))) { | ||
| const expected = packages?.length ? ` Expected one of: ${packages.join(", ")}` : ""; | ||
| throw new Error(`Invalid release package ${JSON.stringify(pkg)}.${expected}`); | ||
| } | ||
|
|
||
| const pkgDir = path.resolve(getPkgDir(pkg)); | ||
| const pkgPath = path.join(pkgDir, "package.json"); | ||
| if (semver.valid(release)) validatePublishVersion(release); | ||
| const packageJson = JSON.parse(fs.readFileSync(pkgPath, "utf-8")) as { | ||
| version?: unknown; | ||
| }; | ||
| if (typeof packageJson.version !== "string") { | ||
| throw new Error(`Package ${JSON.stringify(pkg)} does not have a valid version`); | ||
| } | ||
| const currentPrerelease = semver.prerelease(packageJson.version); | ||
| const currentPreid = | ||
| typeof currentPrerelease?.[0] === "string" ? currentPrerelease[0] : undefined; | ||
|
|
||
| const previousVersion = packageJson.version; | ||
| const version = resolveVersion(previousVersion, release, preid ?? currentPreid ?? "beta"); | ||
| validatePublishVersion(version); | ||
| updateVersion(pkgPath, version); | ||
|
|
||
| await generateChangelog?.(pkg, version); | ||
|
|
||
| return { pkg, previousVersion, version, tag: toTag(pkg, version) }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.