-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack_builder.js
239 lines (204 loc) · 7.65 KB
/
webpack_builder.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const fs = require("fs")
const path = require("path")
const workerFarm = require("worker-farm")
const exec = require("child-process-promise").exec
const async = require("async")
let buildingInProgress = false
let currentBuildingModule = ""
// some modules will not like the custom stack trace logic
const ops = Error.prepareStackTrace
Error.prepareStackTrace = undefined
const webpackBuildTask = {
shouldBuild(resourceName) {
const numMetaData = GetNumResourceMetadata(resourceName, "packm_config")
if (numMetaData > 0) {
for (let i = 0; i < numMetaData; i++) {
const configName = GetResourceMetadata(
resourceName,
"packm_config"
)
if (shouldBuild(configName)) {
return true
}
}
}
return false
function loadCache(config) {
// const configPath = GetResourcePath(resourceName) + "/" + config
// const packm = require(configPath)
// if (!packm.cache) return null
const cachePath = `cache/${resourceName}/${config.replace(
/\//g,
"_"
)}.json`
try {
return JSON.parse(
fs.readFileSync(cachePath, { encoding: "utf8" })
)
} catch {
return null
}
}
function shouldBuild(config) {
const cache = loadCache(config)
if (!cache) {
return true
}
for (const file of cache) {
const stats = getStat(file.name)
if (
!stats ||
stats.mtime !== file.stats.mtime ||
stats.size !== file.stats.size ||
stats.inode !== file.stats.inode
) {
return true
}
}
return false
}
function getStat(path) {
try {
const stat = fs.statSync(path)
return stat
? {
mtime: stat.mtimeMs,
size: stat.size,
inode: stat.ino,
}
: null
} catch {
return null
}
}
},
build(resourceName, cb) {
let buildWebpack = async () => {
let error = null
const configs = []
const promises = []
const numMetaData = GetNumResourceMetadata(
resourceName,
"packm_config"
)
for (let i = 0; i < numMetaData; i++) {
configs.push(
GetResourceMetadata(resourceName, "packm_config", i)
)
}
for (const configName of configs) {
const configPath =
GetResourcePath(resourceName) + "/" + configName
const cachePath = `cache/${resourceName}/${configName.replace(
/\//g,
"_"
)}.json`
try {
fs.mkdirSync(path.dirname(cachePath))
} catch {}
const config = require(configPath)
const workers = workerFarm(require.resolve("./webpack_runner"))
if (config) {
const resourcePath = path.resolve(
GetResourcePath(resourceName)
)
while (buildingInProgress) {
console.log(
`webpack is busy: we are waiting to compile ${resourceName} (${configName})`
)
await sleep(3000)
}
console.log(
`${resourceName}: started building ${configName}`
)
buildingInProgress = true
currentBuildingModule = resourceName
promises.push(
new Promise((resolve, reject) => {
workers(
{
configPath,
resourcePath,
cachePath,
},
(err, outp) => {
workerFarm.end(workers)
if (err) {
console.error(err.stack || err)
if (err.details) {
console.error(err.details)
}
buildingInProgress = false
currentBuildingModule = ""
currentBuildingScript = ""
reject(
"worker farm webpack errored out"
)
return
}
if (outp.errors) {
for (const error of outp.errors) {
console.log(error)
}
buildingInProgress = false
currentBuildingModule = ""
currentBuildingScript = ""
reject("webpack got an error")
return
}
console.log(
`${resourceName}: built ${configName}`
)
buildingInProgress = false
resolve()
}
)
})
)
}
}
try {
await Promise.all(promises)
} catch (e) {
error = e.toString()
}
buildingInProgress = false
currentBuildingModule = ""
if (error) {
cb(false, error)
} else cb(true)
}
buildWebpack().then()
},
}
RegisterCommand(
"packm",
async (source, args) => {
let cfgfile = GetResourceMetadata(args[0], "packm_config", 0)
if (!cfgfile) {
console.log(args[0] + " not using packM")
return
}
webpackBuildTask.build(args[0], (err) => {
if (source !== 0) return
if (!err) console.log("build " + args[0] + " failed.")
console.log("build " + args[0] + " success.", source)
let packm = require(path.join(GetResourcePath(args[0]), cfgfile))
exec(
`${path.join(
GetResourcePath(GetCurrentResourceName()),
`/bin/icecon_${process.platform}_amd64`
)} -c "restart ${args[0]}" ${packm.rcon.addr} ${
packm.rcon.password
}`
).then((result) => {
// console.log("\n", result.stdout)
})
})
},
true
)
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
RegisterResourceBuildTaskFactory("p_webpack", () => webpackBuildTask)