|
| 1 | +import { $ } from "bun" |
| 2 | + |
| 3 | +const getLatestTag = async () => { |
| 4 | + const out = |
| 5 | + await $`git describe --tags $(git rev-list --tags --max-count=1)`.quiet() |
| 6 | + return out.text().trim() |
| 7 | +} |
| 8 | + |
| 9 | +const getCurrentBranch = async () => { |
| 10 | + const out = await $`git rev-parse --abbrev-ref HEAD`.quiet() |
| 11 | + return out.text().trim() |
| 12 | +} |
| 13 | + |
| 14 | +const pushStash = async () => { |
| 15 | + const id = crypto.randomUUID() |
| 16 | + const out = await $`git stash push -u -m ${id}`.quiet() |
| 17 | + const noStash = out.text().includes("No local changes") |
| 18 | + |
| 19 | + return async () => { |
| 20 | + if (noStash) return |
| 21 | + await $`git stash apply stash^{/${id}}`.quiet() |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +const build = async (name, outDir) => { |
| 26 | + console.info(`Build ${name}:`) |
| 27 | + await $`bun run l10n:build`.quiet() |
| 28 | + console.info(`√ extracted translations`) |
| 29 | + await $`vite build --outDir=${outDir}`.quiet() |
| 30 | + console.info(`√ bundled app`) |
| 31 | + console.info("") |
| 32 | +} |
| 33 | + |
| 34 | +const main = async () => { |
| 35 | + const initialBranch = await getCurrentBranch() |
| 36 | + console.info("💾 Saving changes...\n") |
| 37 | + const popInitalState = await pushStash() |
| 38 | + |
| 39 | + try { |
| 40 | + const tag = await getLatestTag() |
| 41 | + const devDir = "tmp-dist/main" |
| 42 | + const prodDir = `tmp-dist/${tag}` |
| 43 | + |
| 44 | + await build("dev", devDir) |
| 45 | + |
| 46 | + const popMainDist = await pushStash() |
| 47 | + await $`git checkout tags/${tag}`.quiet() |
| 48 | + await popMainDist() |
| 49 | + |
| 50 | + await build("prod", prodDir) |
| 51 | + |
| 52 | + console.info("📦️ Build final package...\n") |
| 53 | + await $`rm -rf dist`.quiet() |
| 54 | + await $`mv ${prodDir} dist`.quiet() |
| 55 | + await $`mv ${devDir} dist/dev`.quiet() |
| 56 | + } catch (error) { |
| 57 | + console.error("⚠️ Error: Somthing went wrong") |
| 58 | + console.error(error) |
| 59 | + } finally { |
| 60 | + console.info("🔄 Restoring initial state...") |
| 61 | + await $`git checkout ${initialBranch}`.quiet() |
| 62 | + await popInitalState() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void main() |
0 commit comments