diff --git a/.changeset/shy-dingos-grin.md b/.changeset/shy-dingos-grin.md new file mode 100644 index 00000000..6fa94583 --- /dev/null +++ b/.changeset/shy-dingos-grin.md @@ -0,0 +1,5 @@ +--- +"@nodesecure/mama": minor +--- + +Added lockfile scanning utils diff --git a/workspaces/mama/src/index.ts b/workspaces/mama/src/index.ts index 1dabb0ab..750ff967 100644 --- a/workspaces/mama/src/index.ts +++ b/workspaces/mama/src/index.ts @@ -3,5 +3,7 @@ export { packageJSONIntegrityHash, parseNpmSpec, inspectModuleType, + scanLockFiles, + LOCK_FILES, type PackageModuleType } from "./utils/index.ts"; diff --git a/workspaces/mama/src/utils/index.ts b/workspaces/mama/src/utils/index.ts index d081be93..3f2648ff 100644 --- a/workspaces/mama/src/utils/index.ts +++ b/workspaces/mama/src/utils/index.ts @@ -1,3 +1,4 @@ export * from "./integrity-hash.ts"; export * from "./inspectModuleType.ts"; export * from "./parseNpmSpec.ts"; +export * from "./scan-lockfiles.ts"; diff --git a/workspaces/mama/src/utils/scan-lockfiles.ts b/workspaces/mama/src/utils/scan-lockfiles.ts new file mode 100644 index 00000000..98380fb8 --- /dev/null +++ b/workspaces/mama/src/utils/scan-lockfiles.ts @@ -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; } = {}; + for (const [k, v] of Object.entries(LOCK_FILES)) { + const filePath = path.join(dirPath, v); + if (fs.existsSync(filePath)) { + result[k] = filePath; + } + } + + const isEmpty = Object.keys(result).length === 0; + + return isEmpty ? null : result; +} diff --git a/workspaces/mama/test/scan-lockfiles.spec.ts b/workspaces/mama/test/scan-lockfiles.spec.ts new file mode 100644 index 00000000..29a5e882 --- /dev/null +++ b/workspaces/mama/test/scan-lockfiles.spec.ts @@ -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); + }); +});