changelog.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env bun
  2. import { rm } from "fs/promises"
  3. import path from "path"
  4. import { parseArgs } from "util"
  5. const root = path.resolve(import.meta.dir, "..")
  6. const file = path.join(root, "UPCOMING_CHANGELOG.md")
  7. const { values, positionals } = parseArgs({
  8. args: Bun.argv.slice(2),
  9. options: {
  10. from: { type: "string", short: "f" },
  11. to: { type: "string", short: "t" },
  12. variant: { type: "string", default: "low" },
  13. quiet: { type: "boolean", default: false },
  14. print: { type: "boolean", default: false },
  15. help: { type: "boolean", short: "h", default: false },
  16. },
  17. allowPositionals: true,
  18. })
  19. const args = [...positionals]
  20. if (values.from) args.push("--from", values.from)
  21. if (values.to) args.push("--to", values.to)
  22. if (values.help) {
  23. console.log(`
  24. Usage: bun script/changelog.ts [options]
  25. Generates UPCOMING_CHANGELOG.md by running the opencode changelog command.
  26. Options:
  27. -f, --from <version> Starting version (default: latest non-draft GitHub release)
  28. -t, --to <ref> Ending ref (default: HEAD)
  29. --variant <name> Thinking variant for opencode run (default: low)
  30. --quiet Suppress opencode command output unless it fails
  31. --print Print the generated UPCOMING_CHANGELOG.md after success
  32. -h, --help Show this help message
  33. Examples:
  34. bun script/changelog.ts
  35. bun script/changelog.ts --from 1.0.200
  36. bun script/changelog.ts -f 1.0.200 -t 1.0.205
  37. `)
  38. process.exit(0)
  39. }
  40. await rm(file, { force: true })
  41. const quiet = values.quiet
  42. const cmd = ["opencode", "run"]
  43. cmd.push("--variant", values.variant)
  44. cmd.push("--command", "changelog", "--", ...args)
  45. const proc = Bun.spawn(cmd, {
  46. cwd: root,
  47. stdin: "inherit",
  48. stdout: quiet ? "pipe" : "inherit",
  49. stderr: quiet ? "pipe" : "inherit",
  50. })
  51. const [out, err] = quiet
  52. ? await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()])
  53. : ["", ""]
  54. const code = await proc.exited
  55. if (code === 0) {
  56. if (values.print) process.stdout.write(await Bun.file(file).text())
  57. process.exit(0)
  58. }
  59. if (quiet) {
  60. if (out) process.stdout.write(out)
  61. if (err) process.stderr.write(err)
  62. }
  63. process.exit(code)