Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
80 lines (66 loc) · 2.32 KB
/
Copy pathmain.js
File metadata and controls
80 lines (66 loc) · 2.32 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
79
80
//#region Imports
const {PreProcessor} = require("./functions/preprocessResource")
const {isLeapDependency, hasAnyFileBeenModified, relativeToAbs, writeFile, rmSync} = require("./functions/utils")
//#endregion
//#region Build Task
let leapBuildTask = {
shouldBuild(res) {
return isLeapDependency(res) && hasAnyFileBeenModified(res)
},
build(res, cb) {
// We need to use this trick as the build function cant be directly async
let buildleap = async () => {
try {
const preprocessor = new PreProcessor(res)
try {
await preprocessor.run()
await preprocessor.setPathsAsBuildRelative()
await preprocessor.writeCache()
} catch (e) {
cb(false, e.message)
return
}
cb(true)
} catch (e) {
cb(false, e.message)
}
}
buildleap().then()
}
}
// Register the build task after the server loaded (setImmediate runs immediately after the first tick in the main thread, that's started after the server loaded)
setImmediate(() => {
RegisterResourceBuildTaskFactory("leap", () => leapBuildTask)
})
//#endregion
//#region Command
RegisterCommand("leap", async (source, args) => {
if (source != 0) return // only server side can use the command
let [type, resourceName] = args
// No type or resource provided, display help
if (!type || !resourceName) {
console.log(`leap restart <resource>`)
console.log(`leap build <resource>`)
return
}
let preprocessor = new PreProcessor(resourceName)
switch(type) {
case "build": {
const _path = relativeToAbs("build/", resourceName)
rmSync(_path, {recursive: true, force: true})
await preprocessor.run(true)
await preprocessor.setPathsAsBuildRelative()
console.log(`^2Builded ${resourceName} successfully^0`)
break;
}
case "restart": {
StopResource(resourceName)
await preprocessor.run()
await preprocessor.setPathsAsBuildRelative()
await preprocessor.writeCache()
StartResource(resourceName)
break;
}
}
}, true)
//#endregion