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
5 changes: 5 additions & 0 deletions .changeset/pump-it-up.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/scanner": minor
---

Add possibility to highlight all packageas under a scope
10 changes: 8 additions & 2 deletions workspaces/scanner/src/depWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,14 @@ export async function depWalker(
const semverRanges = parseSemverRange(options.highlight?.packages ?? {});
for (const version of Object.entries(dependency.versions)) {
const [verStr, verDescriptor] = version as [string, DependencyVersion];
const range = semverRanges?.[packageName];
if (range && semver.satisfies(verStr, range)) {
const packageRange = semverRanges?.[packageName];
const org = parseNpmSpec(packageName)?.org;
const isScopeHighlighted = org !== null && `@${org}` in semverRanges;

if (
(packageRange && semver.satisfies(verStr, packageRange)) ||
isScopeHighlighted
) {
highlightedPackages.add(`${packageName}@${verStr}`);
}
verDescriptor.flags.push(
Expand Down
7 changes: 7 additions & 0 deletions workspaces/scanner/src/utils/parseSemverRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export function parseSemverRange(packages: HighlightPackages) {

function parseSpecs(specs: string[]) {
return specs.reduce((acc, spec) => {
// Handle scope-only entries like "@fastify", matching all packages under that scope
if (/^@[^/@]+$/.test(spec)) {
acc[spec] = ["*"];

return acc;
}

const parsedSpec = parseNpmSpec(spec);
if (!parsedSpec) {
return acc;
Expand Down
42 changes: 42 additions & 0 deletions workspaces/scanner/test/depWalker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,48 @@ describe("depWalker", { concurrency: 2 }, () => {
]
);
});

it("should highlight all packages of a scope using a semver range map", { skip }, async(t) => {
const { logger } = buildLogger();
t.after(() => logger.removeAllListeners());

const { highlighted } = await depWalker(
new ManifestManager(config),
structuredClone({
...kDefaultWalkerOptions,
highlight: {
packages: { "@slimio": "*" },
contacts: []
}
}),
logger
);

assert.ok(highlighted.packages.every((pkg) => pkg.startsWith("@slimio/")));
assert.ok(highlighted.packages.some((pkg) => pkg.startsWith("@slimio/is")));
assert.ok(highlighted.packages.some((pkg) => pkg.startsWith("@slimio/config")));
});

it("should highlight all packages of a scope from an array of specs", { skip }, async(t) => {
const { logger } = buildLogger();
t.after(() => logger.removeAllListeners());

const { highlighted } = await depWalker(
new ManifestManager(config),
structuredClone({
...kDefaultWalkerOptions,
highlight: {
packages: ["@slimio"],
contacts: []
}
}),
logger
);

assert.ok(highlighted.packages.every((pkg) => pkg.startsWith("@slimio/")));
assert.ok(highlighted.packages.some((pkg) => pkg.startsWith("@slimio/is")));
assert.ok(highlighted.packages.some((pkg) => pkg.startsWith("@slimio/config")));
});
});
});

Expand Down
13 changes: 13 additions & 0 deletions workspaces/scanner/test/utils/parseSemverRange.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,18 @@ describe("utils.parseSemverRange", () => {
it("should not parse invalid specs", () => {
assert.deepEqual(parseSemverRange([""]), {});
});

it("should parse scope-only entries as wildcards", () => {
assert.deepEqual(parseSemverRange(["@nodesecure"]), {
"@nodesecure": "*"
});
});

it("should parse scope-only entries alongside regular specs", () => {
assert.deepEqual(parseSemverRange(["@nodesecure", "foo@1.0.0"]), {
"@nodesecure": "*",
foo: "1.0.0"
});
});
});
});
Loading