-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·39 lines (34 loc) · 1.09 KB
/
server.js
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
#!/usr/bin/env node
import { argv, env, exit } from 'node:process'
import yargs from 'yargs'
import { createApp, createLogger, getConfig } from './index.js'
const args = await yargs(argv.slice(2))
.describe('production', 'Run in production mode')
.describe('test', 'Run in test mode')
.boolean(['production', 'test'])
.conflicts('production', 'test')
.middleware((argv) => {
const NODE_ENV = ['production', 'test'].find((k) => argv[k])
argv.NODE_ENV = NODE_ENV ?? env.NODE_ENV
})
.check(({ NODE_ENV }) => {
if (NODE_ENV == null) return true
if (env.NODE_ENV == null) return true
if (NODE_ENV !== env.NODE_ENV) {
throw new Error(`Cannot use --${NODE_ENV} with NODE_ENV=${env.NODE_ENV}`)
}
return true
})
.strict().argv
try {
const { NODE_ENV } = args
const config = await getConfig({ ...env, NODE_ENV })
const logger = createLogger(config)
const app = createApp(logger, config)
await app.start()
logger.info(`Server: ${app.baseUrl}`)
} catch (err) {
const logger = createLogger()
logger.fatal({ err }, 'Server: failed to start')
exit(1)
}