Repository navigation
Expand file tree
/
Copy pathruntime.cjs
More file actions
77 lines (62 loc) · 1.59 KB
/
Copy pathruntime.cjs
File metadata and controls
77 lines (62 loc) · 1.59 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
#!/usr/bin/env node
const { process, console } = globalThis;
const fs = require("fs");
function readEnv(name, options = {}) {
const { required = false, defaultValue = "" } = options;
const value = process.env[name];
if (typeof value === "undefined" || value === null || value === "") {
if (required) {
throw new Error(`Missing required environment variable: ${name}`);
}
return defaultValue;
}
return String(value);
}
function normalizeBoolean(value, defaultValue = false) {
if (typeof value === "boolean") {
return value;
}
if (value === undefined || value === null || value === "") {
return defaultValue;
}
const normalized = String(value).trim().toLowerCase();
if (["1", "true", "yes", "on"].includes(normalized)) {
return true;
}
if (["0", "false", "no", "off"].includes(normalized)) {
return false;
}
return defaultValue;
}
function writeGithubOutput(key, value, outputPath = process.env.GITHUB_OUTPUT) {
if (!outputPath) {
throw new Error("GITHUB_OUTPUT is not set");
}
fs.appendFileSync(outputPath, `${key}=${value}\n`, "utf8");
}
function log(level, message, metadata = null) {
const record = {
level,
message,
timestamp: new Date().toISOString(),
};
if (metadata && typeof metadata === "object") {
record.metadata = metadata;
}
console.log(JSON.stringify(record));
}
async function runMain(mainFn) {
try {
await mainFn();
} catch (error) {
console.error(error.message);
process.exit(1);
}
}
module.exports = {
readEnv,
normalizeBoolean,
writeGithubOutput,
log,
runMain,
};