github-matrix.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. 'darwin-x64': 'macos-15-intel',
  17. 'darwin-arm64': 'macos-latest',
  18. };
  19. function runnerFor(platform) {
  20. const runner = RUNNERS[platform];
  21. if (!runner) {
  22. throw new Error(`missing GitHub runner for platform: ${platform}`);
  23. }
  24. return runner;
  25. }
  26. function platformManifests() {
  27. return platformDirs().map((dir) => ({
  28. dir,
  29. name: path.basename(dir),
  30. prebuilds: readJson(path.join(root, dir, 'prebuilds.json')),
  31. }));
  32. }
  33. function ciMatrix() {
  34. const platforms = [...new Set(platformManifests().map(({ prebuilds }) => prebuilds.platform))].sort();
  35. return {
  36. include: platforms.map((platform) => ({ platform, runner: runnerFor(platform) })),
  37. };
  38. }
  39. function releasePrebuildMatrix() {
  40. return {
  41. include: platformManifests().map(({ dir, name, prebuilds }) => ({
  42. platform: prebuilds.platform,
  43. package: name,
  44. dir,
  45. runner: runnerFor(prebuilds.platform),
  46. artifact: `prebuild-${name}`,
  47. })),
  48. };
  49. }
  50. const target = process.argv[2];
  51. const matrices = {
  52. ci: ciMatrix,
  53. 'release-prebuild': releasePrebuildMatrix,
  54. compatibility: () => ciMatrix().include.flatMap((row) => [20, 22, 24, 26].map((node) => ({ ...row, node }))),
  55. };
  56. if (!target || !matrices[target]) {
  57. console.error(`Usage: node scripts/github-matrix.mjs <${Object.keys(matrices).join('|')}>`);
  58. process.exit(1);
  59. }
  60. process.stdout.write(JSON.stringify(matrices[target]()));