Skip to content

Commit

Permalink
fix(utils): read dir recursive (#10318)
Browse files Browse the repository at this point in the history
What:
 - Using `Object.defineProperty) to avoid issues on Node versions that set this property as readonly.
  • Loading branch information
carlos-r-l-rodrigues authored Nov 27, 2024
1 parent e04c9cf commit e9003c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-singers-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---

fix(utils): set readonly property on recursive dir read
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,30 @@ describe("readDirRecursive", () => {

const result = await readDirRecursive("/root")

const paths = result.map((r) => r.path)

expect(paths).toEqual([
"/root",
"/root",
"/root/subdir",
"/root/subdir",
"/root/subdir/nested",
])

expect(result).toEqual([
{ name: "file1.txt", isDirectory: expect.any(Function), path: "/root" },
{ name: "subdir", isDirectory: expect.any(Function), path: "/root" },
{ name: "file1.txt", isDirectory: expect.any(Function) },
{ name: "subdir", isDirectory: expect.any(Function) },
{
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",
},
])

Expand Down
4 changes: 3 additions & 1 deletion packages/core/utils/src/common/read-dir-recursive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export async function readDirRecursive(dir: string): Promise<Dirent[]> {

for (const entry of entries) {
const fullPath = join(dir, entry.name)
entry.path = dir
Object.defineProperty(entry, "path", {
value: dir,
})
allEntries.push(entry)

if (entry.isDirectory()) {
Expand Down

0 comments on commit e9003c3

Please sign in to comment.