Open History to see the last commit
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)\.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];
31const RUST = /^native\/(resource-monitor|kde-snap-shot|hyprland-snap-shot)\//;
32
33// These tests read other packages without declaring dependencies.
34const TEST_ONLY_DEPS = {
35 "@t3tools/provider-pi": ["@t3tools/client-runtime"],
36 "@t3tools/scripts": [
37 "@t3tools/desktop",
38 "@t3tools/mobile",
39 "@t3tools/web",
40 "t3",
41 "@t3tools/provider-cursor",
42 ],
43};
44// The server bundles the web build without importing it.
45const BUILD_ONLY_DEPS = { t3: ["@t3tools/web"] };
46
47const MOBILE_NATIVE =
48 /^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\//;
49
50const OWN_TEST_JOB = new Set(["t3", "@t3tools/web", "@t3tools/monorepo"]);
51
52function readWorkspace() {
53 const patterns = [];
54 let inPackages = false;
55 for (const line of fs.readFileSync(path.join(root, "pnpm-workspace.yaml"), "utf8").split("\n")) {
56 if (/^\S/.test(line)) inPackages = line.startsWith("packages:");
57 const entry = inPackages && line.match(/^\s+-\s+["']?([^"'\s]+)/);
58 if (entry) patterns.push(entry[1]);
59 }
60
61 const dirs = patterns.flatMap((pattern) => {
62 if (!pattern.endsWith("/*")) return [pattern];
63 const parent = pattern.slice(0, -2);
64 return fs.readdirSync(path.join(root, parent)).map((name) => `${parent}/${name}`);
65 });
66
67 const packages = [];
68 for (const dir of dirs) {
69 const manifestPath = path.join(root, dir, "package.json");
70 if (!fs.existsSync(manifestPath)) continue;
71 const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
72 const deps = {
73 ...manifest.dependencies,
74 ...manifest.devDependencies,
75 ...manifest.peerDependencies,
76 };
77 packages.push({
78 name: manifest.name,
79 dir,
80 hasTest: Boolean(manifest.scripts?.test),
81 deps: Object.keys(deps).filter(
82 (dep) =>
83 String(deps[dep]).startsWith("workspace:") &&
84 !BUILD_ONLY_DEPS[manifest.name]?.includes(dep),
85 ),
86 });
87 }
88 return packages;
89}
90
91// [] means inert; null means unknown and requires every job.
92function ownersOf(file, packages) {
93 if (INERT.some((pattern) => pattern.test(file))) return [];
94 // A manifest can change dependencies or versions other packages read.
95 if (path.posix.basename(file) === "package.json") return null;
96 if (RUNS_EVERYTHING.some((pattern) => pattern.test(file))) return null;
97 const extra = EXTRA_OWNERS.flatMap(([pattern, names]) => (pattern.test(file) ? names : []));
98 // scripts/build-desktop-artifact.test.ts reads the crates' sources.
99 if (RUST.test(file)) return ["@t3tools/scripts", ...extra];
100
101 const owner = packages
102 .filter((pkg) => file.startsWith(`${pkg.dir}/`))
103 .sort((a, b) => b.dir.length - a.dir.length)[0];
104 if (owner) return [owner.name, ...extra];
105 return extra.length > 0 ? extra : null;
106}
107
108function decide(files, packages, full) {
109 const all = new Set(packages.map((pkg) => pkg.name));
110 let affected = new Set();
111 let everything = full || files === null;
112 let rust = false;
113
114 for (const file of files ?? []) {
115 if (RUST.test(file)) rust = true;
116 const owners = ownersOf(file, packages);
117 if (owners === null) {
118 everything = true;
119 console.error(`Runs everything: ${file}`);
120 } else {
121 for (const owner of owners) affected.add(owner);
122 }
123 }
124
125 // Release smoke reads scripts/ itself, not what the scripts tests read.
126 let scriptsChanged = everything;
127 if (everything) {
128 affected = all;
129 } else {
130 let grew = true;
131 while (grew) {
132 grew = false;
133 for (const pkg of packages) {
134 if (!affected.has(pkg.name) && pkg.deps.some((dep) => affected.has(dep))) {
135 affected.add(pkg.name);
136 grew = true;
137 }
138 }
139 }
140 scriptsChanged = affected.has("@t3tools/scripts");
141 for (const [name, deps] of Object.entries(TEST_ONLY_DEPS)) {
142 if (deps.some((dep) => affected.has(dep))) affected.add(name);
143 }
144 }
145
146 const tested = packages.filter(
147 (pkg) => pkg.hasTest && !OWN_TEST_JOB.has(pkg.name) && affected.has(pkg.name),
148 );
149 const has = (name) => affected.has(name);
150
151 return {
152 affected: [...affected].sort(),
153 outputs: {
154 typecheck: affected.size > 0,
155 build: has("@t3tools/desktop") || has("t3") || has("@t3tools/web"),
156 test: tested.length > 0,
157 // Empty means every package the Test job covers.
158 test_packages: everything ? "" : tested.map((pkg) => pkg.name).join(" "),
159 test_web: has("@t3tools/web"),
160 test_server: has("t3"),
161 rust: everything || rust,
162 release_smoke: scriptsChanged,
163 mobile_native: files === null || files.some((file) => MOBILE_NATIVE.test(file)),
164 },
165 };
166}
167
168function main() {
169 const args = process.argv.slice(2);
170 const pathsIndex = args.indexOf("--paths");
171 const files =
172 pathsIndex === -1
173 ? null
174 : fs
175 .readFileSync(args[pathsIndex + 1], "utf8")
176 .split("\n")
177 .filter(Boolean);
178 const { affected, outputs } = decide(files, readWorkspace(), args.includes("--full"));
179
180 console.error(`Affected packages: ${affected.join(", ") || "none"}`);
181 for (const [key, value] of Object.entries(outputs)) console.log(`${key}=${value}`);
182}
183
184module.exports = { readWorkspace, decide };
185
186if (require.main === module) main();