github-matrix.mjs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env node
  2. /**
  3. * Derive the GitHub Actions matrices from the checked-in package matrix
  4. * (`packages/<name>/prebuilds.json`). Single source: adding a platform
  5. * package extends CI and Release without editing a workflow.
  6. *
  7. * node scripts/github-matrix.mjs ci → one leg per distinct platform
  8. * node scripts/github-matrix.mjs release-prebuild → one leg per platform package
  9. */
  10. import path from 'node:path';
  11. import { platformDirs, readJson, root } from './repo.mjs';
  12. /** GitHub runner per prebuilds.json `platform` value — native builders only, no cross toolchain. */
  13. const RUNNERS = {
  14. 'linux-x64': 'ubuntu-24.04',
  15. 'linux-arm64': 'ubuntu-24.04-arm',
  16. };
  17. function runnerFor(platform) {
  18. const runner = RUNNERS[platform];
  19. if (!runner) {
  20. throw new Error(`missing GitHub runner for platform: ${platform}`);
  21. }
  22. return runner;
  23. }
  24. function platformManifests() {
  25. return platformDirs().map((dir) => ({
  26. dir,
  27. name: path.basename(dir),
  28. prebuilds: readJson(path.join(root, dir, 'prebuilds.json')),
  29. }));
  30. }
  31. function ciMatrix() {
  32. const platforms = [...new Set(platformManifests().map(({ prebuilds }) => prebuilds.platform))].sort();
  33. return {
  34. include: platforms.map((platform) => ({ platform, runner: runnerFor(platform) })),
  35. };
  36. }
  37. function releasePrebuildMatrix() {
  38. return {
  39. include: platformManifests().map(({ dir, name, prebuilds }) => ({
  40. platform: prebuilds.platform,
  41. package: name,
  42. dir,
  43. runner: runnerFor(prebuilds.platform),
  44. artifact: `prebuild-${name}`,
  45. })),
  46. };
  47. }
  48. const target = process.argv[2];
  49. const matrices = {
  50. ci: ciMatrix,
  51. 'release-prebuild': releasePrebuildMatrix,
  52. };
  53. if (!target || !matrices[target]) {
  54. console.error(`Usage: node scripts/github-matrix.mjs <${Object.keys(matrices).join('|')}>`);
  55. process.exit(1);
  56. }
  57. process.stdout.write(JSON.stringify(matrices[target]()));