release: avoid package.json drift during publish

This commit is contained in:
Dax Raad
2026-04-18 12:31:59 -04:00
parent c5c38cad9c
commit 5eaef6b758
3 changed files with 49 additions and 23 deletions

View File

@@ -11,22 +11,28 @@ async function published(name: string, version: string) {
}
await $`bun tsc`
const pkg = await import("../package.json").then(
(m) => m.default as { name: string; version: string; exports: Record<string, string> },
)
const original = JSON.parse(JSON.stringify(pkg))
const originalText = await Bun.file("package.json").text()
const pkg = JSON.parse(originalText) as {
name: string
version: string
exports: Record<string, string>
}
if (await published(pkg.name, pkg.version)) {
console.log(`already published ${pkg.name}@${pkg.version}`)
process.exit(0)
}
for (const [key, value] of Object.entries(pkg.exports)) {
const file = value.replace("./src/", "./dist/").replace(".ts", "")
// @ts-ignore
pkg.exports[key] = {
import: file + ".js",
types: file + ".d.ts",
} else {
for (const [key, value] of Object.entries(pkg.exports)) {
const file = value.replace("./src/", "./dist/").replace(".ts", "")
// @ts-ignore
pkg.exports[key] = {
import: file + ".js",
types: file + ".d.ts",
}
}
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
try {
await $`bun pm pack`
await $`npm publish *.tgz --tag ${Script.channel} --access public`
} finally {
await Bun.write("package.json", originalText)
}
}
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
await $`bun pm pack && npm publish *.tgz --tag ${Script.channel} --access public`
await Bun.write("package.json", JSON.stringify(original, null, 2))

View File

@@ -11,12 +11,12 @@ async function published(name: string, version: string) {
return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
}
const pkg = (await import("../package.json").then((m) => m.default)) as {
const originalText = await Bun.file("package.json").text()
const pkg = JSON.parse(originalText) as {
name: string
version: string
exports: Record<string, unknown>
}
const original = JSON.parse(JSON.stringify(pkg))
function transformExports(exports: Record<string, unknown>) {
return Object.fromEntries(
Object.entries(exports).map(([key, value]) => {
@@ -33,10 +33,13 @@ function transformExports(exports: Record<string, unknown>) {
}
if (await published(pkg.name, pkg.version)) {
console.log(`already published ${pkg.name}@${pkg.version}`)
process.exit(0)
} else {
pkg.exports = transformExports(pkg.exports)
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
try {
await $`bun pm pack`
await $`npm publish *.tgz --tag ${Script.channel} --access public`
} finally {
await Bun.write("package.json", originalText)
}
}
pkg.exports = transformExports(pkg.exports)
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
await $`bun pm pack`
await $`npm publish *.tgz --tag ${Script.channel} --access public`
await Bun.write("package.json", JSON.stringify(original, null, 2))

View File

@@ -15,11 +15,23 @@ const pkgjsons = await Array.fromAsync(
).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
const extensionToml = fileURLToPath(new URL("../packages/extensions/zed/extension.toml", import.meta.url))
const publishPackageJsons = ["packages/plugin/package.json", "packages/sdk/js/package.json"]
async function hasChanges() {
return (await $`git diff --quiet && git diff --cached --quiet`.nothrow()).exitCode !== 0
}
async function hasPublishPackageJsonChanges() {
if ((await $`git diff --quiet -- ${publishPackageJsons}`.nothrow()).exitCode !== 0) return true
return (await $`git diff --cached --quiet -- ${publishPackageJsons}`.nothrow()).exitCode !== 0
}
async function logPublishPackageJsonChanges() {
await $`git status --short -- ${publishPackageJsons}`
await $`git diff -- ${publishPackageJsons}`
await $`git diff --cached -- ${publishPackageJsons}`
}
async function releaseTagExists() {
return (await $`git rev-parse -q --verify refs/tags/${tag}`.nothrow()).exitCode === 0
}
@@ -76,6 +88,11 @@ if (Script.release) {
if (Script.release && !Script.preview) {
await $`git fetch origin`
if (await hasPublishPackageJsonChanges()) {
console.error("publish scripts left package.json changes before syncing dev")
await logPublishPackageJsonChanges()
throw new Error("packages/plugin/package.json or packages/sdk/js/package.json changed during publish")
}
await $`git checkout -B dev origin/dev`
await prepareReleaseFiles()
if (await hasChanges()) {