assemble-prebuilds.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env node
  2. /**
  3. * Assemble downloaded release artifacts into the platform packages and
  4. * verify the result. The Release workflow's build legs upload one
  5. * `prebuild-<package>` artifact per platform package (its `bin/` payload);
  6. * this script copies each into `packages/<package>/bin/` and then checks
  7. * every declared binary for presence and native architecture.
  8. *
  9. * Usage: `node scripts/assemble-prebuilds.mjs <artifact-root>`.
  10. */
  11. import fs from 'node:fs';
  12. import path from 'node:path';
  13. import { platformDirs, root, verifyPlatformBinaries } from './repo.mjs';
  14. const artifactRoot = path.resolve(process.argv[2] || '.release/prebuild-artifacts');
  15. if (!fs.existsSync(artifactRoot)) {
  16. throw new Error(`prebuild artifact directory does not exist: ${artifactRoot}`);
  17. }
  18. const platforms = platformDirs().map((dir) => path.basename(dir));
  19. for (const name of platforms) {
  20. const binDir = path.join(root, 'packages', name, 'bin');
  21. fs.rmSync(binDir, { recursive: true, force: true });
  22. fs.mkdirSync(binDir, { recursive: true });
  23. }
  24. for (const artifactName of fs.readdirSync(artifactRoot)) {
  25. const artifactDir = path.join(artifactRoot, artifactName);
  26. if (!fs.statSync(artifactDir).isDirectory()) continue;
  27. const name = platforms.find((candidate) => artifactName === `prebuild-${candidate}`);
  28. if (!name) {
  29. throw new Error(`cannot map artifact to a platform package: ${artifactName}`);
  30. }
  31. for (const file of fs.readdirSync(artifactDir)) {
  32. const source = path.join(artifactDir, file);
  33. const destination = path.join(root, 'packages', name, 'bin', file);
  34. fs.cpSync(source, destination, { recursive: true, preserveTimestamps: true });
  35. console.log(`Copied ${path.relative(root, source)} -> ${path.relative(root, destination)}`);
  36. }
  37. }
  38. for (const dir of platformDirs()) {
  39. const metadata = JSON.parse(fs.readFileSync(path.join(root, dir, 'prebuilds.json'), 'utf8'));
  40. for (const binary of metadata.binaries) {
  41. if (binary.kind === 'static-musl') fs.chmodSync(path.join(root, dir, binary.path), 0o755);
  42. }
  43. const { name, count } = verifyPlatformBinaries(path.join(root, dir));
  44. console.log(`Verified ${name}: ${count} binaries`);
  45. }