-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
56 lines (47 loc) · 1.53 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { spawn } from 'child_process'
import { resolve } from 'path'
const { BOT_RUNNER_INSTANCES, DEBUG } = process.env
type BotInstance = {
name: string // Name to reference the bot in the runner logs
bot: string // Name of folder in node_modules to run the start command
command?: string // The command to run in the root of the bot folder. Default: `yarn run start`
env: { [key: string]: string } // Env vars to set for the instance
delay?: number // Seconds to delay the start command of the bot. Default: 0
}
const instances: BotInstance[] = JSON.parse(BOT_RUNNER_INSTANCES)
const processes = []
const defaultEnv = {}
if (DEBUG) {
defaultEnv['DEBUG'] = 'true'
}
const exec = (instance: BotInstance) => {
instance.command ??= 'yarn run start'
const [runCmd, ...runOpts] = instance.command.split(' ')
const p = spawn(runCmd, runOpts, {
cwd: resolve(__dirname, `../node_modules/${instance.bot}`),
env: { ...process.env, ...defaultEnv, ...instance.env },
stdio: 'inherit',
})
processes.push(p)
}
async function main() {
const run = async () => {
for (const instance of instances) {
const { delay, bot, name } = instance
const delayMsg = delay ? ` in ${delay}s` : ''
console.log(`Starting ${name} (${bot})${delayMsg}...`)
setTimeout(() => {
exec(instance)
}, (delay ?? 0) * 1000)
}
}
run()
process.on('SIGINT', () => {
console.log('Caught interrupt signal. Stopping...')
for (const p of processes) {
p.kill('SIGINT')
}
process.exit()
})
}
main()