-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdev-server.mjs
More file actions
78 lines (69 loc) · 2 KB
/
dev-server.mjs
File metadata and controls
78 lines (69 loc) · 2 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// @ts-nocheck
import { context } from 'esbuild'
import { spawn } from 'child_process'
import { clientBuild, serverBuild } from './esbuild.config.mjs'
import { rm, access, constants } from 'node:fs'
const args = ['--enable-source-maps', '--inspect', 'dist/application.js']
let server
const startServer = async () => {
if (server) server.kill()
console.log(`💻\x1b[32m node\x1b[0m\x1b[33m ${args.join(' ')}\x1b[0m`)
server = spawn('node', args, {
stdio: 'inherit',
})
}
async function startDevServer() {
process.env.PROJECT_DIR = process.cwd()
const clientCtx = await context(clientBuild)
const serverCtx = await context({
...serverBuild,
plugins: [
...serverBuild.plugins,
{
name: 'server-rebuild',
setup(build) {
build.onEnd(async (result) => {
if (result.errors.length === 0) {
await startServer()
}
})
},
},
],
})
await Promise.all([clientCtx.watch(), serverCtx.watch()])
const cleanup = async () => {
server?.kill()
await Promise.all([clientCtx.dispose(), serverCtx.dispose()]).then(() => {
console.log('\n🔽 dev server down')
})
process.exit()
}
process.on('SIGINT', cleanup)
process.on('SIGTERM', cleanup)
}
rm('dist', { recursive: true, force: true }, async () => {
console.log('✅ [dist] cleared')
if (process.env.NODE_ENV === 'test') {
console.log('🧪 [cypress/test.env] loaded environment')
args.unshift('-r', 'dotenv/config')
args.push('dotenv_config_path=test/cypress/test.env')
} else {
access('/.dockerenv', constants.R_OK, (err) => {
if (err) {
console.log('🔩 [.env] loaded environment')
args.unshift('-r', 'dotenv/config')
} else {
console.log('🐳 docker environment')
}
})
}
startDevServer()
.then(() => {
console.log('🔼 dev server going up')
})
.catch((err) => {
console.error('💥 dev server failed to start', err)
process.exit(1)
})
})