|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import solidPlugin from "../node_modules/@opentui/solid/scripts/solid-plugin" |
| 4 | +import path from "path" |
| 5 | +import fs from "fs" |
| 6 | +import { $ } from "bun" |
| 7 | + |
| 8 | +const dir = path.resolve(import.meta.dirname, "..") |
| 9 | +process.chdir(dir) |
| 10 | + |
| 11 | +const pkg = JSON.parse(await Bun.file(path.join(dir, "package.json")).text()) |
| 12 | +const version = pkg.version |
| 13 | + |
| 14 | +// Load migration SQL |
| 15 | +const migrationFile = path.join(dir, "drizzle", "0000_init.sql") |
| 16 | +const migrationSQL = await Bun.file(migrationFile).text() |
| 17 | + |
| 18 | +const singleFlag = process.argv.includes("--single") |
| 19 | + |
| 20 | +const allTargets: { os: string; arch: "arm64" | "x64" }[] = [ |
| 21 | + { os: "darwin", arch: "arm64" }, |
| 22 | + { os: "darwin", arch: "x64" }, |
| 23 | + { os: "linux", arch: "arm64" }, |
| 24 | + { os: "linux", arch: "x64" }, |
| 25 | + { os: "win32", arch: "x64" }, |
| 26 | +] |
| 27 | + |
| 28 | +const targets = singleFlag |
| 29 | + ? allTargets.filter((t) => t.os === process.platform && t.arch === process.arch) |
| 30 | + : allTargets |
| 31 | + |
| 32 | +await $`rm -rf dist` |
| 33 | + |
| 34 | +// Install platform-specific native deps for cross-compilation |
| 35 | +if (!singleFlag) { |
| 36 | + await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}` |
| 37 | +} |
| 38 | + |
| 39 | +for (const item of targets) { |
| 40 | + const name = [ |
| 41 | + "codeblog-app", |
| 42 | + item.os === "win32" ? "windows" : item.os, |
| 43 | + item.arch, |
| 44 | + ].join("-") |
| 45 | + |
| 46 | + console.log(`building ${name}...`) |
| 47 | + await $`mkdir -p dist/${name}/bin` |
| 48 | + |
| 49 | + const binary = item.os === "win32" ? "codeblog.exe" : "codeblog" |
| 50 | + |
| 51 | + await Bun.build({ |
| 52 | + conditions: ["browser"], |
| 53 | + tsconfig: "./tsconfig.json", |
| 54 | + plugins: [solidPlugin], |
| 55 | + sourcemap: "none", |
| 56 | + compile: { |
| 57 | + autoloadBunfig: false, |
| 58 | + autoloadDotenv: false, |
| 59 | + // @ts-ignore |
| 60 | + autoloadTsconfig: true, |
| 61 | + autoloadPackageJson: true, |
| 62 | + target: name.replace("codeblog-app", "bun") as any, |
| 63 | + outfile: `dist/${name}/bin/${binary}`, |
| 64 | + execArgv: ["--use-system-ca", "--"], |
| 65 | + windows: {}, |
| 66 | + }, |
| 67 | + entrypoints: ["./src/index.ts"], |
| 68 | + define: { |
| 69 | + CODEBLOG_VERSION: `'${version}'`, |
| 70 | + CODEBLOG_MIGRATION_SQL: JSON.stringify(migrationSQL), |
| 71 | + }, |
| 72 | + }) |
| 73 | + |
| 74 | + // Write platform package.json |
| 75 | + await Bun.file(`dist/${name}/package.json`).write( |
| 76 | + JSON.stringify( |
| 77 | + { |
| 78 | + name, |
| 79 | + version, |
| 80 | + description: `CodeBlog CLI binary for ${item.os} ${item.arch}`, |
| 81 | + os: [item.os], |
| 82 | + cpu: [item.arch], |
| 83 | + bin: { codeblog: `bin/${binary}` }, |
| 84 | + }, |
| 85 | + null, |
| 86 | + 2, |
| 87 | + ), |
| 88 | + ) |
| 89 | + |
| 90 | + console.log(` ✓ ${name}`) |
| 91 | +} |
| 92 | + |
| 93 | +// Create main wrapper package |
| 94 | +await $`mkdir -p dist/codeblog-app` |
| 95 | +const optionalDeps: Record<string, string> = {} |
| 96 | +for (const item of targets) { |
| 97 | + const name = [ |
| 98 | + "codeblog-app", |
| 99 | + item.os === "win32" ? "windows" : item.os, |
| 100 | + item.arch, |
| 101 | + ].join("-") |
| 102 | + optionalDeps[name] = version |
| 103 | +} |
| 104 | + |
| 105 | +await Bun.file("dist/codeblog-app/package.json").write( |
| 106 | + JSON.stringify( |
| 107 | + { |
| 108 | + name: "codeblog-app", |
| 109 | + version, |
| 110 | + description: "CLI client for CodeBlog — the forum where AI writes the posts", |
| 111 | + license: "MIT", |
| 112 | + author: "CodeBlog-ai", |
| 113 | + homepage: "https://github.com/CodeBlog-ai/codeblog-app", |
| 114 | + repository: { type: "git", url: "https://github.com/CodeBlog-ai/codeblog-app" }, |
| 115 | + bin: { codeblog: "bin/codeblog" }, |
| 116 | + optionalDependencies: optionalDeps, |
| 117 | + }, |
| 118 | + null, |
| 119 | + 2, |
| 120 | + ), |
| 121 | +) |
| 122 | + |
| 123 | +// Copy the launcher script |
| 124 | +await $`mkdir -p dist/codeblog-app/bin` |
| 125 | +await Bun.file("dist/codeblog-app/bin/codeblog").write( |
| 126 | + await Bun.file(path.join(dir, "bin", "codeblog")).text(), |
| 127 | +) |
| 128 | +await $`chmod +x dist/codeblog-app/bin/codeblog` |
| 129 | + |
| 130 | +console.log("") |
| 131 | +console.log(`Built ${targets.length} platform binaries for v${version}`) |
| 132 | +console.log("") |
| 133 | + |
| 134 | +// If --publish flag, publish all packages |
| 135 | +if (process.argv.includes("--publish")) { |
| 136 | + for (const item of targets) { |
| 137 | + const name = [ |
| 138 | + "codeblog-app", |
| 139 | + item.os === "win32" ? "windows" : item.os, |
| 140 | + item.arch, |
| 141 | + ].join("-") |
| 142 | + console.log(`Publishing ${name}@${version}...`) |
| 143 | + await $`npm publish --access public`.cwd(`dist/${name}`) |
| 144 | + } |
| 145 | + console.log(`Publishing codeblog-app@${version}...`) |
| 146 | + await $`npm publish --access public`.cwd(`dist/codeblog-app`) |
| 147 | + console.log("All packages published!") |
| 148 | +} |
0 commit comments