-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
56 lines (41 loc) · 1.15 KB
/
index.ts
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
/* eslint-disable no-console */
import path from "path";
const NODE_ENV = process.env.NODE_ENV;
const __dirname = path.dirname(new URL(import.meta.url).pathname);
const start = async () => {
try {
if (isProduction()) return await runProduction();
runDev();
} catch (error) {
console.error("something bad happened during the initialization");
console.error(error);
process.exit(1);
}
};
const isProduction = () =>
["production", "production_local"].some((i) => i === NODE_ENV);
const runProduction = async () => {
const appPath = path.join(
__dirname,
"build",
getBuildFolderName(),
"app.mjs"
);
const app = await import(appPath);
app.runner();
};
const getBuildFolderName = () => {
const { NODE_VERSION = getNodeVersion() } = process.env;
if (!NODE_VERSION) throw "Node version not supported!";
return `node${NODE_VERSION.slice(0, 2)}`;
};
const getNodeVersion = () => {
return (
process.env.npm_config_user_agent
?.split("node/v")[1]
?.split(" ")[0]
?.slice(0, 2) || "18"
);
};
const runDev = async () => (await import(path.join(__dirname, "src"))).runner();
await start();