Skip to content

Commit e5bc1be

Browse files
committed
refactor(mama): enhance scanLockFiles implementation
1 parent d6c08cb commit e5bc1be

File tree

9 files changed

+101
-69
lines changed

9 files changed

+101
-69
lines changed

.changeset/hip-cases-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/mama": patch
3+
---
4+
5+
Refactor scanLockFiles

workspaces/mama/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ This package exports a set of standalone utilities that are internally used by t
3838
- [packageJSONIntegrityHash](./docs/packageJSONIntegrityHash.md)
3939
- [parseNpmSpec](./docs/parseNpmSpec.md)
4040
- [inspectModuleType](./docs/inspectModuleType.md)
41-
- [scanLockFiles](./docs/scan-lockfiles.md)
41+
- [scanLockFiles](./docs/scanLockFiles.md)
4242

4343
## ManifestManager API
4444

workspaces/mama/docs/scan-lockfiles.md

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# scanLockFiles
2+
3+
Scans for lock files in the given path.
4+
5+
This function searches for common package manager lock files
6+
such as package-lock.json, yarn.lock, pnpm-lock.yaml, etc.
7+
returns an Object of found lock file with their paths
8+
9+
## Function Signature
10+
11+
```ts
12+
export const LOCK_FILES = {
13+
npm: "package-lock.json",
14+
bun: "bun.lockb",
15+
pnpm: "pnpm-lock.yaml",
16+
yarn: "yarn.lock",
17+
deno: "deno.lock"
18+
} as const;
19+
20+
export type LockFileProvider = keyof typeof LOCK_FILES;
21+
export type LockFileName = typeof LOCK_FILES[LockFileProvider];
22+
23+
export function scanLockFiles(dirPath: string): Partial<Record<LockFileProvider, LockFileName>>;
24+
```

workspaces/mama/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from "./integrity-hash.ts";
22
export * from "./inspectModuleType.ts";
33
export * from "./parseNpmSpec.ts";
4-
export * from "./scan-lockfiles.ts";
4+
export * from "./scanLockFiles.ts";

workspaces/mama/src/utils/scan-lockfiles.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Import Node.js Dependencies
2+
import fs from "node:fs";
3+
4+
export const LOCK_FILES = {
5+
npm: "package-lock.json",
6+
bun: "bun.lockb",
7+
pnpm: "pnpm-lock.yaml",
8+
yarn: "yarn.lock",
9+
deno: "deno.lock"
10+
} as const;
11+
12+
export type LockFileProvider = keyof typeof LOCK_FILES;
13+
export type LockFileName = typeof LOCK_FILES[LockFileProvider];
14+
15+
export function scanLockFiles(
16+
dirPath: string
17+
): Partial<Record<LockFileProvider, LockFileName>> {
18+
const dir = fs.readdirSync(dirPath);
19+
if (dir.length === 0) {
20+
return {};
21+
}
22+
23+
const filteredEntries = Object
24+
.entries(LOCK_FILES)
25+
.flatMap(([providerName, fileName]) => (dir.includes(fileName) ? [[providerName, fileName]] : []));
26+
27+
return Object.fromEntries(filteredEntries);
28+
}

workspaces/mama/test/scan-lockfiles.spec.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Import Node.js Dependencies
2+
import { describe, test, beforeEach, afterEach } from "node:test";
3+
import assert from "node:assert";
4+
import fs from "node:fs";
5+
import os from "node:os";
6+
import path from "node:path";
7+
8+
// Import Internal Dependencies
9+
import { scanLockFiles, LOCK_FILES } from "../src/index.ts";
10+
11+
describe("scanLockFiles", () => {
12+
let tmpDir: string;
13+
14+
beforeEach(() => {
15+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "/"));
16+
});
17+
18+
afterEach(() => {
19+
fs.rmSync(tmpDir, { recursive: true, force: true });
20+
});
21+
22+
test("should scan lock files", () => {
23+
const output = Array.from(createFile(tmpDir));
24+
assert.deepEqual(
25+
scanLockFiles(tmpDir),
26+
Object.fromEntries(output)
27+
);
28+
});
29+
30+
test("should return null no lockfiles", () => {
31+
assert.deepEqual(scanLockFiles(tmpDir), {});
32+
});
33+
});
34+
35+
function* createFile(tempDir: string): IterableIterator<[string, string]> {
36+
for (const [providerName, fileName] of Object.entries(LOCK_FILES)) {
37+
const filepath = path.join(tempDir, fileName);
38+
39+
fs.writeFileSync(filepath, "");
40+
yield [providerName, fileName];
41+
}
42+
}

0 commit comments

Comments
 (0)