revenge-bundle-next/scripts/dev.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-03-21 19:59:16 +07:00
import watcher from '@parcel/watcher'
2025-04-15 14:22:41 +07:00
import { debounce } from '@revenge-mod/utils/callbacks'
2025-03-21 19:59:16 +07:00
import chalk from 'chalk'
2025-04-15 14:22:41 +07:00
import { join } from 'path'
2025-03-21 19:59:16 +07:00
import build from './build'
const cwdify = (path: string) => join(process.cwd(), path)
const Sources = ['src', 'lib', 'plugins'].map(cwdify)
const ExitTriggers = ['scripts', 'package.json', 'bun.lock'].map(cwdify)
const buildDevWithDebounceAndCatchErrors = debounce(async () => {
needRebuild = false
2025-03-21 19:59:16 +07:00
try {
return await build(true)
} catch (e) {
console.error(e)
}
}, 250)
let needRebuild = true
2025-03-21 19:59:16 +07:00
watcher.subscribe(process.cwd(), async (err, events) => {
if (err) return console.error(err)
if (events.some(it => ExitTriggers.some(se => it.path.startsWith(se)))) {
console.error(chalk.redBright('\u26A0 Scripts or packages has changed, exiting!'))
process.exit()
}
needRebuild ||= events.some(it => Sources.some(src => it.path.startsWith(src)))
2025-03-21 19:59:16 +07:00
})
const server = Bun.serve({
async fetch(req, srv) {
if (needRebuild) await buildDevWithDebounceAndCatchErrors()
2025-03-21 19:59:16 +07:00
console.debug(chalk.gray(`\u{1F79B} Receiving request from ${srv.requestIP(req)!.address}`))
return new Response(Bun.file('./dist/revenge.bundle'))
},
port: 4040,
})
console.info(chalk.cyanBright(`\u24D8 Serving on port ${server.port}`))