Skip to content

Commit 7853ac7

Browse files
committed
improvement: rewrite to use native node packages
1 parent 72629ab commit 7853ac7

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

docker/inject.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { copySync, removeSync, readFileSync, writeFileSync } = require("fs-extra");
2-
const klaw = require("klaw");
1+
const { cpSync, rmSync, readFileSync, writeFileSync, readdirSync } = require("node:fs");
2+
const { join } = require("node:path");
33

44
const BUILD_DIR = "dist";
55
const OUT_DIR = "dist_injected";
@@ -15,35 +15,32 @@ const REPLACEMENTS = {
1515
__VITE_HCAPTCHA_SITEKEY__: process.env.VITE_HCAPTCHA_SITEKEY || "",
1616
};
1717

18-
(async () => {
19-
console.log("Preparing injected build...");
20-
21-
try {
22-
removeSync(OUT_DIR);
23-
} catch (_) {}
24-
25-
copySync(BUILD_DIR, OUT_DIR);
26-
27-
console.log("Injecting environment variables...");
28-
for await (const file of klaw(OUT_DIR)) {
29-
const path = file.path;
30-
if (path.endsWith(".js") || path.endsWith(".html")) {
31-
let data = readFileSync(path, "utf-8");
32-
let modified = false;
33-
34-
for (const [placeholder, value] of Object.entries(REPLACEMENTS)) {
35-
if (data.includes(placeholder)) {
36-
data = data.replaceAll(placeholder, value);
37-
modified = true;
38-
}
39-
}
40-
41-
if (modified) {
42-
console.log("Injected:", path);
43-
writeFileSync(path, data);
44-
}
18+
console.log("Preparing injected build...");
19+
20+
rmSync(OUT_DIR, { recursive: true, force: true });
21+
cpSync(BUILD_DIR, OUT_DIR, { recursive: true });
22+
23+
console.log("Injecting environment variables...");
24+
const files = readdirSync(OUT_DIR, { recursive: true });
25+
26+
for (const file of files) {
27+
const path = join(OUT_DIR, file);
28+
if (!path.endsWith(".js") && !path.endsWith(".html")) continue;
29+
30+
let data = readFileSync(path, "utf-8");
31+
let modified = false;
32+
33+
for (const [placeholder, value] of Object.entries(REPLACEMENTS)) {
34+
if (data.includes(placeholder)) {
35+
data = data.replaceAll(placeholder, value);
36+
modified = true;
4537
}
4638
}
4739

48-
console.log("Injection complete.");
49-
})();
40+
if (modified) {
41+
console.log("Injected:", path);
42+
writeFileSync(path, data);
43+
}
44+
}
45+
46+
console.log("Injection complete.");

docker/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
"start": "node inject.js && sirv dist_injected --port 5000 --cors --single --host"
77
},
88
"dependencies": {
9-
"fs-extra": "^11.3.0",
10-
"klaw": "^4.1.0",
119
"sirv-cli": "^3.0.1"
1210
},
1311
"engines": {

0 commit comments

Comments
 (0)