@delta-base/observability
Version:
Observability framework for delta-base applications
187 lines (183 loc) • 7.45 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/scripts/collect-build-info.ts
var collect_build_info_exports = {};
__export(collect_build_info_exports, {
collectBuildInfo: () => collectBuildInfo
});
module.exports = __toCommonJS(collect_build_info_exports);
var import_node_child_process = require("child_process");
var import_node_fs = require("fs");
var import_node_path = require("path");
function safeExec(command, fallback = "") {
try {
return (0, import_node_child_process.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 = (0, import_node_path.resolve)(packageJsonDir, "package.json");
let packageJson = {};
if ((0, import_node_fs.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 = (0, import_node_path.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;
`;
(0, import_node_fs.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);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
collectBuildInfo
});
//# sourceMappingURL=collect-build-info.js.map