Skip to content
Open
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/shy-dingos-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/mama": minor
---

Added lockfile scanning utils
2 changes: 2 additions & 0 deletions workspaces/mama/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ export {
packageJSONIntegrityHash,
parseNpmSpec,
inspectModuleType,
scanLockFiles,
LOCK_FILES,
type PackageModuleType
} from "./utils/index.ts";
1 change: 1 addition & 0 deletions workspaces/mama/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./integrity-hash.ts";
export * from "./inspectModuleType.ts";
export * from "./parseNpmSpec.ts";
export * from "./scan-lockfiles.ts";
24 changes: 24 additions & 0 deletions workspaces/mama/src/utils/scan-lockfiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Import Node.js Dependencies
import fs from "node:fs";
import path from "node:path";

export const LOCK_FILES = {
npm: "package-lock.json",
bun: "bun.lockb",
yarn: "yarn.lock",
pnpm: "pnpm-lock.yaml"
};

export function scanLockFiles(dirPath: string): null | object {
const result: { [k: string]: string; } = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would work with an Array from the start to avoid useless lookup, at the end you can use Object.fromEntries

for (const [k, v] of Object.entries(LOCK_FILES)) {
const filePath = path.join(dirPath, v);
if (fs.existsSync(filePath)) {
result[k] = filePath;
}
Comment on lines +16 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably better to use fs.readdirSync instead of doing 1..N I/O fs (N being the number of elements in LOCK_FILES)

}

const isEmpty = Object.keys(result).length === 0;

return isEmpty ? null : result;
}
30 changes: 30 additions & 0 deletions workspaces/mama/test/scan-lockfiles.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Import Node.js Dependencies
import { describe, test } from "node:test";
import assert from "node:assert";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

// Import Internal Dependencies
import { scanLockFiles, LOCK_FILES } from "../src/index.ts";

describe("scanLockFiles", () => {
test("should scan lock files", () => {
const output: typeof LOCK_FILES = {} as typeof LOCK_FILES;
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "/"));

for (const [k, v] of Object.entries(LOCK_FILES)) {
const filepath = path.join(tmpDir, v);

fs.writeFileSync(filepath, "");
output[k as keyof typeof LOCK_FILES] = filepath;
}

assert.deepEqual(scanLockFiles(tmpDir), output);
});

test("should return null no lockfiles", () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "/"));
assert.deepEqual(scanLockFiles(tmpDir), null);
});
});