publish.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import pkg from "../package.json"
  4. import { Script } from "@opencode-ai/script"
  5. import { fileURLToPath } from "url"
  6. const dir = fileURLToPath(new URL("..", import.meta.url))
  7. process.chdir(dir)
  8. async function published(name: string, version: string) {
  9. return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
  10. }
  11. async function publish(dir: string, name: string, version: string) {
  12. if (process.platform !== "win32") await $`chmod -R 755 .`.cwd(dir)
  13. if (await published(name, version)) return console.log(`already published ${name}@${version}`)
  14. await $`bun pm pack`.cwd(dir)
  15. await $`npm publish *.tgz --access public --tag ${Script.channel}`.cwd(dir)
  16. }
  17. const binaries: Record<string, string> = {}
  18. for (const filepath of new Bun.Glob("*/package.json").scanSync({ cwd: "./dist" })) {
  19. const item = await Bun.file(`./dist/${filepath}`).json()
  20. binaries[item.name] = item.version
  21. }
  22. console.log("binaries", binaries)
  23. const version = Object.values(binaries)[0]
  24. await $`mkdir -p ./dist/${pkg.name}/bin`
  25. await $`cp ./bin/lildax.cjs ./dist/${pkg.name}/bin/lildax`
  26. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  27. JSON.stringify(
  28. {
  29. name: pkg.name,
  30. bin: { lildax: "./bin/lildax" },
  31. version,
  32. license: pkg.license,
  33. repository: { type: "git", url: "git+https://github.com/anomalyco/opencode.git" },
  34. os: ["darwin", "linux", "win32"],
  35. cpu: ["arm64", "x64"],
  36. optionalDependencies: binaries,
  37. },
  38. null,
  39. 2,
  40. ),
  41. )
  42. await Promise.all(
  43. Object.entries(binaries).map(([name, version]) =>
  44. publish(`./dist/${name.replace("@opencode-ai/", "")}`, name, version),
  45. ),
  46. )
  47. await publish(`./dist/${pkg.name}`, pkg.name, version)