将内部脚本归入 build、release、generate、dev 和 checks,保留公开安装脚本路径。 同步命令、CI、发布流程、脚本间调用及根目录定位,补齐脚本说明与完成报告。 验证:929 项测试、类型与版本检查通过;二进制及插件构建、冒烟与打包通过。 OpenAPI 产物不变;工作流 Schema 导出存在基线即可复现的 Zod transform 错误,单独记录。
139 lines
5.5 KiB
JavaScript
139 lines
5.5 KiB
JavaScript
import { mkdirSync, readFileSync, existsSync, unlinkSync, symlinkSync } from "node:fs";
|
|
import { spawnSync } from "node:child_process";
|
|
import { dirname, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
|
|
const bun = process.env.BUN_BIN || "bun";
|
|
|
|
const TARGET_MAP = {
|
|
"darwin-arm64": { bunTarget: "bun-darwin-arm64", outfile: "dist/bin/hose-use-darwin-arm64" },
|
|
"darwin-x64": { bunTarget: "bun-darwin-x64", outfile: "dist/bin/hose-use-darwin-x64" },
|
|
"linux-x64": { bunTarget: "bun-linux-x64", outfile: "dist/bin/hose-use-linux-x64" },
|
|
"linux-arm64": { bunTarget: "bun-linux-arm64", outfile: "dist/bin/hose-use-linux-arm64" },
|
|
"windows-x64": { bunTarget: "bun-windows-x64", outfile: "dist/bin/hose-use-windows-x64.exe" }
|
|
};
|
|
|
|
const args = process.argv.slice(2);
|
|
const isAll = args.includes("--all") || args.includes("--matrix");
|
|
const targetArg = args.find((arg) => arg.startsWith("--target="))?.replace("--target=", "");
|
|
const isRelease = args.includes("--release") ||
|
|
process.env.HOSE_BUILD_RELEASE === "1" ||
|
|
(Boolean(process.env.CI) && process.env.GITHUB_REF_TYPE === "tag");
|
|
|
|
function getBuildInfo(releaseMode) {
|
|
const pkg = JSON.parse(readFileSync(resolve(root, "package.json"), "utf8"));
|
|
if (releaseMode) return { version: pkg.version, channel: "release", isRelease: true };
|
|
|
|
try {
|
|
const result = spawnSync("git", ["describe", "--tags", "--long", "--dirty", "--always"], { cwd: root, encoding: "utf8" });
|
|
if (result.status === 0 && result.stdout) {
|
|
const trimmed = result.stdout.trim();
|
|
const dirty = trimmed.endsWith("-dirty");
|
|
const clean = dirty ? trimmed.slice(0, -6) : trimmed;
|
|
const longMatch = clean.match(/^v?(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.]+)?)-(\d+)-g([0-9a-f]+)$/);
|
|
let baseVersion = pkg.version;
|
|
let commitsAhead = 0;
|
|
let commit = "";
|
|
if (longMatch) {
|
|
baseVersion = longMatch[1];
|
|
commitsAhead = Number.parseInt(longMatch[2], 10);
|
|
commit = longMatch[3];
|
|
} else {
|
|
const revision = spawnSync("git", ["rev-parse", "--short", "HEAD"], { cwd: root, encoding: "utf8" });
|
|
if (revision.status === 0 && revision.stdout) commit = revision.stdout.trim();
|
|
}
|
|
const dirtySuffix = dirty ? ".dirty" : "";
|
|
const commitSuffix = commit ? `+g${commit}${dirtySuffix}` : dirty ? "+dirty" : "";
|
|
return {
|
|
version: `${baseVersion}-dev.${commitsAhead}${commitSuffix}`,
|
|
channel: "dev",
|
|
isRelease: false,
|
|
git: { baseVersion, commitsAhead, commit, dirty, describe: trimmed }
|
|
};
|
|
}
|
|
} catch {
|
|
// Fall back below if git is unavailable.
|
|
}
|
|
return { version: `${pkg.version}-dev.0+local`, channel: "dev", isRelease: false };
|
|
}
|
|
|
|
const buildInfo = getBuildInfo(isRelease);
|
|
const buildEnv = {
|
|
...process.env,
|
|
HOSE_BUILD_VERSION: buildInfo.version,
|
|
HOSE_BUILD_CHANNEL: buildInfo.channel,
|
|
HOSE_BUILD_IS_RELEASE: String(buildInfo.isRelease),
|
|
...(buildInfo.git ? {
|
|
HOSE_BUILD_GIT_BASE: buildInfo.git.baseVersion,
|
|
HOSE_BUILD_GIT_COMMITS_AHEAD: String(buildInfo.git.commitsAhead),
|
|
HOSE_BUILD_GIT_COMMIT: buildInfo.git.commit,
|
|
HOSE_BUILD_GIT_DIRTY: String(buildInfo.git.dirty),
|
|
HOSE_BUILD_GIT_DESCRIBE: buildInfo.git.describe
|
|
} : {})
|
|
};
|
|
|
|
console.log(`[build:binary] version: ${buildInfo.version} (${buildInfo.channel}, isRelease: ${buildInfo.isRelease})`);
|
|
|
|
if (isAll) {
|
|
for (const [name, config] of Object.entries(TARGET_MAP)) {
|
|
buildTarget(name, config.bunTarget, resolve(root, config.outfile), "src/cli.ts", buildEnv);
|
|
}
|
|
} else if (targetArg) {
|
|
const config = TARGET_MAP[targetArg];
|
|
if (!config) {
|
|
throw new Error(`Unknown target: ${targetArg}. Available targets: ${Object.keys(TARGET_MAP).join(", ")}`);
|
|
}
|
|
buildTarget(targetArg, config.bunTarget, resolve(root, config.outfile), "src/cli.ts", buildEnv);
|
|
} else {
|
|
const outputHose = resolve(root, "dist/bin/hose-use");
|
|
buildTarget("host", null, outputHose, "src/cli.ts", buildEnv);
|
|
|
|
// Unix Multicall Binary: 创建 hose-aipro -> hose-use 软链接别名
|
|
const outputAipro = resolve(root, "dist/bin/hose-aipro");
|
|
try {
|
|
if (existsSync(outputAipro)) unlinkSync(outputAipro);
|
|
symlinkSync("hose-use", outputAipro);
|
|
console.log(`[build:binary] created multicall symlink: ${outputAipro} -> hose-use`);
|
|
} catch {
|
|
// 忽略软链接异常
|
|
}
|
|
}
|
|
|
|
function buildTarget(name, bunTarget, output, entry = "src/cli.ts", env = process.env) {
|
|
mkdirSync(dirname(output), { recursive: true });
|
|
const buildArgs = ["build", "--compile", "--env=HOSE_BUILD_*"];
|
|
if (bunTarget) {
|
|
buildArgs.push(`--target=${bunTarget}`);
|
|
}
|
|
buildArgs.push("--outfile", output, entry);
|
|
|
|
console.log(`[build:binary] compiling target: ${name} -> ${output}`);
|
|
run(bun, buildArgs, { env });
|
|
|
|
if (process.platform === "darwin" && (name.startsWith("darwin") || name === "host")) {
|
|
try {
|
|
run("/usr/bin/codesign", ["--remove-signature", output], { quiet: true });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
try {
|
|
run("/usr/bin/codesign", ["--force", "--sign", "-", output], { quiet: true });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
|
|
function run(command, args, options = {}) {
|
|
const result = spawnSync(command, args, {
|
|
cwd: root,
|
|
stdio: options.quiet ? ["ignore", "ignore", "ignore"] : "inherit",
|
|
env: options.env ?? process.env
|
|
});
|
|
if (result.error) throw result.error;
|
|
if (result.status !== 0) {
|
|
throw new Error(`${command} ${args.join(" ")} failed with status ${result.status}`);
|
|
}
|
|
}
|