-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathpublish.ts
72 lines (59 loc) · 2.21 KB
/
publish.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bun
import * as Bun from "bun"
import { parseArgs } from "node:util"
import { consola } from "./logger.ts"
import jsrJson from "../jsr.json" with { type: "json" }
import packageJson from "../package.json" with { type: "json" }
const CURRENT_JSR_JSON_VERSION = jsrJson.version
const CURRENT_PACKAGE_JSON_VERSION = packageJson.version
/**
* Use this script to publish a new version of the TypeScript SDK to JSR registry
* This will check if the contracts in the SDK are up to date with the contracts in the registry
* If not it will fail
*
* Usage:
*
* `bun scripts/publish --period patch`
* `bun scripts/publish --period minor --dry-run`
*/
const { values } = parseArgs({
args: process.argv.slice(2),
strict: true,
options: {
period: { type: "string", default: "patch" },
"dry-run": { type: "boolean", default: false }
}
})
const PERIOD = values.period ?? "patch"
const DRY_RUN = values["dry-run"] ?? false
main().catch(_ => {
consola.error(_)
process.exit(1)
})
async function main() {
try {
if (DRY_RUN) {
return await Bun.$ /* sh */`bunx jsr publish --allow-dirty --allow-slow-types --dry-run`
}
const bumpPackage = await Bun.$ /* sh */`npm version --preiod ${PERIOD} --no-git-tag-version`
const version = bumpPackage.text().trim().replace(/^v/, "")
// sync jsr.json version with package.json version
const syncJsr =
await Bun.$ /* sh */`jq --arg version "${version}" '.version = $version' jsr.json > jsr.temp.json && mv jsr.temp.json jsr.json`
consola.info("Sync jsr.json version with package.json version", syncJsr.text())
return await Bun.$ /* sh */`bunx jsr publish --allow-dirty --allow-slow-types`
} catch (error) {
const errorMessage = error instanceof Error ? error.message : error
consola.error(errorMessage)
// revert changes
await resetVersions()
consola.info("Reset package.json version")
return
}
}
async function resetVersions() {
const currentVersion = await Bun.$ /* sh */`jq --raw-output .version package.json`.text()
const newVersion =
await Bun.$ /* sh */`${currentVersion.trim()} | awk -F'[.-]' '{print $1"."$2"."$3"-"$4"."$6-1}'`
await Bun.$ /* sh */`npm version ${newVersion} --no-git-tag-version`
}