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/ninety-months-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/tarball": patch
---

fix file import detection and avoid confusion with package with dots
15 changes: 9 additions & 6 deletions workspaces/tarball/src/utils/analyzeDependencies.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)],
Expand All @@ -123,7 +125,7 @@ export function analyzeDependencies(

flags: {
hasExternalCapacity: nodeDependencies.some((depName) => kExternalModules.has(depName)),
hasMissingOrUnusedDependency: unusedDependencies.length > 0 || missingDependencies.length > 0
hasMissingOrUnusedDependency
}
};
}
Expand All @@ -133,9 +135,10 @@ function difference<T>(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(
Expand Down
21 changes: 21 additions & 0 deletions workspaces/tarball/test/utils/analyzeDependencies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down