|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Prints multiline paths for actions/upload-artifact `path`, derived from the Nx project |
| 4 | + * graph: merged `outputs` of `build:transpile`, `build:types`, and `build:extension` for |
| 5 | + * every project under `packages/` and `dev-packages/`. |
| 6 | + * |
| 7 | + * Each line is an absolute path pattern suitable for @actions/glob (same style as the |
| 8 | + * previous hand-maintained BUILD_PATHS list). Uses a wildcard in the package name segment |
| 9 | + * plus a shared path suffix, except for the single-segment `build` output directory, which |
| 10 | + * must stay concrete (`packages/deno/build`) so we do not match every package's `build/` tree. |
| 11 | + * |
| 12 | + * Usage: GITHUB_WORKSPACE=<abs repo> yarn ci:print-build-artifact-paths |
| 13 | + * (defaults to cwd when GITHUB_WORKSPACE is unset) |
| 14 | + */ |
| 15 | +import { execSync } from "node:child_process"; |
| 16 | +import fs from "node:fs"; |
| 17 | +import path from "node:path"; |
| 18 | +import { fileURLToPath } from "node:url"; |
| 19 | + |
| 20 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 21 | +const workspaceRoot = path.resolve(__dirname, ".."); |
| 22 | +const graphPath = path.join( |
| 23 | + workspaceRoot, |
| 24 | + ".nx", |
| 25 | + "ci-print-build-artifact-paths-graph.json", |
| 26 | +); |
| 27 | + |
| 28 | +const TARGETS = ["build:transpile", "build:types", "build:extension"]; |
| 29 | + |
| 30 | +fs.mkdirSync(path.dirname(graphPath), { recursive: true }); |
| 31 | +execSync(`yarn nx graph --file="${graphPath}"`, { |
| 32 | + cwd: workspaceRoot, |
| 33 | + stdio: ["ignore", "pipe", "inherit"], |
| 34 | +}); |
| 35 | + |
| 36 | +const { graph } = JSON.parse(fs.readFileSync(graphPath, "utf8")); |
| 37 | +try { |
| 38 | + fs.unlinkSync(graphPath); |
| 39 | +} catch { |
| 40 | + // ignore |
| 41 | +} |
| 42 | + |
| 43 | +/** @type {Map<string, Set<string>>} key = `${kind}\0${suffix}` */ |
| 44 | +const groups = new Map(); |
| 45 | + |
| 46 | +for (const node of Object.values(graph.nodes)) { |
| 47 | + const root = node.data?.root; |
| 48 | + if ( |
| 49 | + !root || |
| 50 | + (!root.startsWith("packages/") && !root.startsWith("dev-packages/")) |
| 51 | + ) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + const [kind, pkg] = root.split("/"); |
| 56 | + if (!kind || !pkg) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + const targets = node.data?.targets || {}; |
| 61 | + for (const targetName of TARGETS) { |
| 62 | + const outputs = targets[targetName]?.outputs; |
| 63 | + if (!Array.isArray(outputs)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + for (const output of outputs) { |
| 68 | + const rel = output.replace(/\{projectRoot\}/g, root).replace(/\\/g, "/"); |
| 69 | + const prefix = `${kind}/${pkg}/`; |
| 70 | + if (!rel.startsWith(prefix)) { |
| 71 | + throw new Error( |
| 72 | + `Unexpected Nx output (missing project prefix): ${rel}`, |
| 73 | + ); |
| 74 | + } |
| 75 | + const suffix = rel.slice(prefix.length); |
| 76 | + const key = `${kind}\0${suffix}`; |
| 77 | + if (!groups.has(key)) { |
| 78 | + groups.set(key, new Set()); |
| 79 | + } |
| 80 | + groups.get(key).add(pkg); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const ws = (process.env.GITHUB_WORKSPACE || workspaceRoot).replace(/\\/g, "/"); |
| 86 | +const lines = new Set(); |
| 87 | + |
| 88 | +for (const [key, pkgSet] of groups) { |
| 89 | + const [kind, suffix] = key.split("\0"); |
| 90 | + const needsConcretePath = |
| 91 | + pkgSet.size === 1 && |
| 92 | + !suffix.includes("/") && |
| 93 | + !/[?*]/.test(suffix) && |
| 94 | + suffix === "build"; |
| 95 | + |
| 96 | + if (needsConcretePath) { |
| 97 | + const pkg = [...pkgSet][0]; |
| 98 | + lines.add(`${ws}/${kind}/${pkg}/build`); |
| 99 | + } else { |
| 100 | + lines.add(`${ws}/${kind}/*/${suffix}`); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +process.stdout.write([...lines].sort((a, b) => a.localeCompare(b)).join("\n")); |
| 105 | +if (lines.size) { |
| 106 | + process.stdout.write("\n"); |
| 107 | +} |
0 commit comments