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-04-15 14:50:14 +07:00
|
|
|
import os from 'os'
|
2025-03-21 19:59:16 +07:00
|
|
|
import build from './build'
|
2025-04-15 14:50:14 +07:00
|
|
|
import pkg from '../package.json'
|
|
|
|
|
|
|
|
|
|
const lanHost = process.argv.includes('--lan')
|
|
|
|
|
|
|
|
|
|
console.info(chalk.redBright(`\nRevenge ${chalk.white(`v${pkg.version}`)}\n`))
|
2025-03-21 19:59:16 +07:00
|
|
|
|
|
|
|
|
const cwdify = (path: string) => join(process.cwd(), path)
|
|
|
|
|
const Sources = ['src', 'lib', 'plugins'].map(cwdify)
|
2025-05-02 22:05:43 +07:00
|
|
|
const ExitTriggers = ['scripts', 'shims', 'package.json', 'bun.lock', 'tsconfig.json'].map(cwdify)
|
2025-03-21 19:59:16 +07:00
|
|
|
|
2025-04-15 14:50:14 +07:00
|
|
|
const debouncedBuildDev = debounce(() => ((needRebuild = false), build(true).catch(console.error)), 250)
|
2025-03-21 19:59:16 +07:00
|
|
|
|
2025-03-29 23:30:25 +07:00
|
|
|
let needRebuild = true
|
|
|
|
|
|
2025-04-15 14:50:14 +07:00
|
|
|
watcher.subscribe(process.cwd(), (err, events) => {
|
2025-03-21 19:59:16 +07:00
|
|
|
if (err) return console.error(err)
|
|
|
|
|
|
|
|
|
|
if (events.some(it => ExitTriggers.some(se => it.path.startsWith(se)))) {
|
2025-05-02 22:05:43 +07:00
|
|
|
console.error(chalk.redBright('\u26A0 Scripts, packages, shims, or tsconfig.json has changed, exiting!'))
|
2025-03-21 19:59:16 +07:00
|
|
|
process.exit()
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-29 23:30:25 +07:00
|
|
|
needRebuild ||= events.some(it => Sources.some(src => it.path.startsWith(src)))
|
2025-03-21 19:59:16 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const server = Bun.serve({
|
2025-04-15 14:50:14 +07:00
|
|
|
hostname: lanHost ? '0.0.0.0' : '127.0.0.1',
|
2025-03-29 23:30:25 +07:00
|
|
|
async fetch(req, srv) {
|
2025-04-15 14:50:14 +07:00
|
|
|
if (needRebuild) await debouncedBuildDev()
|
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,
|
|
|
|
|
})
|
|
|
|
|
|
2025-04-15 14:50:14 +07:00
|
|
|
if (lanHost) console.info(chalk.gray('\u24D8 Listening on all interfaces...'))
|
|
|
|
|
else console.info(chalk.gray('\u24D8 Listening locally... Use --lan to listen on all interfaces'))
|
|
|
|
|
|
2025-03-21 19:59:16 +07:00
|
|
|
console.info(chalk.cyanBright(`\u24D8 Serving on port ${server.port}`))
|
2025-04-15 14:50:14 +07:00
|
|
|
console.info(chalk.gray('\u24D8 Accessible on:'))
|
|
|
|
|
|
|
|
|
|
for (const int of Object.values(os.networkInterfaces()))
|
|
|
|
|
if (int)
|
|
|
|
|
for (const det of int) {
|
|
|
|
|
if (det.family !== 'IPv4' || (!det.internal && !lanHost)) continue
|
|
|
|
|
console.info(chalk.gray(`- http://${det.address}:${server.port}`))
|
|
|
|
|
}
|