-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #8419
- Loading branch information
1 parent
ec1ab4d
commit 42c08fa
Showing
11 changed files
with
153 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@medusajs/framework": patch | ||
"@medusajs/utils": patch | ||
--- | ||
|
||
Fix loaders path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/core/utils/src/common/__tests__/read-dir-recursive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |