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

ci-changes.cjs

190 lines6.7 KB

1// Unknown paths run every job; recognized packages pull in their dependents.
2// scripts/check-changed.ts shares these decisions so local checks match CI.
3const fs = require("node:fs");
4const path = require("node:path");
5​
6const root = path.resolve(__dirname, "../..");
7​
8const INERT = [
9 /^docs\//,
10 /^\.(agents|cursor|vscode|devcontainer|macroscope|repos)\//,
11 /^[^/]+\.md$/,
12 /^LICENSE$/,
13];
14​
15// Shared test setup and vendored mobile dependencies affect every package.
16const RUNS_EVERYTHING = [
17 /^packages\/shared\/src\/(HostProcess|testing\/(longTempDir|providerEnv))\.ts$/,
18 /^apps\/mobile\/deps\//,
19];
20​
21// Includes cross-package reads absent from package.json dependencies.
22const EXTRA_OWNERS = [
23 // Vite configs and tsconfigs of these apps import scripts/lib directly.
24 [/^scripts\/lib\//, ["@t3tools/desktop", "@t3tools/mobile", "@t3tools/web", "t3"]],
25 // scripts/build-desktop-artifact.ts reads the GNOME extension manifest.
26 [/^apps\/desktop\/gnome-extension\//, ["@t3tools/scripts"]],
27 [/^native\/browser-secret\//, ["@t3tools/desktop"]],
28 [/^native\/kde-snap-shot\//, ["@t3tools/desktop"]],
29 [/^native\/libghostty-vt\//, ["@t3tools/web"]],
30 // apps/server/src/cloud/ConnectReachability.test.ts checks the guide's headings.
31 [/^docs\/user\/t3-connect-troubleshooting\.md$/, ["t3"]],
32];
33const RUST = /^native\/(resource-monitor|kde-snap-shot|hyprland-snap-shot)\//;
34​
35// These tests read other packages without declaring dependencies.
36const TEST_ONLY_DEPS = {
37 // Evals import the server's source to test its real model-facing text.
38 "@t3tools/evals": ["t3"],
39 "@t3tools/provider-pi": ["@t3tools/client-runtime"],
40 "@t3tools/scripts": [
41 "@t3tools/desktop",
42 "@t3tools/mobile",
43 "@t3tools/web",
44 "t3",
45 "@t3tools/provider-cursor",
46 ],
47};
48// The server bundles the web build without importing it.
49const BUILD_ONLY_DEPS = { t3: ["@t3tools/web"] };
50​
51const MOBILE_NATIVE =
52 /^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\//;
53​
54const OWN_TEST_JOB = new Set(["t3", "@t3tools/web", "@t3tools/monorepo"]);
55​
56function readWorkspace() {
57 const patterns = [];
58 let inPackages = false;
59 for (const line of fs.readFileSync(path.join(root, "pnpm-workspace.yaml"), "utf8").split("\n")) {
60 if (/^\S/.test(line)) inPackages = line.startsWith("packages:");
61 const entry = inPackages && line.match(/^\s+-\s+["']?([^"'\s]+)/);
62 if (entry) patterns.push(entry[1]);
63 }
64​
65 const dirs = patterns.flatMap((pattern) => {
66 if (!pattern.endsWith("/*")) return [pattern];
67 const parent = pattern.slice(0, -2);
68 return fs.readdirSync(path.join(root, parent)).map((name) => `${parent}/${name}`);
69 });
70​
71 const packages = [];
72 for (const dir of dirs) {
73 const manifestPath = path.join(root, dir, "package.json");
74 if (!fs.existsSync(manifestPath)) continue;
75 const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
76 const deps = {
77 ...manifest.dependencies,
78 ...manifest.devDependencies,
79 ...manifest.peerDependencies,
80 };
81 packages.push({
82 name: manifest.name,
83 dir,
84 hasTest: Boolean(manifest.scripts?.test),
85 deps: Object.keys(deps).filter(
86 (dep) =>
87 String(deps[dep]).startsWith("workspace:") &&
88 !BUILD_ONLY_DEPS[manifest.name]?.includes(dep),
89 ),
90 });
91 }
92 return packages;
94​
95// [] means inert; null means unknown and requires every job.
96function ownersOf(file, packages) {
97 const extra = EXTRA_OWNERS.flatMap(([pattern, names]) => (pattern.test(file) ? names : []));
98 if (INERT.some((pattern) => pattern.test(file))) return extra;
99 // A manifest can change dependencies or versions other packages read.
100 if (path.posix.basename(file) === "package.json") return null;
101 if (RUNS_EVERYTHING.some((pattern) => pattern.test(file))) return null;
102 // scripts/build-desktop-artifact.test.ts reads the crates' sources.
103 if (RUST.test(file)) return ["@t3tools/scripts", ...extra];
104​
105 const owner = packages
106 .filter((pkg) => file.startsWith(`${pkg.dir}/`))
107 .sort((a, b) => b.dir.length - a.dir.length)[0];
108 if (owner) return [owner.name, ...extra];
109 return extra.length > 0 ? extra : null;
111​
112function decide(files, packages, full) {
113 const all = new Set(packages.map((pkg) => pkg.name));
114 let affected = new Set();
115 let everything = full || files === null;
116 let rust = false;
117​
118 for (const file of files ?? []) {
119 if (RUST.test(file)) rust = true;
120 const owners = ownersOf(file, packages);
121 if (owners === null) {
122 everything = true;
123 console.error(`Runs everything: ${file}`);
124 } else {
125 for (const owner of owners) affected.add(owner);
126 }
127 }
128​
129 // Release smoke reads scripts/ itself, not what the scripts tests read.
130 let scriptsChanged = everything;
131 if (everything) {
132 affected = all;
133 } else {
134 let grew = true;
135 while (grew) {
136 grew = false;
137 for (const pkg of packages) {
138 if (!affected.has(pkg.name) && pkg.deps.some((dep) => affected.has(dep))) {
139 affected.add(pkg.name);
140 grew = true;
141 }
142 }
143 }
144 scriptsChanged = affected.has("@t3tools/scripts");
145 for (const [name, deps] of Object.entries(TEST_ONLY_DEPS)) {
146 if (deps.some((dep) => affected.has(dep))) affected.add(name);
147 }
148 }
149​
150 const tested = packages.filter(
151 (pkg) => pkg.hasTest && !OWN_TEST_JOB.has(pkg.name) && affected.has(pkg.name),
152 );
153 const has = (name) => affected.has(name);
154​
155 return {
156 affected: [...affected].sort(),
157 outputs: {
158 typecheck: affected.size > 0,
159 build: has("@t3tools/desktop") || has("t3") || has("@t3tools/web"),
160 test: tested.length > 0,
161 // Empty means every package the Test job covers.
162 test_packages: everything ? "" : tested.map((pkg) => pkg.name).join(" "),
163 test_web: has("@t3tools/web"),
164 test_server: has("t3"),
165 rust: everything || rust,
166 release_smoke: scriptsChanged,
167 mobile_native: files === null || files.some((file) => MOBILE_NATIVE.test(file)),
168 },
169 };
171​
172function main() {
173 const args = process.argv.slice(2);
174 const pathsIndex = args.indexOf("--paths");
175 const files =
176 pathsIndex === -1
177 ? null
178 : fs
179 .readFileSync(args[pathsIndex + 1], "utf8")
180 .split("\n")
181 .filter(Boolean);
182 const { affected, outputs } = decide(files, readWorkspace(), args.includes("--full"));
183​
184 console.error(`Affected packages: ${affected.join(", ") || "none"}`);
185 for (const [key, value] of Object.entries(outputs)) console.log(`${key}=${value}`);
187​
188module.exports = { readWorkspace, decide };
189​
190if (require.main === module) main();