Skip to content

Commit 76543c3

Browse files
ErwanRauloRaulo Erwan.
andauthored
feat: highlight all packages under scope (#715)
Co-authored-by: Raulo Erwan. <erwan.raulo.externe@emeria.eu>
1 parent 2355081 commit 76543c3

5 files changed

Lines changed: 75 additions & 2 deletions

File tree

.changeset/pump-it-up.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/scanner": minor
3+
---
4+
5+
Add possibility to highlight all packageas under a scope

workspaces/scanner/src/depWalker.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,14 @@ export async function depWalker(
386386
const semverRanges = parseSemverRange(options.highlight?.packages ?? {});
387387
for (const version of Object.entries(dependency.versions)) {
388388
const [verStr, verDescriptor] = version as [string, DependencyVersion];
389-
const range = semverRanges?.[packageName];
390-
if (range && semver.satisfies(verStr, range)) {
389+
const packageRange = semverRanges?.[packageName];
390+
const org = parseNpmSpec(packageName)?.org;
391+
const isScopeHighlighted = org !== null && `@${org}` in semverRanges;
392+
393+
if (
394+
(packageRange && semver.satisfies(verStr, packageRange)) ||
395+
isScopeHighlighted
396+
) {
391397
highlightedPackages.add(`${packageName}@${verStr}`);
392398
}
393399
verDescriptor.flags.push(

workspaces/scanner/src/utils/parseSemverRange.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ export function parseSemverRange(packages: HighlightPackages) {
2121

2222
function parseSpecs(specs: string[]) {
2323
return specs.reduce((acc, spec) => {
24+
// Handle scope-only entries like "@fastify", matching all packages under that scope
25+
if (/^@[^/@]+$/.test(spec)) {
26+
acc[spec] = ["*"];
27+
28+
return acc;
29+
}
30+
2431
const parsedSpec = parseNpmSpec(spec);
2532
if (!parsedSpec) {
2633
return acc;

workspaces/scanner/test/depWalker.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,48 @@ describe("depWalker", { concurrency: 2 }, () => {
316316
]
317317
);
318318
});
319+
320+
it("should highlight all packages of a scope using a semver range map", { skip }, async(t) => {
321+
const { logger } = buildLogger();
322+
t.after(() => logger.removeAllListeners());
323+
324+
const { highlighted } = await depWalker(
325+
new ManifestManager(config),
326+
structuredClone({
327+
...kDefaultWalkerOptions,
328+
highlight: {
329+
packages: { "@slimio": "*" },
330+
contacts: []
331+
}
332+
}),
333+
logger
334+
);
335+
336+
assert.ok(highlighted.packages.every((pkg) => pkg.startsWith("@slimio/")));
337+
assert.ok(highlighted.packages.some((pkg) => pkg.startsWith("@slimio/is")));
338+
assert.ok(highlighted.packages.some((pkg) => pkg.startsWith("@slimio/config")));
339+
});
340+
341+
it("should highlight all packages of a scope from an array of specs", { skip }, async(t) => {
342+
const { logger } = buildLogger();
343+
t.after(() => logger.removeAllListeners());
344+
345+
const { highlighted } = await depWalker(
346+
new ManifestManager(config),
347+
structuredClone({
348+
...kDefaultWalkerOptions,
349+
highlight: {
350+
packages: ["@slimio"],
351+
contacts: []
352+
}
353+
}),
354+
logger
355+
);
356+
357+
assert.ok(highlighted.packages.every((pkg) => pkg.startsWith("@slimio/")));
358+
assert.ok(highlighted.packages.some((pkg) => pkg.startsWith("@slimio/is")));
359+
assert.ok(highlighted.packages.some((pkg) => pkg.startsWith("@slimio/config")));
360+
});
319361
});
320362
});
321363

workspaces/scanner/test/utils/parseSemverRange.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,18 @@ describe("utils.parseSemverRange", () => {
5252
it("should not parse invalid specs", () => {
5353
assert.deepEqual(parseSemverRange([""]), {});
5454
});
55+
56+
it("should parse scope-only entries as wildcards", () => {
57+
assert.deepEqual(parseSemverRange(["@nodesecure"]), {
58+
"@nodesecure": "*"
59+
});
60+
});
61+
62+
it("should parse scope-only entries alongside regular specs", () => {
63+
assert.deepEqual(parseSemverRange(["@nodesecure", "foo@1.0.0"]), {
64+
"@nodesecure": "*",
65+
foo: "1.0.0"
66+
});
67+
});
5568
});
5669
});

0 commit comments

Comments
 (0)