fix(framework): handle deprecated Dirent path (#10179)

What:

- `Dirent` class from `NodeJS` has different properties in different versions, causing issues in our loaders.
- Util `readDirRecursive` was introduced, avoiding old node versions to break

FIXES: https://github.com/medusajs/medusa/issues/8419
This commit is contained in:
Carlos R. L. Rodrigues
2024-11-20 17:25:48 -03:00
committed by GitHub
parent ec1ab4db87
commit 42c08fa8e0
11 changed files with 153 additions and 73 deletions

View File

@@ -0,0 +1,61 @@
import { readdir } from "fs/promises"
import { join } from "path"
import { readDirRecursive } from "../read-dir-recursive"
jest.mock("fs/promises")
jest.mock("path")
describe("readDirRecursive", () => {
it("should recursively read directories and return all entries", async () => {
const mockReaddir = readdir as jest.MockedFunction<typeof readdir>
const mockJoin = join as jest.MockedFunction<typeof join>
// dir structure
const dirStructure = {
"/root": [
{ name: "file1.txt", isDirectory: () => false },
{ name: "subdir", isDirectory: () => true },
],
"/root/subdir": [
{ name: "file2.txt", isDirectory: () => false },
{ name: "nested", isDirectory: () => true },
],
"/root/subdir/nested": [{ name: "file3.txt", isDirectory: () => false }],
}
mockReaddir.mockImplementation((dir) => {
return dirStructure[dir as string] ?? []
})
mockJoin.mockImplementation((...paths) => paths.join("/"))
const result = await readDirRecursive("/root")
expect(result).toEqual([
{ name: "file1.txt", isDirectory: expect.any(Function), path: "/root" },
{ name: "subdir", isDirectory: expect.any(Function), path: "/root" },
{
name: "file2.txt",
isDirectory: expect.any(Function),
path: "/root/subdir",
},
{
name: "nested",
isDirectory: expect.any(Function),
path: "/root/subdir",
},
{
name: "file3.txt",
isDirectory: expect.any(Function),
path: "/root/subdir/nested",
},
])
expect(mockReaddir).toHaveBeenCalledWith("/root", { withFileTypes: true })
expect(mockReaddir).toHaveBeenCalledWith("/root/subdir", {
withFileTypes: true,
})
expect(mockReaddir).toHaveBeenCalledWith("/root/subdir/nested", {
withFileTypes: true,
})
})
})

View File

@@ -1,4 +1,3 @@
import { dirname, join } from "path"
import {
constants,
type Dirent,
@@ -8,8 +7,10 @@ import {
type StatOptions,
type WriteFileOptions,
} from "fs"
import { dirname, join } from "path"
import { readDirRecursive } from "./read-dir-recursive"
const { rm, stat, mkdir, access, readdir, readFile, writeFile } = promises
const { rm, stat, mkdir, access, readFile, writeFile } = promises
export type JSONFileOptions = WriteFileOptions & {
spaces?: number | string
@@ -127,10 +128,7 @@ export class FileSystem {
*/
readDir(dirPath?: string): Promise<Dirent[]> {
const location = dirPath ? this.makePath(dirPath) : this.basePath
return readdir(location, {
recursive: true,
withFileTypes: true,
})
return readDirRecursive(location)
}
/**

View File

@@ -56,6 +56,7 @@ export * from "./pick-value-from-object"
export * from "./plurailze"
export * from "./prefix-array-items"
export * from "./promise-all"
export * from "./read-dir-recursive"
export * from "./remote-query-object-from-string"
export * from "./remote-query-object-to-string"
export * from "./remove-nullisih"

View File

@@ -0,0 +1,23 @@
import { Dirent } from "fs"
import { readdir } from "fs/promises"
import { join } from "path"
export async function readDirRecursive(dir: string): Promise<Dirent[]> {
let allEntries: Dirent[] = []
const readRecursive = async (dir) => {
const entries = await readdir(dir, { withFileTypes: true })
for (const entry of entries) {
const fullPath = join(dir, entry.name)
entry.path = dir
allEntries.push(entry)
if (entry.isDirectory()) {
await readRecursive(fullPath)
}
}
}
await readRecursive(dir)
return allEntries
}