-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaffected-apps.js
More file actions
202 lines (184 loc) · 5.54 KB
/
affected-apps.js
File metadata and controls
202 lines (184 loc) · 5.54 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
#!/usr/bin/env node
/**
* Auto "affected apps" → Playwright tags for non-Nx monorepos.
* - Scans apps/* and libs/* for package.json (workspace packages)
* - Builds full reverse dependency graph (handles lib→lib→app chains)
* - Detects changed packages via `git diff --name-only <BASE_REF>...HEAD`
* - Emits JSON array of tags: ["@app:web","@app:admin"]
*
* Env:
* BASE_REF (default: origin/main)
*
* Assumptions:
* - Apps live under apps/* and contain an e2e/ folder
* - Internal libs live under libs/* (can depend on each other)
* - package.json "name" is present for each internal package
*/
import fs from "fs";
import path from "path";
import { execSync } from "child_process";
const BASE_REF = process.env.BASE_REF || "origin/main";
const ROOT = process.cwd();
// ---------- utils ----------
const readJson = (p) => {
try {
return JSON.parse(fs.readFileSync(p, "utf8"));
} catch {
return null;
}
};
const isDir = (p) => {
try {
return fs.statSync(p).isDirectory();
} catch {
return false;
}
};
const listDirs = (p) =>
isDir(p)
? fs
.readdirSync(p)
.map((n) => path.join(p, n))
.filter(isDir)
: [];
// ---------- discover workspace packages in apps/* and libs/* ----------
function discoverPackages() {
const groups = ["apps", "libs"];
const pkgs = [];
for (const g of groups) {
const base = path.join(ROOT, g);
for (const dir of listDirs(base)) {
const pkgJson = path.join(dir, "package.json");
const pkg = readJson(pkgJson);
if (!pkg) continue;
const relDir = path.relative(ROOT, dir).replace(/\\/g, "/");
const isApp = relDir.startsWith("apps/") && isDir(path.join(dir, "e2e"));
const deps = {
...pkg.dependencies,
...pkg.devDependencies,
...pkg.peerDependencies,
...pkg.optionalDependencies,
};
pkgs.push({
name: pkg.name, // required to link deps
relDir, // e.g. "libs/ui"
isApp, // apps/* with e2e/
slug: path.basename(dir), // "web", "admin"
deps: Object.keys(deps || {}),
});
}
}
// Index by name
const byName = new Map();
for (const p of pkgs) {
if (p.name) byName.set(p.name, p);
}
return { pkgs, byName };
}
// ---------- build reverse dependency graph (internal only) ----------
function buildReverseGraph(pkgs) {
const internalNames = new Set(pkgs.map((p) => p.name).filter(Boolean));
const reverse = new Map(); // key: internal pkg name -> Set(dependent pkg names)
for (const name of internalNames) reverse.set(name, new Set());
for (const p of pkgs) {
for (const d of p.deps) {
if (internalNames.has(d) && p.name) {
reverse.get(d).add(p.name); // d <- p
}
}
}
return { internalNames, reverse };
}
// ---------- map changed files -> changed package names ----------
function changedFiles(baseRef) {
try {
const out = execSync(`git diff --name-only ${baseRef}...HEAD`, { stdio: "pipe" })
.toString()
.split("\n")
.map((s) => s.trim())
.filter(Boolean);
return out;
} catch {
// Fallback: everything since initial commit
const out = execSync(`git diff --name-only $(git rev-list --max-parents=0 HEAD)`, {
stdio: "pipe",
})
.toString()
.split("\n")
.map((s) => s.trim())
.filter(Boolean);
return out;
}
}
function changedPackageNames(files, pkgs) {
const hits = new Set();
for (const f of files) {
for (const p of pkgs) {
if (f.startsWith(p.relDir + "/")) {
if (p.name) hits.add(p.name);
}
}
}
return hits;
}
// ---------- detect root-wide changes that should affect all apps ----------
function isRootWideChange(files) {
const patterns = [
/^playwright\.config\.(ts|js|mjs)$/,
/^package\.json$/,
/^pnpm-lock\.yaml$/,
/^yarn\.lock$/,
/^package-lock\.json$/,
/^\.github\//,
/^dockerfile$/i,
];
return files.some((f) => patterns.some((rx) => rx.test(f)));
}
// ---------- BFS over reverse graph to find all dependents (apps included) ----------
function collectDependents(changedNames, reverse) {
const out = new Set(changedNames);
const queue = [...changedNames];
while (queue.length) {
const n = queue.shift();
const dependents = reverse.get(n);
if (!dependents) continue;
for (const m of dependents) {
if (!out.has(m)) {
out.add(m);
queue.push(m);
}
}
}
return out; // set of pkg names
}
// ---------- main ----------
(function main() {
const { pkgs, byName } = discoverPackages();
const apps = pkgs.filter((p) => p.isApp);
const files = changedFiles(BASE_REF);
if (isRootWideChange(files)) {
// Affect all apps
const tags = apps.map((a) => `@app:${a.slug}`);
process.stdout.write(JSON.stringify(tags.sort()) + "\n");
return;
}
// Seed = packages whose directories changed
const changedPkgs = changedPackageNames(files, pkgs);
// Build reverse graph and collect dependents
const { reverse } = buildReverseGraph(pkgs);
const impacted = collectDependents(changedPkgs, reverse);
// Output only app tags
const affectedAppSlugs = new Set();
for (const name of impacted) {
const pkg = byName.get(name);
if (pkg && pkg.isApp) affectedAppSlugs.add(pkg.slug);
}
// Also: direct app dir changes where app lacks a "name" (rare)
for (const f of files) {
for (const a of apps) {
if (f.startsWith(a.relDir + "/")) affectedAppSlugs.add(a.slug);
}
}
const tags = [...affectedAppSlugs].sort().map((s) => `@app:${s}`);
process.stdout.write(JSON.stringify(tags) + "\n");
})();