Skip to content

Commit

Permalink
feat(groups): handle negation for packages option
Browse files Browse the repository at this point in the history
Closes #232
  • Loading branch information
ardelato authored and JamieMason committed Jul 24, 2024
1 parent 15a5184 commit 0d6b608
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 7 additions & 0 deletions site/src/partials/group-config/_packages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import OptionalPill from '@site/components/pill/optional.astro';

- An array of strings which should match the `name` properties of your package.json files.
- If omitted, the default behaviour is to match every package.
- Negated types are also supported, so a value of `["!my-client", "!my-server"]` would assign everything **except** the packages `my-client` and `my-server` to this group.
- The strings can be any combination of exact matches or [minimatch](https://github.com/isaacs/minimatch) glob patterns:

```json title="Examples of valid values"
Expand All @@ -16,6 +17,12 @@ packages: ["@my-repo/**"]
// ✅ match specific packages by name
packages: ["my-server", "my-client"]

// ✅ match all packages except negated ones
packages: ["!my-server", "!@my-repo/**]

// ❌ no mixing of specific and negated packages
packages: ["my-client", "!@my-repo/**"]

// ❌ not file system paths, name properties of package.json files
packages: ["packages/my-client"]

Expand Down
12 changes: 7 additions & 5 deletions src/guards/can-add-to-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ function matchesDependencies(
}

function matchesPackages(packages: unknown, instance: Instance) {
// matches if not defined
if (!isNonEmptyArray(packages)) return true;
return packages.some((pattern) => minimatch(instance.pkgName, pattern));
return matchesKnownList(packages, instance.pkgName);
}

function matchesDependencyTypes(dependencyTypes: unknown, instance: Instance): boolean {
Expand All @@ -65,6 +63,10 @@ function matchesKnownList(values: unknown, value: string): boolean {
positive.push(name);
}
});
if (isNonEmptyArray(negative) && !negative.includes(value)) return true;
return isNonEmptyArray(positive) && positive.includes(value);
if (isNonEmptyArray(negative) && !someMinimatch(value, negative)) return true;
return isNonEmptyArray(positive) && someMinimatch(value, positive);
}

function someMinimatch(value: string, patterns: string[]): boolean {
return patterns.some((pattern) => minimatch(value, pattern))
}

0 comments on commit 0d6b608

Please sign in to comment.