Skip to content

Commit

Permalink
chore: add dev & comp scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
renejfc committed Mar 25, 2024
1 parent 64d2c0b commit 23342e3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ out
# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
},
"scripts": {
"start": "NODE_ENV=production bun ./src/index.tsx",
"dev": "bun ./src/index.tsx",
"build": "bun build --target=bun ./src/index.ts --outdir ./build",
"dev": "bun ./scripts/dev.ts",
"comp": "bun ./scripts/build.ts",
"format": "biome format . --changed",
"format:fix": "biome format . --write --changed",
"typecheck": "tsc",
Expand All @@ -39,6 +39,6 @@
},
"type": "module",
"bin": {
"conmmit": "./src/index.tsx"
"conmmit": "./dist/conmmit.js"
}
}
65 changes: 65 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { $, file } from "bun"
import c from "picocolors"

const ENTRYPOINT = "./src/index.tsx"
const OUTDIR = "./dist"
const COMP_OUTFILE = `${OUTDIR}/conmmit`
const BUILD_OUTFILE = `${OUTDIR}/conmmit.js`
const SHOULD_COMPILE = false // not sure if shipping binaries tbh...

const build = await $`bun build --target=bun --minify ${ENTRYPOINT} --outfile ${BUILD_OUTFILE}`.quiet()

if (SHOULD_COMPILE) {
const comp = await $`bun build --compile --minify --sourcemap ${ENTRYPOINT} --outfile ${COMP_OUTFILE}`.quiet()
if (comp.exitCode === 0) {
const stdout = formatStdout(comp.stdout)
const size = fileSize(COMP_OUTFILE, "mb")

console.log(`
${c.bold("--- Compilation ---")}
${stdout}
File size: ${size} MB
`)
} else {
console.error(`
Compilation error:
${comp.stderr.toString()}
`)
}
}

if (build.exitCode === 0) {
const stdout = c.dim(build.stdout.toString().trim())
const size = fileSize(BUILD_OUTFILE, "kb")

console.log(`
${c.bold("--- Build ---")}
${stdout}
File size: ${size} KB
`)
} else {
console.error(`
Build error:
${build.stderr.toString()}
`)
}

function bytesTo(bytes: number, to: "mb" | "kb") {
const base = 1024
if (to === "mb") return (bytes / base ** 2).toFixed(2)
return (bytes / base).toFixed(2)
}

function formatStdout(stdout: Buffer) {
return c.italic(c.dim(stdout.toString().trim()))
}

function fileSize(path: string, unit: "mb" | "kb") {
return bytesTo(file(path).size, unit)
}
22 changes: 22 additions & 0 deletions scripts/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { watch } from "node:fs/promises"
import path from "node:path"
import { $ } from "bun"

const WATCH_DIR = path.join(__dirname, "../src")

const watcher = watch(WATCH_DIR, { recursive: true })
console.log(`Watching ${WATCH_DIR}`)

for await (const event of watcher) {
console.log(`Detected ${event.eventType} in ${event.filename}`)

const compp = await $`bun comp`.quiet()

if (compp.exitCode === 0)
console.log("Compiled")
}

process.on("SIGINT", () => {
console.log("Closing watcher...")
process.exit(0)
})

0 comments on commit 23342e3

Please sign in to comment.