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