diff --git a/.changeset/ninety-months-wait.md b/.changeset/ninety-months-wait.md new file mode 100644 index 00000000..ce6bfb78 --- /dev/null +++ b/.changeset/ninety-months-wait.md @@ -0,0 +1,5 @@ +--- +"@nodesecure/tarball": patch +--- + +fix file import detection and avoid confusion with package with dots diff --git a/workspaces/tarball/src/utils/analyzeDependencies.ts b/workspaces/tarball/src/utils/analyzeDependencies.ts index de267973..6b681e8c 100644 --- a/workspaces/tarball/src/utils/analyzeDependencies.ts +++ b/workspaces/tarball/src/utils/analyzeDependencies.ts @@ -1,6 +1,3 @@ -// Import Node.js Dependencies -import path from "node:path"; - // Import Third-party Dependencies import { ManifestManager } from "@nodesecure/mama"; import type { NodeImport } from "@nodesecure/npm-types"; @@ -56,6 +53,7 @@ export const NODE_BUILTINS = new Set([ "diagnostics_channel" ]); +const kFileExtensions = [".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".node", ".json"]; const kExternalModules = new Set(["http", "https", "net", "http2", "dgram", "child_process"]); export interface analyzeDependenciesOptions { @@ -114,6 +112,10 @@ export function analyzeDependencies( .filter((name: string) => !(name in nodejsImports) && !thirdPartyDependenciesAliased.has(name)); const nodeDependencies = sourceDependencies.filter((name) => isCoreModule(name)); + const hasMissingOrUnusedDependency = + unusedDependencies.length > 0 || + missingDependencies.length > 0; + return { nodeDependencies, thirdPartyDependencies: [...new Set(thirdPartyDependencies)], @@ -123,7 +125,7 @@ export function analyzeDependencies( flags: { hasExternalCapacity: nodeDependencies.some((depName) => kExternalModules.has(depName)), - hasMissingOrUnusedDependency: unusedDependencies.length > 0 || missingDependencies.length > 0 + hasMissingOrUnusedDependency } }; } @@ -133,9 +135,10 @@ function difference(arr1: T[], arr2: T[]): T[] { } function isFile( - name: string + filePath: string ) { - return name.startsWith(".") || path.extname(name) !== ""; + return filePath.startsWith(".") + || kFileExtensions.some((extension) => filePath.endsWith(extension)); } function isCoreModule( diff --git a/workspaces/tarball/test/utils/analyzeDependencies.spec.ts b/workspaces/tarball/test/utils/analyzeDependencies.spec.ts index 2cc30ae5..5d148cd2 100644 --- a/workspaces/tarball/test/utils/analyzeDependencies.spec.ts +++ b/workspaces/tarball/test/utils/analyzeDependencies.spec.ts @@ -26,6 +26,27 @@ test("analyzeDependencies should detect Node.js dependencies and also flag hasEx }); }); +test("analyzeDependencies should detect no unused or missing dependencies and avoid confusion for package name with dots", () => { + const mama = { + dependencies: ["lodash.isequal"], + devDependencies: [] + }; + + const result = analyzeDependencies(["lodash.isequal"], { + mama, + tryDependencies: new Set() + }); + + assert.deepEqual(result, { + nodeDependencies: [], + thirdPartyDependencies: ["lodash.isequal"], + subpathImportsDependencies: {}, + unusedDependencies: [], + missingDependencies: [], + flags: { hasExternalCapacity: false, hasMissingOrUnusedDependency: false } + }); +}); + test("analyzeDependencies should detect prefixed (namespaced 'node:') core dependencies", () => { const mama = { dependencies: ["node:foo"],