Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ci-changes.cjs

203 lines7.5 KB

1// Decides which CI jobs a change needs. Prints `key=value` lines for
2// $GITHUB_OUTPUT; a job runs unless its key is "false".
3//
4// node ci-changes.cjs --paths <file> gate by the listed paths
5// node ci-changes.cjs --paths <file> --full run every Linux job, gate only the macOS lint
6// node ci-changes.cjs changed paths unknown: run everything
7//
8// Every changed path maps to workspace packages, which then pull in their
9// dependents from package.json. A path this file does not recognize runs
10// everything, so a new root file or directory can only cost minutes.
11const fs = require("node:fs");
12const path = require("node:path");
13​
14const root = path.resolve(__dirname, "../..");
15​
16// Never read by a build, typecheck, or test. Lint still formats them.
17const INERT = [
18 /^docs\//,
19 /^\.(agents|cursor|vscode|devcontainer|macroscope|repos)\//,
20 /^[^/]+\.md$/,
21 /^LICENSE$/,
22];
23​
24// Run everything: the root vite.config.ts loads the first as setup for every
25// package's tests, and the vendored mobile tarballs are installed dependencies.
26const RUNS_EVERYTHING = [
27 /^packages\/shared\/src\/(HostProcess|testing\/longTempDir)\.ts$/,
28 /^apps\/mobile\/deps\//,
29];
30​
31// Paths outside a package, or files inside one that other packages read
32// through relative imports rather than a package.json dependency.
33const EXTRA_OWNERS = [
34 // Vite configs and tsconfigs of these apps import scripts/lib directly.
35 [/^scripts\/lib\//, ["@t3tools/desktop", "@t3tools/mobile", "@t3tools/web", "t3"]],
36 // scripts/build-desktop-artifact.ts reads the GNOME extension manifest.
37 [/^apps\/desktop\/gnome-extension\//, ["@t3tools/scripts"]],
38 [/^native\/browser-secret\//, ["@t3tools/desktop"]],
39 [/^native\/libghostty-vt\//, ["@t3tools/web"]],
40];
41const RUST = /^native\/(resource-monitor|kde-snap-shot|hyprland-snap-shot)\//;
42​
43// Tests that read another package's files without declaring it. The scripts
44// tests read app manifests, assets, and sources (cursor-sdk-packaging,
45// third-party-licenses, brand-assets, build-desktop-artifact).
46const TEST_ONLY_DEPS = {
47 "@t3tools/provider-pi": ["@t3tools/client-runtime"],
48 "@t3tools/scripts": [
49 "@t3tools/desktop",
50 "@t3tools/mobile",
51 "@t3tools/web",
52 "t3",
53 "@t3tools/provider-cursor",
54 ],
55};
56// Declared dependencies that only feed a build step: the server bundles the
57// built web app (apps/server/vite.config.ts) but never imports it.
58const BUILD_ONLY_DEPS = { t3: ["@t3tools/web"] };
59​
60// Sources scripts/mobile-native-static-check.ts lints, plus the tool and rule
61// configuration that decides how it lints them, plus the root package.json
62// that defines the lint:mobile command.
63const MOBILE_NATIVE =
64 /^apps\/mobile\/.*\.(swift|kt|kts)$|^apps\/mobile\/(\.swiftlint\.yml|detekt\.yml|\.editorconfig|Brewfile)$|^scripts\/mobile-native-static-check\.ts$|^package\.json$|^\.github\/workflows\/ci\.yml$|^\.github\/scripts\/ci-changes\.cjs$|^\.github\/actions\/ci-changes\//;
65​
66// Jobs that test every package except these, which have jobs of their own.
67const OWN_TEST_JOB = new Set(["t3", "@t3tools/web", "@t3tools/monorepo"]);
68​
69function readWorkspace() {
70 const patterns = [];
71 let inPackages = false;
72 for (const line of fs.readFileSync(path.join(root, "pnpm-workspace.yaml"), "utf8").split("\n")) {
73 if (/^\S/.test(line)) inPackages = line.startsWith("packages:");
74 const entry = inPackages && line.match(/^\s+-\s+["']?([^"'\s]+)/);
75 if (entry) patterns.push(entry[1]);
76 }
77​
78 const dirs = patterns.flatMap((pattern) => {
79 if (!pattern.endsWith("/*")) return [pattern];
80 const parent = pattern.slice(0, -2);
81 return fs.readdirSync(path.join(root, parent)).map((name) => `${parent}/${name}`);
82 });
83​
84 const packages = [];
85 for (const dir of dirs) {
86 const manifestPath = path.join(root, dir, "package.json");
87 if (!fs.existsSync(manifestPath)) continue;
88 const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
89 const deps = {
90 ...manifest.dependencies,
91 ...manifest.devDependencies,
92 ...manifest.peerDependencies,
93 };
94 packages.push({
95 name: manifest.name,
96 dir,
97 hasTest: Boolean(manifest.scripts?.test),
98 deps: Object.keys(deps).filter(
99 (dep) =>
100 String(deps[dep]).startsWith("workspace:") &&
101 !BUILD_ONLY_DEPS[manifest.name]?.includes(dep),
102 ),
103 });
104 }
105 return packages;
107​
108// Returns the packages a path touches, [] for an inert path, or null when the
109// path is not understood and everything must run.
110function ownersOf(file, packages) {
111 if (INERT.some((pattern) => pattern.test(file))) return [];
112 // A manifest can change dependencies or versions other packages read.
113 if (path.posix.basename(file) === "package.json") return null;
114 if (RUNS_EVERYTHING.some((pattern) => pattern.test(file))) return null;
115 // scripts/build-desktop-artifact.test.ts reads the crates' sources.
116 if (RUST.test(file)) return ["@t3tools/scripts"];
117​
118 const extra = EXTRA_OWNERS.flatMap(([pattern, names]) => (pattern.test(file) ? names : []));
119 const owner = packages
120 .filter((pkg) => file.startsWith(`${pkg.dir}/`))
121 .sort((a, b) => b.dir.length - a.dir.length)[0];
122 if (owner) return [owner.name, ...extra];
123 return extra.length > 0 ? extra : null;
125​
126function decide(files, packages, full) {
127 const all = new Set(packages.map((pkg) => pkg.name));
128 let affected = new Set();
129 let everything = full || files === null;
130 let rust = false;
131​
132 for (const file of files ?? []) {
133 if (RUST.test(file)) rust = true;
134 const owners = ownersOf(file, packages);
135 if (owners === null) {
136 everything = true;
137 console.error(`Runs everything: ${file}`);
138 } else {
139 for (const owner of owners) affected.add(owner);
140 }
141 }
142​
143 // Release smoke reads scripts/ itself, not what the scripts tests read.
144 let scriptsChanged = everything;
145 if (everything) {
146 affected = all;
147 } else {
148 // Add dependents until nothing changes.
149 let grew = true;
150 while (grew) {
151 grew = false;
152 for (const pkg of packages) {
153 if (!affected.has(pkg.name) && pkg.deps.some((dep) => affected.has(dep))) {
154 affected.add(pkg.name);
155 grew = true;
156 }
157 }
158 }
159 scriptsChanged = affected.has("@t3tools/scripts");
160 for (const [name, deps] of Object.entries(TEST_ONLY_DEPS)) {
161 if (deps.some((dep) => affected.has(dep))) affected.add(name);
162 }
163 }
164​
165 const tested = packages.filter(
166 (pkg) => pkg.hasTest && !OWN_TEST_JOB.has(pkg.name) && affected.has(pkg.name),
167 );
168 const has = (name) => affected.has(name);
169​
170 return {
171 affected: [...affected].sort(),
172 outputs: {
173 typecheck: affected.size > 0,
174 build: has("@t3tools/desktop") || has("t3") || has("@t3tools/web"),
175 test: tested.length > 0,
176 // Empty means every package the Test job covers.
177 test_packages: everything ? "" : tested.map((pkg) => pkg.name).join(" "),
178 test_web: has("@t3tools/web"),
179 test_server: has("t3"),
180 rust: everything || rust,
181 release_smoke: scriptsChanged,
182 mobile_native: files === null || files.some((file) => MOBILE_NATIVE.test(file)),
183 },
184 };
186​
187function main() {
188 const args = process.argv.slice(2);
189 const pathsIndex = args.indexOf("--paths");
190 const files =
191 pathsIndex === -1
192 ? null
193 : fs
194 .readFileSync(args[pathsIndex + 1], "utf8")
195 .split("\n")
196 .filter(Boolean);
197 const { affected, outputs } = decide(files, readWorkspace(), args.includes("--full"));
198​
199 console.error(`Affected packages: ${affected.join(", ") || "none"}`);
200 for (const [key, value] of Object.entries(outputs)) console.log(`${key}=${value}`);
202​
203main();