diff --git a/.changeset/hip-cases-learn.md b/.changeset/hip-cases-learn.md new file mode 100644 index 00000000..804d15e1 --- /dev/null +++ b/.changeset/hip-cases-learn.md @@ -0,0 +1,5 @@ +--- +"@nodesecure/mama": patch +--- + +Refactor scanLockFiles diff --git a/workspaces/mama/README.md b/workspaces/mama/README.md index 1b48f9f9..1003967b 100644 --- a/workspaces/mama/README.md +++ b/workspaces/mama/README.md @@ -38,7 +38,7 @@ This package exports a set of standalone utilities that are internally used by t - [packageJSONIntegrityHash](./docs/packageJSONIntegrityHash.md) - [parseNpmSpec](./docs/parseNpmSpec.md) - [inspectModuleType](./docs/inspectModuleType.md) -- [scanLockFiles](./docs/scan-lockfiles.md) +- [scanLockFiles](./docs/scanLockFiles.md) ## ManifestManager API diff --git a/workspaces/mama/docs/scan-lockfiles.md b/workspaces/mama/docs/scan-lockfiles.md deleted file mode 100644 index f03b56d0..00000000 --- a/workspaces/mama/docs/scan-lockfiles.md +++ /dev/null @@ -1,13 +0,0 @@ -# scanLockFiles - -Scans for lock files in the given path. - -This function searches for common package manager lock files -such as package-lock.json, yarn.lock, pnpm-lock.yaml, etc. -returns an Object of found lock file with their paths - -## Function Signature - -```ts -export function scanLockFiles(dirPath: string): null | Record; -``` diff --git a/workspaces/mama/docs/scanLockFiles.md b/workspaces/mama/docs/scanLockFiles.md new file mode 100644 index 00000000..cc19cdae --- /dev/null +++ b/workspaces/mama/docs/scanLockFiles.md @@ -0,0 +1,24 @@ +# scanLockFiles + +Scans for lock files in the given path. + +This function searches for common package manager lock files +such as package-lock.json, yarn.lock, pnpm-lock.yaml, etc. +returns an Object of found lock file with their paths + +## Function Signature + +```ts +export const LOCK_FILES = { + npm: "package-lock.json", + bun: "bun.lockb", + pnpm: "pnpm-lock.yaml", + yarn: "yarn.lock", + deno: "deno.lock" +} as const; + +export type LockFileProvider = keyof typeof LOCK_FILES; +export type LockFileName = typeof LOCK_FILES[LockFileProvider]; + +export function scanLockFiles(dirPath: string): Partial>; +``` diff --git a/workspaces/mama/src/utils/index.ts b/workspaces/mama/src/utils/index.ts index 3f2648ff..9b176a77 100644 --- a/workspaces/mama/src/utils/index.ts +++ b/workspaces/mama/src/utils/index.ts @@ -1,4 +1,4 @@ export * from "./integrity-hash.ts"; export * from "./inspectModuleType.ts"; export * from "./parseNpmSpec.ts"; -export * from "./scan-lockfiles.ts"; +export * from "./scanLockFiles.ts"; diff --git a/workspaces/mama/src/utils/scan-lockfiles.ts b/workspaces/mama/src/utils/scan-lockfiles.ts deleted file mode 100644 index 974cbcb2..00000000 --- a/workspaces/mama/src/utils/scan-lockfiles.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Import Node.js Dependencies -import fs from "node:fs"; - -export const LOCK_FILES = [ - ["npm", "package-lock.json"], - ["bun", "bun.lockb"], - ["pnpm", "pnpm-lock.yaml"], - ["yarn", "yarn.lock"], - ["deno", "deno.lock"] -]; - -export function scanLockFiles(dirPath: string): null | Record { - const dir = fs.readdirSync(dirPath); - if (dir.length === 0) { - return null; - } - - const filteredEntries = Array.from(LOCK_FILES).flatMap(([k, v]) => (dir.includes(v) ? [[k, v]] : []) - ); - - return filteredEntries.length === 0 - ? null - : Object.fromEntries(filteredEntries); -} diff --git a/workspaces/mama/src/utils/scanLockFiles.ts b/workspaces/mama/src/utils/scanLockFiles.ts new file mode 100644 index 00000000..14aad8bd --- /dev/null +++ b/workspaces/mama/src/utils/scanLockFiles.ts @@ -0,0 +1,28 @@ +// Import Node.js Dependencies +import fs from "node:fs"; + +export const LOCK_FILES = { + npm: "package-lock.json", + bun: "bun.lockb", + pnpm: "pnpm-lock.yaml", + yarn: "yarn.lock", + deno: "deno.lock" +} as const; + +export type LockFileProvider = keyof typeof LOCK_FILES; +export type LockFileName = typeof LOCK_FILES[LockFileProvider]; + +export function scanLockFiles( + dirPath: string +): Partial> { + const dir = fs.readdirSync(dirPath); + if (dir.length === 0) { + return {}; + } + + const filteredEntries = Object + .entries(LOCK_FILES) + .flatMap(([providerName, fileName]) => (dir.includes(fileName) ? [[providerName, fileName]] : [])); + + return Object.fromEntries(filteredEntries); +} diff --git a/workspaces/mama/test/scan-lockfiles.spec.ts b/workspaces/mama/test/scan-lockfiles.spec.ts deleted file mode 100644 index 4b96513b..00000000 --- a/workspaces/mama/test/scan-lockfiles.spec.ts +++ /dev/null @@ -1,30 +0,0 @@ -// 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: [string, string][] = []; - const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "/")); - - for (const [k, v] of LOCK_FILES) { - const filepath = path.join(tmpDir, v); - - fs.writeFileSync(filepath, ""); - output.push([k, v]); - } - - assert.deepEqual(scanLockFiles(tmpDir), Object.fromEntries(output)); - }); - - test("should return null no lockfiles", () => { - const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "/")); - assert.deepEqual(scanLockFiles(tmpDir), null); - }); -}); diff --git a/workspaces/mama/test/scanLockFiles.spec.ts b/workspaces/mama/test/scanLockFiles.spec.ts new file mode 100644 index 00000000..8ac34968 --- /dev/null +++ b/workspaces/mama/test/scanLockFiles.spec.ts @@ -0,0 +1,42 @@ +// Import Node.js Dependencies +import { describe, test, beforeEach, afterEach } 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", () => { + let tmpDir: string; + + beforeEach(() => { + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "/")); + }); + + afterEach(() => { + fs.rmSync(tmpDir, { recursive: true, force: true }); + }); + + test("should scan lock files", () => { + const output = Array.from(createFile(tmpDir)); + assert.deepEqual( + scanLockFiles(tmpDir), + Object.fromEntries(output) + ); + }); + + test("should return null no lockfiles", () => { + assert.deepEqual(scanLockFiles(tmpDir), {}); + }); +}); + +function* createFile(tempDir: string): IterableIterator<[string, string]> { + for (const [providerName, fileName] of Object.entries(LOCK_FILES)) { + const filepath = path.join(tempDir, fileName); + + fs.writeFileSync(filepath, ""); + yield [providerName, fileName]; + } +}