-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.config.ts
More file actions
executable file
·122 lines (107 loc) · 2.96 KB
/
build.config.ts
File metadata and controls
executable file
·122 lines (107 loc) · 2.96 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
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
import { build, createServer, InlineConfig } from "vite";
import { rimrafSync } from "rimraf";
import { viteSingleFile } from "vite-plugin-singlefile";
import { parseArgs } from "util";
import chokidar from "chokidar";
import { zip } from "zip-a-folder";
import fs from "fs";
import path from "path";
const REQUIREMENTS_PATH = "requirements.txt";
const patchManifestWithPython = () => {
const manifestPath = path.resolve("metadata", "manifest.json");
const requirementsPath = path.resolve(REQUIREMENTS_PATH);
const pythonPackages = fs.readFileSync(requirementsPath)
.toString()
.split("\n")
.filter((pkg) => pkg.length > 0);
const manifestContent = fs.readFileSync(manifestPath, "utf-8");
const manifest = JSON.parse(manifestContent);
manifest.python = manifest.python ?? {};
manifest.python.packages = pythonPackages;
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), "utf-8");
}
const {
values: { dev: isDev }
} = parseArgs({
args: process.argv,
options: {
dev: {
type: "boolean"
},
build: {
type: "boolean"
}
},
allowPositionals: true
});
const panelConfig: InlineConfig = {
root: "./src/panel",
publicDir: "../../metadata",
build: {
outDir: "../../dist",
emptyOutDir: false
},
plugins: [
viteSingleFile(),
{
name: "configure-response-headers",
configureServer: (server) => {
server.middlewares.use((_req, res, next) => {
res.setHeader("Access-Control-Request-Private-Network", "true");
res.setHeader("Access-Control-Allow-Private-Network", "true");
res.setHeader("Access-Control-Expose-Headers", "ETag");
next();
});
}
}
],
server: {
port: 8888,
cors: {
origin: [
"https://qatium.app",
"http://localhost:8888",
"http://localhost:3000",
"null"
],
credentials: true,
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
optionsSuccessStatus: 204
}
}
};
const pluginDirectory = "src/plugin";
const watchPython = (changed: (path: string) => void) => {
const watcher = chokidar.watch([pluginDirectory, REQUIREMENTS_PATH], {
persistent: true
});
watcher.on("all", (_, path) => {
changed(path);
});
}
const pythonZip = "metadata/plugin.zip";
const buildPython = async (path?: string) => {
console.log("Bundling python code...")
if (path?.includes(REQUIREMENTS_PATH)) {
return patchManifestWithPython();
}
if (fs.existsSync(pythonZip)) {
fs.unlinkSync(pythonZip)
}
await zip(pluginDirectory, pythonZip);
}
(async () => {
rimrafSync("./dist");
await new Promise((resolve) => setTimeout(() => resolve(true), 1000));
if (isDev) {
await buildPython();
watchPython(buildPython);
const server = await createServer(panelConfig);
await server.listen();
server.printUrls();
server.bindCLIShortcuts({ print: true });
} else {
await buildPython(REQUIREMENTS_PATH);
await build(panelConfig);
}
})();