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

ci-changes.cjs

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