package-native-search-tools.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env node
  2. import { createWriteStream, mkdirSync } from "node:fs";
  3. import { createRequire } from "node:module";
  4. import { dirname, join, resolve } from "node:path";
  5. import process from "node:process";
  6. import { pipeline } from "node:stream/promises";
  7. import { fileURLToPath } from "node:url";
  8. import { buildNativeSearchTools } from "./build-native-search-tools.mjs";
  9. import { packSourceAsDeterministicTarGzip } from "./deterministic-tar-archive.mjs";
  10. import { resolveNativeSearchPrebuiltPlan } from "./native-search-tools-config.mjs";
  11. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  12. const require = createRequire(join(repoRoot, "packages/desktop/package.json"));
  13. export function resolveNativeSearchPackagedArtifactPath({ artifact, artifactsDir }) {
  14. return join(
  15. resolve(artifactsDir),
  16. "native-search-tools",
  17. `${artifact.toolId}-${artifact.release}`,
  18. artifact.releaseFileName,
  19. );
  20. }
  21. async function packWindowsZip(binaryPath, binaryName, archivePath) {
  22. const { ZipFile } = require("yazl");
  23. const zip = new ZipFile();
  24. // Bugfix: yazl 按本地时区编码 DOS 时间;固定本地时间并禁用扩展时间戳才能跨时区复现。
  25. zip.addFile(binaryPath, binaryName, {
  26. mtime: new Date(1980, 0, 1, 0, 0, 0, 0),
  27. mode: 0o100755,
  28. forceDosTimestamp: true,
  29. });
  30. zip.end();
  31. await pipeline(zip.outputStream, createWriteStream(archivePath));
  32. }
  33. export async function packageNativeSearchTools({
  34. platform = process.env.ZCODE_TARGET_OS || process.platform,
  35. arch = process.env.ZCODE_TARGET_ARCH || process.arch,
  36. artifactsDir = join(repoRoot, "dist/native-search-tools-deps"),
  37. buildOutputDir,
  38. jobs,
  39. } = {}) {
  40. const buildPlan = buildNativeSearchTools({
  41. platform,
  42. arch,
  43. outputDir: buildOutputDir,
  44. jobs,
  45. });
  46. const prebuiltPlan = resolveNativeSearchPrebuiltPlan({
  47. platform: buildPlan.platform,
  48. arch: buildPlan.arch,
  49. outputDir: buildPlan.outputDir,
  50. });
  51. await packNativeSearchPrebuiltArtifacts({ prebuiltPlan, artifactsDir });
  52. return prebuiltPlan;
  53. }
  54. export async function packNativeSearchPrebuiltArtifacts({ prebuiltPlan, artifactsDir }) {
  55. for (const artifact of prebuiltPlan.artifacts.filter(({ source }) => source === "producer")) {
  56. const archivePath = resolveNativeSearchPackagedArtifactPath({ artifact, artifactsDir });
  57. mkdirSync(dirname(archivePath), { recursive: true });
  58. if (artifact.archiveExt === "zip") {
  59. await packWindowsZip(artifact.binaryPath, artifact.binaryName, archivePath);
  60. } else {
  61. packSourceAsDeterministicTarGzip(artifact.binaryPath, archivePath);
  62. }
  63. console.log(`==> Packaged ${artifact.toolId} ${artifact.release} -> ${archivePath}`);
  64. }
  65. }
  66. function readOption(name) {
  67. const prefix = `--${name}=`;
  68. const inline = process.argv.find((arg) => arg.startsWith(prefix));
  69. if (inline) return inline.slice(prefix.length);
  70. const index = process.argv.indexOf(`--${name}`);
  71. return index >= 0 ? process.argv[index + 1] : undefined;
  72. }
  73. function readJobs() {
  74. const value = readOption("jobs");
  75. if (value === undefined) return undefined;
  76. const jobs = Number.parseInt(value, 10);
  77. if (!Number.isInteger(jobs) || jobs < 1) throw new Error(`invalid --jobs value: ${value}`);
  78. return jobs;
  79. }
  80. async function main() {
  81. await packageNativeSearchTools({
  82. platform: readOption("platform") ?? process.env.ZCODE_TARGET_OS ?? process.platform,
  83. arch: readOption("arch") ?? process.env.ZCODE_TARGET_ARCH ?? process.arch,
  84. artifactsDir: readOption("artifacts-dir"),
  85. buildOutputDir: readOption("build-output-dir"),
  86. jobs: readJobs(),
  87. });
  88. }
  89. if (resolve(process.argv[1] ?? "") === fileURLToPath(import.meta.url)) {
  90. try {
  91. await main();
  92. } catch (error) {
  93. console.error(error instanceof Error ? error.stack : error);
  94. process.exitCode = 1;
  95. }
  96. }