diff --git a/workspaces/scanner/src/depWalker.ts b/workspaces/scanner/src/depWalker.ts index 630d05f..6cd5a1b 100644 --- a/workspaces/scanner/src/depWalker.ts +++ b/workspaces/scanner/src/depWalker.ts @@ -13,7 +13,7 @@ import type { ManifestVersion, PackageJSON } from "@nodesecure/npm-types"; // Import Internal Dependencies import { - getDependenciesWarnings, addMissingVersionFlags + getDependenciesWarnings, addMissingVersionFlags, getUsedDeps } from "./utils/index.js"; import { packageMetadata, manifestMetadata } from "./npmRegistry.js"; import { Logger, ScannerLoggerEvents } from "./class/logger.class.js"; @@ -233,7 +233,7 @@ export async function depWalker( } const usedBy: Record = Object.create(null); - for (const [name, version] of [...usedDeps].map((name) => name.split("@"))) { + for (const [name, version] of getUsedDeps(usedDeps)) { usedBy[name] = version; } Object.assign(verDescriptor.usedBy, usedBy); diff --git a/workspaces/scanner/src/utils/getUsedDeps.ts b/workspaces/scanner/src/utils/getUsedDeps.ts new file mode 100644 index 0000000..ee6dcc7 --- /dev/null +++ b/workspaces/scanner/src/utils/getUsedDeps.ts @@ -0,0 +1,12 @@ +export function getUsedDeps(deps: Set<`${string}@${string}`>) { + return [...deps].map((name) => { + const isScoped = name.startsWith("@"); + if (isScoped) { + const [nameChunk, version] = name.slice(1).split("@"); + + return [`@${nameChunk}`, version]; + } + + return name.split("@"); + }); +} diff --git a/workspaces/scanner/src/utils/index.ts b/workspaces/scanner/src/utils/index.ts index 995a353..6e72013 100644 --- a/workspaces/scanner/src/utils/index.ts +++ b/workspaces/scanner/src/utils/index.ts @@ -3,6 +3,7 @@ export * from "./warnings.js"; export * from "./addMissingVersionFlags.js"; export * from "./getLinks.js"; export * from "./urlToString.js"; +export * from "./getUsedDeps.js"; export const NPM_TOKEN = typeof process.env.NODE_SECURE_TOKEN === "string" ? { token: process.env.NODE_SECURE_TOKEN } : diff --git a/workspaces/scanner/test/depWalker.spec.ts b/workspaces/scanner/test/depWalker.spec.ts index b71eb58..0283f09 100644 --- a/workspaces/scanner/test/depWalker.spec.ts +++ b/workspaces/scanner/test/depWalker.spec.ts @@ -92,6 +92,12 @@ test("execute depWalker on @slimio/config", async() => { "@iarna/toml", "@slimio/config" ].sort()); + + const ajvDescriptor = resultAsJSON.ajv.versions["6.12.6"]; + const ajvUsedBy = Object.keys(ajvDescriptor.usedBy); + assert.deepEqual(ajvUsedBy, [ + "@slimio/config" + ]); }); test("execute depWalker on pkg.gitdeps", async() => { diff --git a/workspaces/scanner/test/utils/getUsedDeps.spec.ts b/workspaces/scanner/test/utils/getUsedDeps.spec.ts new file mode 100644 index 0000000..c4752f0 --- /dev/null +++ b/workspaces/scanner/test/utils/getUsedDeps.spec.ts @@ -0,0 +1,22 @@ +// Import Node.js Dependencies +import { test } from "node:test"; +import assert from "node:assert"; + +// Import Internal Dependencies +import { getUsedDeps } from "../../src/utils/index.js"; + +test("getUsedDeps should handle scoped packages", () => { + const deps = getUsedDeps(new Set([ + "@slimio/is@latest" + ])); + + assert.deepStrictEqual(deps, [["@slimio/is", "latest"]]); +}); + +test("getUsedDeps should handle non-scoped packages", () => { + const deps = getUsedDeps(new Set([ + "is@latest" + ])); + + assert.deepStrictEqual(deps, [["is", "latest"]]); +});