-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
85 lines (73 loc) · 2.49 KB
/
config.ts
File metadata and controls
85 lines (73 loc) · 2.49 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
import { readFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { spawnSync } from "node:child_process";
export const DATA_DIR = process.env.DATA_DIR ?? "./data";
export const DB_PATH = process.env.DB_PATH ?? join(DATA_DIR, "garden.db");
export const PORT = Number.parseInt(process.env.PORT ?? "3000", 10);
export const BUILD_INFO = getBuildInfo();
export { getBuildInfo };
function getBuildInfo() {
const envSha = process.env.GIT_COMMIT_SHA?.trim();
const envMessage = process.env.GIT_COMMIT_MESSAGE?.trim();
const sha =
(envSha && envSha.length > 0 ? envSha : null) ??
readGitFromFiles().sha ??
readGit("rev-parse", "HEAD");
const message =
(envMessage && envMessage.length > 0 ? envMessage : null) ??
readGitFromFiles().message ??
readGit("log", "-1", "--pretty=%s");
if (!sha && !message) return null;
return {
sha: sha?.trim() || "unknown",
message: message?.trim() || "",
};
}
function readGit(...args: string[]) {
try {
const result = spawnSync("git", args, { encoding: "utf8" });
if (result.status !== 0) return null;
return result.stdout.trim() || null;
} catch {
return null;
}
}
function readGitFromFiles() {
try {
const headPath = join(process.cwd(), ".git", "HEAD");
if (!existsSync(headPath)) return { sha: null, message: null };
const head = readFileSync(headPath, "utf8").trim();
let sha = "";
if (head.startsWith("ref:")) {
const ref = head.replace("ref:", "").trim();
sha = readPackedRef(ref) ?? "";
} else {
sha = head;
}
const logPath = join(process.cwd(), ".git", "logs", "HEAD");
let message = "";
if (existsSync(logPath)) {
const log = readFileSync(logPath, "utf8").trim().split("\n").pop() ?? "";
const parts = log.split("\t");
message = parts[1] ? parts[1].trim() : "";
}
return { sha: sha || null, message: message || null };
} catch {
return { sha: null, message: null };
}
}
function readPackedRef(ref: string) {
const refPath = join(process.cwd(), ".git", ref);
if (existsSync(refPath)) {
return readFileSync(refPath, "utf8").trim();
}
const packedPath = join(process.cwd(), ".git", "packed-refs");
if (!existsSync(packedPath)) return null;
const lines = readFileSync(packedPath, "utf8").split("\n");
for (const line of lines) {
if (!line || line.startsWith("#") || line.startsWith("^")) continue;
const [hash, name] = line.split(" ");
if (name === ref) return hash;
}
return null;
}