From e9003c3124f284eae7e1457101bf10c1beccbac1 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Wed, 27 Nov 2024 11:37:07 -0300 Subject: [PATCH] fix(utils): read dir recursive (#10318) What: - Using `Object.defineProperty) to avoid issues on Node versions that set this property as readonly. --- .changeset/clever-singers-vanish.md | 5 +++++ .../common/__tests__/read-dir-recursive.spec.ts | 17 ++++++++++++----- .../core/utils/src/common/read-dir-recursive.ts | 4 +++- 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 .changeset/clever-singers-vanish.md diff --git a/.changeset/clever-singers-vanish.md b/.changeset/clever-singers-vanish.md new file mode 100644 index 0000000000000..76755b302bdaf --- /dev/null +++ b/.changeset/clever-singers-vanish.md @@ -0,0 +1,5 @@ +--- +"@medusajs/utils": patch +--- + +fix(utils): set readonly property on recursive dir read diff --git a/packages/core/utils/src/common/__tests__/read-dir-recursive.spec.ts b/packages/core/utils/src/common/__tests__/read-dir-recursive.spec.ts index 217a6bae33a56..d36e232f15695 100644 --- a/packages/core/utils/src/common/__tests__/read-dir-recursive.spec.ts +++ b/packages/core/utils/src/common/__tests__/read-dir-recursive.spec.ts @@ -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", }, ]) diff --git a/packages/core/utils/src/common/read-dir-recursive.ts b/packages/core/utils/src/common/read-dir-recursive.ts index 77627718417d0..7ad9d2a7b43f2 100644 --- a/packages/core/utils/src/common/read-dir-recursive.ts +++ b/packages/core/utils/src/common/read-dir-recursive.ts @@ -9,7 +9,9 @@ export async function readDirRecursive(dir: string): Promise { 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()) {