| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import { execSync } from "node:child_process";
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
- import { createRequire } from "node:module";
- import { resolve } from "node:path";
- import { fileURLToPath } from "node:url";
- const require = createRequire(import.meta.url);
- const moduleDir = import.meta.dirname;
- function findPackageDir(packageName, startDirs) {
- for (const startDir of startDirs) {
- let currentDir = resolve(startDir);
- while (true) {
- const packageJsonPath = resolve(currentDir, "package.json");
- if (existsSync(packageJsonPath)) {
- try {
- const packageJson = readJson(packageJsonPath);
- if (packageJson.name === packageName) {
- return currentDir;
- }
- } catch {
- // ignore invalid package.json and continue walking up
- }
- }
- const parentDir = resolve(currentDir, "..");
- if (parentDir === currentDir) {
- break;
- }
- currentDir = parentDir;
- }
- }
- throw new Error(`Unable to find package directory for ${packageName}`);
- }
- const desktopDir = findPackageDir("@zcode/desktop", [
- moduleDir,
- resolve(moduleDir, ".."),
- process.cwd(),
- ]);
- const workspaceDir = resolve(desktopDir, "../..");
- const metadataDir = resolve(desktopDir, "out/metadata");
- const metadataPath = resolve(metadataDir, "build-meta.json");
- function readJson(filePath) {
- return JSON.parse(readFileSync(filePath, "utf-8"));
- }
- function normalizeVersion(version) {
- if (typeof version !== "string" || version.length === 0) {
- return "unknown";
- }
- const normalized = version.replace(/^[^\d]*/, "");
- return normalized || version;
- }
- function resolveInstalledPackageVersion(packageName, fallbackVersion) {
- try {
- const packageJsonPath = require.resolve(`${packageName}/package.json`, { paths: [desktopDir] });
- return normalizeVersion(readJson(packageJsonPath).version);
- } catch {
- return normalizeVersion(fallbackVersion);
- }
- }
- function resolveCommitId() {
- try {
- return execSync("git rev-parse --short=8 HEAD", {
- cwd: workspaceDir,
- stdio: ["ignore", "pipe", "ignore"],
- })
- .toString()
- .trim();
- } catch {
- return process.env.ZCODE_COMMIT ?? "unknown";
- }
- }
- export function collectBuildMetadata() {
- const rootPackageJson = readJson(resolve(workspaceDir, "package.json"));
- const desktopPackageJson = readJson(resolve(desktopDir, "package.json"));
- return {
- appVersion: normalizeVersion(rootPackageJson.version),
- buildCommitId: resolveCommitId(),
- buildTime: new Date().toISOString(),
- electronBuilderVersion: resolveInstalledPackageVersion(
- "electron-builder",
- desktopPackageJson.devDependencies?.["electron-builder"],
- ),
- };
- }
- export function readBuildMetadata() {
- if (!existsSync(metadataPath)) {
- return null;
- }
- try {
- return readJson(metadataPath);
- } catch {
- return null;
- }
- }
- export function getBuildMetadata() {
- return readBuildMetadata() ?? collectBuildMetadata();
- }
- export function writeBuildMetadata() {
- // About 之前分别在 tsup、vite 里各算一份 commit 和时间。
- // 问题原因:两次构建是独立进程,时间点天然不一致;后面再打包时,最终安装包里展示的信息也不一定对应同一次产物。
- // 这里先统一落盘成 build-meta.json,再让构建和运行时都复用同一份数据,保证 about 可追溯。
- const metadata = collectBuildMetadata();
- mkdirSync(metadataDir, { recursive: true });
- writeFileSync(metadataPath, `${JSON.stringify(metadata, null, 2)}\n`, "utf-8");
- return metadata;
- }
- export function getBuildMetadataPath() {
- return metadataPath;
- }
- const entryFilePath = process.argv[1] ? resolve(process.argv[1]) : null;
- const currentFilePath = fileURLToPath(import.meta.url);
- if (entryFilePath === currentFilePath) {
- const metadata = writeBuildMetadata();
- process.stdout.write(`[build-meta] wrote ${metadataPath}\n`);
- process.stdout.write(
- `[build-meta] commit=${metadata.buildCommitId} time=${metadata.buildTime}\n`,
- );
- }
|