@lodestar/prover
Version:
A Typescript implementation of the Ethereum Consensus light client
66 lines • 2.12 kB
JavaScript
import { execSync } from "node:child_process";
// This file is created in the build step and is distributed through NPM
// MUST be in sync with `-/gitDataPath.ts` and `package.json` files.
import { readGitDataFile } from "./gitDataPath.js";
/** Reads git data from a persisted file or local git data at build time. */
export function readAndGetGitData() {
try {
// Gets git data containing current branch and commit info from persistent file.
let persistedGitData;
try {
persistedGitData = readGitDataFile();
}
catch (_e) {
persistedGitData = {};
}
const currentGitData = getGitData();
return {
// If the CLI is run from source, prioritze current git data
// over `.git-data.json` file, which might be stale here.
branch: currentGitData.branch && currentGitData.branch.length > 0
? currentGitData.branch
: (persistedGitData.branch ?? ""),
commit: currentGitData.commit && currentGitData.commit.length > 0
? currentGitData.commit
: (persistedGitData.commit ?? ""),
};
}
catch (_e) {
return {
branch: "",
commit: "",
};
}
}
/** Gets git data containing current branch and commit info from CLI. */
export function getGitData() {
return {
branch: process.env.GIT_BRANCH ?? getBranch(),
commit: process.env.GIT_COMMIT ?? getCommit(),
};
}
/** Tries to get branch from git CLI. */
function getBranch() {
try {
return shellSilent("git rev-parse --abbrev-ref HEAD");
}
catch (_e) {
return "";
}
}
/** Tries to get commit from git from git CLI. */
function getCommit() {
try {
return shellSilent("git rev-parse --verify HEAD");
}
catch (_e) {
return "";
}
}
/** Silent shell that won't pollute stdout, or stderr */
function shellSilent(cmd) {
return execSync(cmd, { stdio: ["ignore", "pipe", "ignore"] })
.toString()
.trim();
}
//# sourceMappingURL=index.js.map