Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"globals": "^16.5.0",
"js-yaml": "^5.2.2",
"knip": "^6.23.0",
"minimatch": "^10.0.0",
"nx": "^22.0.0",
"oxfmt": "^0.59.0",
"oxlint": "^1.51.0",
Expand Down
25 changes: 18 additions & 7 deletions scripts/affected.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getBaseCommit, getChangedFiles, git } from "@rnx-kit/tools-git";
import * as yaml from "js-yaml";
import { Minimatch } from "minimatch";
import * as fs from "node:fs";
import * as path from "node:path";

type MatchChangedFiles = { "any-glob-to-any-file": string[] };
type Match = { "changed-files": MatchChangedFiles[] };
Expand All @@ -21,12 +21,23 @@ function loadLabels(): Record<string, Match[] | undefined> {
return yaml.load(yml) as Record<string, Match[] | undefined>;
}

/**
* Makes a glob pattern match dotfiles, emulating minimatch's `{ dot: true }`.
*
* `path.matchesGlob` won't match a path segment that starts with a dot against
* `*`/`**`, so we insert an optional dot before every wildcard segment (a `*`
* right after a `/`, or at the very start of the pattern).
*/
function dotAware(pattern: string): string {
const withDot = pattern.replaceAll("/*", "/{.,}*");
return withDot.startsWith("*") ? `{.,}${withDot}` : withDot;
}

/**
* Makes platform specific file path matchers.
*/
function makeMatchers(): Record<string, Minimatch[]> {
const matchers: Record<string, Minimatch[]> = {};
const options = { dot: true };
function makeMatchers(): Record<string, string[]> {
const matchers: Record<string, string[]> = {};
const labels = loadLabels();

for (const [label, match] of Object.entries(labels)) {
Expand All @@ -36,7 +47,7 @@ function makeMatchers(): Record<string, Minimatch[]> {

const patterns = match[0]["changed-files"][0]["any-glob-to-any-file"];
const platform = label.split(": ")[1];
matchers[platform] = patterns.map((m) => new Minimatch(m, options));
matchers[platform] = patterns.map(dotAware);
}

return matchers;
Expand Down Expand Up @@ -70,8 +81,8 @@ function getAffectedPlatforms(targetBranch: string | undefined): string[] {
}

const affectedPlatforms = new Set<string>();
for (const [platform, matchers] of Object.entries(platformMatchers)) {
if (matchers.some((m) => changedFiles.some((f) => m.match(f)))) {
for (const [platform, patterns] of Object.entries(platformMatchers)) {
if (patterns.some((p) => changedFiles.some((f) => path.matchesGlob(f, p)))) {
affectedPlatforms.add(platform);
}
}
Expand Down
3 changes: 1 addition & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2508,7 +2508,6 @@ __metadata:
globals: "npm:^16.5.0"
js-yaml: "npm:^5.2.2"
knip: "npm:^6.23.0"
minimatch: "npm:^10.0.0"
nx: "npm:^22.0.0"
oxfmt: "npm:^0.59.0"
oxlint: "npm:^1.51.0"
Expand Down Expand Up @@ -11675,7 +11674,7 @@ __metadata:
languageName: node
linkType: hard

"minimatch@npm:^10.0.0, minimatch@npm:^10.0.3, minimatch@npm:^10.1.1, minimatch@npm:^10.2.2, minimatch@npm:^10.2.5":
"minimatch@npm:^10.0.3, minimatch@npm:^10.1.1, minimatch@npm:^10.2.2, minimatch@npm:^10.2.5":
version: 10.2.5
resolution: "minimatch@npm:10.2.5"
dependencies:
Expand Down