@delta-base/observability
Version:
Observability framework for delta-base applications
169 lines (165 loc) • 6.63 kB
JavaScript
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
// src/scripts/collect-build-info.ts
import { execSync } from "child_process";
import { existsSync, writeFileSync } from "fs";
import { resolve } from "path";
function safeExec(command, fallback = "") {
try {
return execSync(command, { encoding: "utf8", stdio: "pipe" }).trim();
} catch (error) {
console.warn(
`Warning: Failed to execute "${command}": ${error instanceof Error ? error.message : String(error)}`
);
return fallback;
}
}
function collectBuildInfo(options = {}) {
const {
packageJsonDir = process.cwd(),
outputDir = "src",
outputFileName = "build-info.ts",
defaultServiceName = "unknown-service",
includeGitInfo = true,
includeCiInfo = true,
customFields = {}
} = options;
console.log("Collecting build information...");
const packageJsonPath = resolve(packageJsonDir, "package.json");
let packageJson = {};
if (existsSync(packageJsonPath)) {
packageJson = __require(packageJsonPath);
} else {
console.warn(`Warning: package.json not found at ${packageJsonPath}`);
}
const buildInfo = {
"service.name": packageJson.name || defaultServiceName,
"service.version": packageJson.version || "0.0.0",
"build.timestamp": (/* @__PURE__ */ new Date()).toISOString(),
...customFields
};
const environment = process.env.NODE_ENV || process.env.CLOUDFLARE_ENVIRONMENT || process.env.ENVIRONMENT || "development";
buildInfo["service.environment"] = environment;
if (includeGitInfo) {
const gitHash = safeExec("git rev-parse HEAD", "unknown");
const gitShortHash = gitHash.substring(0, 7);
const gitBranch = safeExec("git rev-parse --abbrev-ref HEAD", "unknown");
const gitMessage = safeExec("git log -1 --pretty=%s", "unknown");
const gitAuthor = safeExec("git log -1 --pretty=%an", "unknown");
const gitTimestamp = safeExec(
"git log -1 --pretty=%ai",
(/* @__PURE__ */ new Date()).toISOString()
);
const gitStatus = safeExec("git status --porcelain", "");
Object.assign(buildInfo, {
"git.commit_hash": gitHash,
"git.commit_hash_short": gitShortHash,
"git.branch": gitBranch,
"git.commit_message": gitMessage,
"git.commit_author": gitAuthor,
"git.commit_timestamp": gitTimestamp,
"git.is_dirty": gitStatus.length > 0
});
}
if (includeCiInfo) {
const ciUser = process.env.GITHUB_ACTOR || process.env.GITLAB_USER_LOGIN || process.env.USER || process.env.USERNAME || "unknown";
const deploymentTrigger = process.env.GITHUB_EVENT_NAME || process.env.CI_PIPELINE_SOURCE || "manual";
Object.assign(buildInfo, {
"build.id": process.env.GITHUB_RUN_ID || Date.now().toString(),
"deployment.user": ciUser,
"deployment.trigger": deploymentTrigger,
"deployment.ci_build_id": process.env.GITHUB_RUN_ID || process.env.CI_JOB_ID,
"deployment.ci_build_url": process.env.GITHUB_SERVER_URL && process.env.GITHUB_REPOSITORY && process.env.GITHUB_RUN_ID ? `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}` : void 0
});
const repoUrl = process.env.GITHUB_SERVER_URL && process.env.GITHUB_REPOSITORY ? `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}` : void 0;
const prNumber = process.env.GITHUB_EVENT_PULL_REQUEST_NUMBER;
const prUrl = repoUrl && prNumber ? `${repoUrl}/pull/${prNumber}` : void 0;
const compareUrl = repoUrl && includeGitInfo ? `${repoUrl}/compare/${buildInfo["git.commit_hash_short"]}..HEAD` : void 0;
if (repoUrl) buildInfo["git.repository_url"] = repoUrl;
if (prUrl) buildInfo["git.pull_request_url"] = prUrl;
if (compareUrl) buildInfo["git.compare_url"] = compareUrl;
}
const outputPath = resolve(outputDir, outputFileName);
const content = `// Auto-generated build info - do not edit manually
// Generated at: ${(/* @__PURE__ */ new Date()).toISOString()}
export const BUILD_INFO = ${JSON.stringify(buildInfo, null, 2)} as const;
export type BuildInfo = typeof BUILD_INFO;
`;
writeFileSync(outputPath, content);
console.log("Build info collected successfully:");
console.log(
` Service: ${buildInfo["service.name"]}@${buildInfo["service.version"]}`
);
console.log(` Environment: ${buildInfo["service.environment"]}`);
if (includeGitInfo) {
console.log(
` Git: ${buildInfo["git.branch"]}@${buildInfo["git.commit_hash_short"]}`
);
console.log(` Author: ${buildInfo["git.commit_author"]}`);
}
if (includeCiInfo) {
console.log(` Deployed by: ${buildInfo["deployment.user"]}`);
}
return buildInfo;
}
if (__require.main === module) {
const args = process.argv.slice(2);
const options = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
const nextArg = args[i + 1];
switch (arg) {
case "--package-json-dir":
options.packageJsonDir = nextArg;
i++;
break;
case "--output-dir":
options.outputDir = nextArg;
i++;
break;
case "--output-file":
options.outputFileName = nextArg;
i++;
break;
case "--service-name":
options.defaultServiceName = nextArg;
i++;
break;
case "--no-git":
options.includeGitInfo = false;
break;
case "--no-ci":
options.includeCiInfo = false;
break;
case "--help":
console.log(`
Usage: collect-build-info [options]
Options:
--package-json-dir <path> Path to directory containing package.json (default: current directory)
--output-dir <path> Path to output directory (default: src)
--output-file <name> Name of output file (default: build-info.ts)
--service-name <name> Default service name if not found in package.json
--no-git Skip git information collection
--no-ci Skip CI/CD information collection
--help Show this help message
`);
process.exit(0);
break;
}
}
try {
collectBuildInfo(options);
} catch (error) {
console.error("Error collecting build info:", error);
process.exit(1);
}
}
export {
collectBuildInfo
};
//# sourceMappingURL=collect-build-info.mjs.map