Skip to content

Commit 5eaef6b

Browse files
committed
release: avoid package.json drift during publish
1 parent c5c38ca commit 5eaef6b

3 files changed

Lines changed: 49 additions & 23 deletions

File tree

packages/plugin/script/publish.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,28 @@ async function published(name: string, version: string) {
1111
}
1212

1313
await $`bun tsc`
14-
const pkg = await import("../package.json").then(
15-
(m) => m.default as { name: string; version: string; exports: Record<string, string> },
16-
)
17-
const original = JSON.parse(JSON.stringify(pkg))
14+
const originalText = await Bun.file("package.json").text()
15+
const pkg = JSON.parse(originalText) as {
16+
name: string
17+
version: string
18+
exports: Record<string, string>
19+
}
1820
if (await published(pkg.name, pkg.version)) {
1921
console.log(`already published ${pkg.name}@${pkg.version}`)
20-
process.exit(0)
21-
}
22-
for (const [key, value] of Object.entries(pkg.exports)) {
23-
const file = value.replace("./src/", "./dist/").replace(".ts", "")
24-
// @ts-ignore
25-
pkg.exports[key] = {
26-
import: file + ".js",
27-
types: file + ".d.ts",
22+
} else {
23+
for (const [key, value] of Object.entries(pkg.exports)) {
24+
const file = value.replace("./src/", "./dist/").replace(".ts", "")
25+
// @ts-ignore
26+
pkg.exports[key] = {
27+
import: file + ".js",
28+
types: file + ".d.ts",
29+
}
30+
}
31+
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
32+
try {
33+
await $`bun pm pack`
34+
await $`npm publish *.tgz --tag ${Script.channel} --access public`
35+
} finally {
36+
await Bun.write("package.json", originalText)
2837
}
2938
}
30-
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
31-
await $`bun pm pack && npm publish *.tgz --tag ${Script.channel} --access public`
32-
await Bun.write("package.json", JSON.stringify(original, null, 2))

packages/sdk/js/script/publish.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ async function published(name: string, version: string) {
1111
return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
1212
}
1313

14-
const pkg = (await import("../package.json").then((m) => m.default)) as {
14+
const originalText = await Bun.file("package.json").text()
15+
const pkg = JSON.parse(originalText) as {
1516
name: string
1617
version: string
1718
exports: Record<string, unknown>
1819
}
19-
const original = JSON.parse(JSON.stringify(pkg))
2020
function transformExports(exports: Record<string, unknown>) {
2121
return Object.fromEntries(
2222
Object.entries(exports).map(([key, value]) => {
@@ -33,10 +33,13 @@ function transformExports(exports: Record<string, unknown>) {
3333
}
3434
if (await published(pkg.name, pkg.version)) {
3535
console.log(`already published ${pkg.name}@${pkg.version}`)
36-
process.exit(0)
36+
} else {
37+
pkg.exports = transformExports(pkg.exports)
38+
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
39+
try {
40+
await $`bun pm pack`
41+
await $`npm publish *.tgz --tag ${Script.channel} --access public`
42+
} finally {
43+
await Bun.write("package.json", originalText)
44+
}
3745
}
38-
pkg.exports = transformExports(pkg.exports)
39-
await Bun.write("package.json", JSON.stringify(pkg, null, 2))
40-
await $`bun pm pack`
41-
await $`npm publish *.tgz --tag ${Script.channel} --access public`
42-
await Bun.write("package.json", JSON.stringify(original, null, 2))

script/publish.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,23 @@ const pkgjsons = await Array.fromAsync(
1515
).then((arr) => arr.filter((x) => !x.includes("node_modules") && !x.includes("dist")))
1616

1717
const extensionToml = fileURLToPath(new URL("../packages/extensions/zed/extension.toml", import.meta.url))
18+
const publishPackageJsons = ["packages/plugin/package.json", "packages/sdk/js/package.json"]
1819

1920
async function hasChanges() {
2021
return (await $`git diff --quiet && git diff --cached --quiet`.nothrow()).exitCode !== 0
2122
}
2223

24+
async function hasPublishPackageJsonChanges() {
25+
if ((await $`git diff --quiet -- ${publishPackageJsons}`.nothrow()).exitCode !== 0) return true
26+
return (await $`git diff --cached --quiet -- ${publishPackageJsons}`.nothrow()).exitCode !== 0
27+
}
28+
29+
async function logPublishPackageJsonChanges() {
30+
await $`git status --short -- ${publishPackageJsons}`
31+
await $`git diff -- ${publishPackageJsons}`
32+
await $`git diff --cached -- ${publishPackageJsons}`
33+
}
34+
2335
async function releaseTagExists() {
2436
return (await $`git rev-parse -q --verify refs/tags/${tag}`.nothrow()).exitCode === 0
2537
}
@@ -76,6 +88,11 @@ if (Script.release) {
7688

7789
if (Script.release && !Script.preview) {
7890
await $`git fetch origin`
91+
if (await hasPublishPackageJsonChanges()) {
92+
console.error("publish scripts left package.json changes before syncing dev")
93+
await logPublishPackageJsonChanges()
94+
throw new Error("packages/plugin/package.json or packages/sdk/js/package.json changed during publish")
95+
}
7996
await $`git checkout -B dev origin/dev`
8097
await prepareReleaseFiles()
8198
if (await hasChanges()) {

0 commit comments

Comments
 (0)