-
Notifications
You must be signed in to change notification settings - Fork 0
/
devServerInit.js
136 lines (86 loc) · 2.89 KB
/
devServerInit.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
const nodemon = require("nodemon")
const detectPort = require("detect-port")
const args = require("yargs").argv;
const path = require("path")
const fs = require("fs")
const open = require("open")
const waitOn = require("wait-on")
const del = require("del")
const chokidar = require("chokidar")
const mkdirp = require("mkdirp")
const {default: delay} = require("tiny-delay")
let imageWeb
try {
imageWeb = require("image-web")
}
catch(e) {
console.log("Unable to load image-web, skipping image compression. This is probably due to the fact that sharp not able to properly install.")
}
// configureable
const serverEntryFileName = "server.js"
const appEntryFileName = "lbAnmeldung.js"
let serverDir = "./server/dist";
let appDir = "./public/dist";
if (args.dev === "repl") {
serverDir = "./replServer/dist"
}
else if (args.dev === "server") {
serverDir = "./server/dist";
}
let serverEntryPath = path.join(serverDir, serverEntryFileName)
let appEntryPath = path.join(appDir, appEntryFileName);
(async (wantedPort = 6500) => {
await Promise.all([
del(appDir).then(() => console.log("Deleted \"" + appDir + "\".")),
del(serverDir).then(() => console.log("Deleted \"" + serverDir + "\"."))
])
console.log("")
console.log("")
console.log("Waiting for build to finish, before starting the server...")
await waitOn({
resources: [serverEntryPath, appEntryPath]
})
let gotPort;
try {
gotPort = await detectPort(wantedPort)
}
catch(e) {
console.error(e)
return
}
if (imageWeb) {
const compressImages = imageWeb.constrImageWeb(["jpg", "webp", "avif"], ["4K", "2K", "PREV"])
const imgDistPath = "public/res/img/dist"
const imgSrcPath = "app/res/img"
mkdirp.sync(imgSrcPath)
const imgChangeF = async (path, override) => {
console.log("Compressing images")
await delay(1000)
await compressImages(path, imgDistPath, { override, silent: false })
}
imgChangeF(imgSrcPath, false)
chokidar.watch(imgSrcPath, { ignoreInitial: true }).on("change", (path) => imgChangeF(path, true))
chokidar.watch(imgSrcPath, { ignoreInitial: true }).on("add", (path) => imgChangeF(path, false))
}
let server = nodemon({
watch: serverDir,
script: serverEntryPath,
env: {
port: gotPort,
DEV: true
}
})
server.on("restart", (e) => {
console.log("")
console.log("-----------------")
console.log("Server restarting")
console.log("-----------------")
console.log("")
})
console.log("")
console.log("")
if (gotPort !== wantedPort) console.log(`Port ${wantedPort} was occupied, falling back to: http://127.0.0.1:${gotPort}.\n----------------------------------------------\n`)
else console.log(`Serving on http://127.0.0.1:${gotPort}.\n---------------------\n`)
console.log("Starting Browser")
open(`http://127.0.0.1:${gotPort}`)
})(args.port)