1 | import { lstatSync, realpathSync } from "fs";
|
2 | import path from "path";
|
3 | import { fileURLToPath } from "url";
|
4 | import { setFlagsFromString } from "v8";
|
5 | import { runInNewContext } from "vm";
|
6 | import { newLogger } from "@compas/stdlib";
|
7 | import dotenv from "dotenv";
|
8 | import { refreshEnvironmentCache } from "./env.js";
|
9 | import { AppError } from "./error.js";
|
10 | import { isNil } from "./lodash.js";
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export function getSecondsSinceEpoch() {
|
24 | return Math.floor(Date.now() / 1000);
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export function noop() {}
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | let internalGc = global.gc;
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export function gc() {
|
50 | if (isNil(internalGc)) {
|
51 | setFlagsFromString("--expose_gc");
|
52 | internalGc = runInNewContext("gc");
|
53 | }
|
54 |
|
55 |
|
56 | internalGc();
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | export function mainFn(meta, cb) {
|
72 | const { isMainFn, name } = isMainFnAndReturnName(meta);
|
73 | if (!isMainFn) {
|
74 | return;
|
75 | }
|
76 |
|
77 |
|
78 | dotenv.config({ path: path.resolve(process.cwd(), ".env.local") });
|
79 | dotenv.config();
|
80 |
|
81 | refreshEnvironmentCache();
|
82 |
|
83 | const logger = newLogger({
|
84 | ctx: { type: name },
|
85 | });
|
86 |
|
87 | const unhandled = (error) => {
|
88 | logger.error(error);
|
89 | process.exit(1);
|
90 | };
|
91 |
|
92 | process.on("unhandledRejection", (reason, promise) =>
|
93 | unhandled({
|
94 | type: "unhandledRejection",
|
95 | reason: AppError.format(reason),
|
96 | promise,
|
97 | }),
|
98 | );
|
99 |
|
100 | process.on("uncaughtExceptionMonitor", (error, origin) =>
|
101 | logger.error({
|
102 | type: "uncaughtException",
|
103 | error: AppError.format(error),
|
104 | origin,
|
105 | }),
|
106 | );
|
107 |
|
108 | process.on("warning", (warn) =>
|
109 | logger.error({
|
110 | type: "warning",
|
111 | warning: AppError.format(warn),
|
112 | }),
|
113 | );
|
114 |
|
115 | Promise.resolve(cb(logger)).catch((e) => {
|
116 | unhandled({
|
117 | type: "error",
|
118 | message: "Error caught from callback passed in `mainFn`",
|
119 | error: AppError.format(e),
|
120 | });
|
121 | });
|
122 | }
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | export function filenameForModule(meta) {
|
133 | return fileURLToPath(meta.url);
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | export function dirnameForModule(meta) {
|
145 | return path.dirname(filenameForModule(meta));
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | export function isMainFnAndReturnName(meta) {
|
156 | const modulePath = fileURLToPath(meta.url);
|
157 |
|
158 | let scriptPath = process.argv[1];
|
159 |
|
160 |
|
161 | const scriptStat = lstatSync(scriptPath);
|
162 | if (scriptStat.isSymbolicLink()) {
|
163 | scriptPath = realpathSync(scriptPath);
|
164 | }
|
165 | const scriptPathExt = path.extname(scriptPath);
|
166 | if (scriptPathExt) {
|
167 | return {
|
168 | isMainFn: modulePath === scriptPath,
|
169 | name: scriptPath
|
170 | .substring(0, scriptPath.length - scriptPathExt.length)
|
171 | .split(path.sep)
|
172 | .pop(),
|
173 | };
|
174 | }
|
175 |
|
176 | let modulePathWithoutExt = modulePath;
|
177 | const modulePathExt = path.extname(modulePath);
|
178 | if (modulePathExt) {
|
179 | modulePathWithoutExt = modulePathWithoutExt.slice(0, -modulePathExt.length);
|
180 | }
|
181 |
|
182 | return {
|
183 | isMainFn: modulePathWithoutExt === scriptPath,
|
184 | name: modulePathWithoutExt.split(path.sep).pop(),
|
185 | };
|
186 | }
|