|
| 1 | +import { execSync } from "node:child_process"; |
| 2 | +import { appendFileSync } from "node:fs"; |
| 3 | + |
| 4 | +/** @typedef {{ runNode: boolean; runMacos: boolean; runAndroid: boolean }} ChangedScope */ |
| 5 | + |
| 6 | +const DOCS_PATH_RE = /^(docs\/|.*\.mdx?$)/; |
| 7 | +const MACOS_PROTOCOL_GEN_RE = |
| 8 | + /^(apps\/macos\/Sources\/OpenClawProtocol\/|apps\/shared\/OpenClawKit\/Sources\/OpenClawProtocol\/)/; |
| 9 | +const MACOS_NATIVE_RE = /^(apps\/macos\/|apps\/ios\/|apps\/shared\/|Swabble\/)/; |
| 10 | +const ANDROID_NATIVE_RE = /^(apps\/android\/|apps\/shared\/)/; |
| 11 | +const NODE_SCOPE_RE = |
| 12 | + /^(src\/|test\/|extensions\/|packages\/|scripts\/|ui\/|\.github\/|openclaw\.mjs$|package\.json$|pnpm-lock\.yaml$|pnpm-workspace\.yaml$|tsconfig.*\.json$|vitest.*\.ts$|tsdown\.config\.ts$|\.oxlintrc\.json$|\.oxfmtrc\.jsonc$)/; |
| 13 | +const NATIVE_ONLY_RE = |
| 14 | + /^(apps\/android\/|apps\/ios\/|apps\/macos\/|apps\/shared\/|Swabble\/|appcast\.xml$)/; |
| 15 | + |
| 16 | +/** |
| 17 | + * @param {string[]} changedPaths |
| 18 | + * @returns {ChangedScope} |
| 19 | + */ |
| 20 | +export function detectChangedScope(changedPaths) { |
| 21 | + if (!Array.isArray(changedPaths) || changedPaths.length === 0) { |
| 22 | + return { runNode: true, runMacos: true, runAndroid: true }; |
| 23 | + } |
| 24 | + |
| 25 | + let runNode = false; |
| 26 | + let runMacos = false; |
| 27 | + let runAndroid = false; |
| 28 | + let hasNonDocs = false; |
| 29 | + let hasNonNativeNonDocs = false; |
| 30 | + |
| 31 | + for (const rawPath of changedPaths) { |
| 32 | + const path = String(rawPath).trim(); |
| 33 | + if (!path) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + if (DOCS_PATH_RE.test(path)) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + hasNonDocs = true; |
| 42 | + |
| 43 | + if (!MACOS_PROTOCOL_GEN_RE.test(path) && MACOS_NATIVE_RE.test(path)) { |
| 44 | + runMacos = true; |
| 45 | + } |
| 46 | + |
| 47 | + if (ANDROID_NATIVE_RE.test(path)) { |
| 48 | + runAndroid = true; |
| 49 | + } |
| 50 | + |
| 51 | + if (NODE_SCOPE_RE.test(path)) { |
| 52 | + runNode = true; |
| 53 | + } |
| 54 | + |
| 55 | + if (!NATIVE_ONLY_RE.test(path)) { |
| 56 | + hasNonNativeNonDocs = true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (!runNode && hasNonDocs && hasNonNativeNonDocs) { |
| 61 | + runNode = true; |
| 62 | + } |
| 63 | + |
| 64 | + return { runNode, runMacos, runAndroid }; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * @param {string} base |
| 69 | + * @param {string} [head] |
| 70 | + * @returns {string[]} |
| 71 | + */ |
| 72 | +export function listChangedPaths(base, head = "HEAD") { |
| 73 | + if (!base) { |
| 74 | + return []; |
| 75 | + } |
| 76 | + const output = execSync(`git diff --name-only ${base} ${head}`, { |
| 77 | + stdio: ["ignore", "pipe", "pipe"], |
| 78 | + encoding: "utf8", |
| 79 | + }); |
| 80 | + return output |
| 81 | + .split("\n") |
| 82 | + .map((line) => line.trim()) |
| 83 | + .filter((line) => line.length > 0); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * @param {ChangedScope} scope |
| 88 | + * @param {string} [outputPath] |
| 89 | + */ |
| 90 | +export function writeGitHubOutput(scope, outputPath = process.env.GITHUB_OUTPUT) { |
| 91 | + if (!outputPath) { |
| 92 | + throw new Error("GITHUB_OUTPUT is required"); |
| 93 | + } |
| 94 | + appendFileSync(outputPath, `run_node=${scope.runNode}\n`, "utf8"); |
| 95 | + appendFileSync(outputPath, `run_macos=${scope.runMacos}\n`, "utf8"); |
| 96 | + appendFileSync(outputPath, `run_android=${scope.runAndroid}\n`, "utf8"); |
| 97 | +} |
| 98 | + |
| 99 | +function isDirectRun() { |
| 100 | + const direct = process.argv[1]; |
| 101 | + return Boolean(direct && import.meta.url.endsWith(direct)); |
| 102 | +} |
| 103 | + |
| 104 | +/** @param {string[]} argv */ |
| 105 | +function parseArgs(argv) { |
| 106 | + const args = { base: "", head: "HEAD" }; |
| 107 | + for (let i = 0; i < argv.length; i += 1) { |
| 108 | + if (argv[i] === "--base") { |
| 109 | + args.base = argv[i + 1] ?? ""; |
| 110 | + i += 1; |
| 111 | + continue; |
| 112 | + } |
| 113 | + if (argv[i] === "--head") { |
| 114 | + args.head = argv[i + 1] ?? "HEAD"; |
| 115 | + i += 1; |
| 116 | + } |
| 117 | + } |
| 118 | + return args; |
| 119 | +} |
| 120 | + |
| 121 | +if (isDirectRun()) { |
| 122 | + const args = parseArgs(process.argv.slice(2)); |
| 123 | + try { |
| 124 | + const changedPaths = listChangedPaths(args.base, args.head); |
| 125 | + if (changedPaths.length === 0) { |
| 126 | + writeGitHubOutput({ runNode: true, runMacos: true, runAndroid: true }); |
| 127 | + process.exit(0); |
| 128 | + } |
| 129 | + writeGitHubOutput(detectChangedScope(changedPaths)); |
| 130 | + } catch { |
| 131 | + writeGitHubOutput({ runNode: true, runMacos: true, runAndroid: true }); |
| 132 | + } |
| 133 | +} |
0 commit comments