out-of-your-element/start.js
l1s5 023432cc11
start: add process-wide rejection handler with error logger
Note that this also means that unhandled rejections now longer crashes
the program, it just logs the error and continues as usual.
2026-10-26 21:13:35 +09:00

62 lines
1.6 KiB
JavaScript
Executable file

#!/usr/bin/env node
// @ts-check
// Name the process and the main thread
process.title = "ooye"
// Ensure consistent output from the Intl and Temporal API across Node.js versions
require("temporal-polyfill-lite/global")
const fs = require("fs")
const util = require("util")
const sqlite = require("better-sqlite3")
const migrate = require("./src/db/migrate")
const HeatSync = require("heatsync")
const {reg} = require("./src/matrix/read-registration")
const passthrough = require("./src/passthrough")
const db = new sqlite("ooye.db")
process.on("unhandledRejection", e => {
console.error(`Unhandled promise rejection:\n${util.inspect(e, false, 4, false)}`)
})
const sync = new HeatSync({
watchFunction(path, options, cb) {
return fs.watchFile(path, options, (curr, prev) => {
if (curr.mtimeMs !== prev.mtimeMs || curr.size !== prev.size) cb()
})
}
})
Object.assign(passthrough, {sync, db})
const orm = sync.require("./src/db/orm")
passthrough.from = orm.from
passthrough.select = orm.select
const DiscordClient = require("./src/d2m/discord-client")
const discord = new DiscordClient(reg.ooye.discord_token)
passthrough.discord = discord
const {as} = require("./src/matrix/appservice")
passthrough.as = as
const power = require("./src/matrix/power.js")
sync.require("./src/m2d/event-dispatcher")
;(async () => {
await migrate.migrate(db)
process.stdout.write("Connecting to Discord... ")
await discord.cloud.connect()
console.log("ok.")
sync.require("./src/web/server")
await power.applyPower()
discord.cloud.once("ready", () => {
as.listen()
})
require("./src/stdin")
})()