-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathgulpfile.js
132 lines (101 loc) · 3.17 KB
/
gulpfile.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const gulp = require('gulp')
const log = require('fancy-log')
const path = require('path')
const util = require('util')
const { exec } = require('child_process')
const asyncExec = util.promisify(exec)
const DEFAULT_NETWORK_INTERFACE = 'eth0'
async function ExecCommand(command, cmdCwd = null, cmdEnv = null) {
const fixedEnv = process.env
if (cmdEnv != null) {
Object.assign(fixedEnv, cmdEnv)
}
try {
await asyncExec(command, { cwd: cmdCwd, env: fixedEnv })
return { success: true }
} catch (error) {
return { success: false, error }
}
}
async function BuildComponent(componentName) {
const targetPath = path.resolve(`./${componentName}`)
const instRes = await ExecCommand('yarn', targetPath)
if (instRes.success === false) {
log(`Failed to get ${componentName}'s dependencies. ${instRes.error}`)
return false
}
log(`Installed ${componentName}'s dependencies successfully`)
const buildRes = await ExecCommand('npx gulp build', targetPath)
if (buildRes.success === false) {
log(`Failed to build ${componentName}. ${buildRes.error}`)
return false
}
log(`Built ${componentName} successfully`)
return true
}
function StartComponent(componentName, args, cmdEnv) {
const targetPath = path.resolve(`./${componentName}`)
const fixedEnv = process.env
if (cmdEnv != null) {
Object.assign(fixedEnv, cmdEnv)
}
const ls = exec(`node ${args}`, { cwd: targetPath, env: fixedEnv })
ls.stdout.on('data', (data) => {
log(`${componentName}: ${data}`)
})
ls.stderr.on('data', (data) => {
log.error(`${componentName}: ${data}`)
})
ls.on('close', (code) => {
log(`${componentName} exited with code ${code}`)
})
log(`Started ${componentName} successfully`)
}
gulp.task('build', async () => {
const components = ['master-server', 'users-service', 'website']
for (const c of components) {
if ((await BuildComponent(c)) === false) {
return false
}
}
return true
})
function FindNetworkInterface() {
if (process.argv.length >= 5) {
const key = process.argv[3]
const value = process.argv[4]
if (key === '--intf') {
return value
}
}
// fallback to the default interface
return DEFAULT_NETWORK_INTERFACE
}
gulp.task('start', (cb) => {
log("Starting game server's components...")
const targetIntf = FindNetworkInterface()
log(`Using "${targetIntf}" as the game server's net interface`)
StartComponent('users-service', 'dist/service.js', {
NODE_ENV: 'development',
USERS_PORT: 30100,
DB_HOST: 'localhost',
DB_PORT: 5432,
DB_NAME: 'cso2'
})
StartComponent(
'master-server',
`dist/server.js --interface "${targetIntf}"`,
{
NODE_ENV: 'development',
USERSERVICE_HOST: 'localhost',
USERSERVICE_PORT: 30100
}
)
StartComponent('website', 'dist/app.js', {
NODE_ENV: 'development',
WEBSITE_PORT: 8081,
USERSERVICE_HOST: 'localhost',
USERSERVICE_PORT: 30100
})
cb()
})