Skip to content
Merged
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
23 changes: 13 additions & 10 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 @@ -24,9 +24,8 @@ function loadLabels(): Record<string, Match[] | undefined> {
/**
* 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 +35,11 @@ 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((pattern) =>
// Modify pattern to match filenames starting with a period (but ignore
// patterns that start with `*`)
pattern.replaceAll("/*", "/{.,}*")
);
}

return matchers;
Expand All @@ -54,24 +57,24 @@ function getAffectedPlatforms(targetBranch: string | undefined): string[] {
return clean(Object.keys(platformMatchers));
}

const changedFiles = getChangedFiles(baseCommit);
if (changedFiles.length === 0) {
const files = getChangedFiles(baseCommit);
if (files.length === 0) {
// If there are no files, we are building default branch
return clean(Object.keys(platformMatchers));
}

// All platforms are affected if `react-native` related packages are changed
const lockfile = "yarn.lock";
if (changedFiles.includes(lockfile)) {
if (files.includes(lockfile)) {
const diff = git("diff", baseCommit, lockfile);
if (diff.includes("react-native")) {
return clean(Object.keys(platformMatchers));
}
}

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((m) => files.some((f) => path.matchesGlob(f, m)))) {
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 @@ -11676,7 +11675,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
Loading