|
| 1 | +import { resolve, join } from "node:path" |
| 2 | + |
| 3 | +import { $ } from "bun" |
| 4 | + |
| 5 | +const ROOT_DIR = resolve("./") |
| 6 | +const BUILD_DIR = "dist" |
| 7 | + |
| 8 | +const getAbsolutePath = (path: string) => { |
| 9 | + const pwd = resolve(path) |
| 10 | + |
| 11 | + if (!pwd.includes(ROOT_DIR)) { |
| 12 | + // to protect you and me from big oopsies when running `rm -rf ${pwd}` |
| 13 | + console.error(`Cannot escape project directory. pwd: ${pwd}\n`) |
| 14 | + process.exit(1) |
| 15 | + } |
| 16 | + |
| 17 | + return pwd |
| 18 | +} |
| 19 | + |
| 20 | +const getLatestTag = async () => |
| 21 | + $`git describe --tags $(git rev-list --tags --max-count=1)` |
| 22 | + .text() |
| 23 | + .then(out => out.trim()) |
| 24 | + |
| 25 | +type ShellFn = (...args: Parameters<typeof $>) => Promise<string> |
| 26 | + |
| 27 | +const createWorktree = async (path: string, name: string) => { |
| 28 | + const pwd = getAbsolutePath(path) |
| 29 | + |
| 30 | + await $`git worktree add -f ${pwd} ${name}`.quiet() |
| 31 | + |
| 32 | + const shell: ShellFn = (...args: Parameters<ShellFn>) => |
| 33 | + $(...args) |
| 34 | + .cwd(pwd) |
| 35 | + .text() |
| 36 | + |
| 37 | + return { |
| 38 | + name, |
| 39 | + path, |
| 40 | + $: shell, |
| 41 | + mv: (source: string, target: string) => |
| 42 | + $`mv -f "${join(pwd, source)}" "${join(ROOT_DIR, target)}"`.quiet(), |
| 43 | + rm: () => $`rm -rf ${pwd}`.quiet(), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +const build = async ($: ShellFn, name: string) => { |
| 48 | + console.info(`🏗️ Build ${name}:`) |
| 49 | + await $`bun i --frozen-lockfile` |
| 50 | + console.info(` √ installed dependencies`) |
| 51 | + await $`bun run l10n:build` |
| 52 | + console.info(` √ extracted translations`) |
| 53 | + await $`vite build --outDir="./${BUILD_DIR}"` |
| 54 | + console.info(` √ bundled app`) |
| 55 | + console.info("") |
| 56 | +} |
| 57 | + |
| 58 | +const main = async () => { |
| 59 | + try { |
| 60 | + const tag = await getLatestTag() |
| 61 | + |
| 62 | + const prod = await createWorktree("./worktree-clocktopus-prod/", tag) |
| 63 | + const main = await createWorktree("./worktree-clocktopus-main/", "main") |
| 64 | + |
| 65 | + await build(main.$, main.name) |
| 66 | + await build(prod.$, prod.name) |
| 67 | + |
| 68 | + console.info("📦️ Creating final package...") |
| 69 | + await $`rm -rf ./dist`.quiet().catch() |
| 70 | + await prod.mv(BUILD_DIR, "./dist/") |
| 71 | + await main.mv(BUILD_DIR, "./dist/dev") |
| 72 | + |
| 73 | + console.info("🧼 Cleaning up...") |
| 74 | + await prod.rm() |
| 75 | + await main.rm() |
| 76 | + } catch (error) { |
| 77 | + console.error("⚠️ Error: Something went wrong\n") |
| 78 | + console.error(error) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +await main() |
0 commit comments