Created
March 18, 2025 10:30
-
-
Save EmmanuelDemey/c4e41b654dbc38be89aca5b579d337d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { resolve } from "node:path"; | |
import { readAllFiles } from "./readAllFiles.ts"; | |
/** | |
* Glob - ExperimentalWarning --disable-warning=ExperimentalWarning NODE_OPTIONS= | |
* Typescript --experimental-transform-types --no-experimental-strip-types. import type | |
* Test | |
*/ | |
export const folder = resolve(import.meta.dirname, process.env.ROOT_FOLDER!); | |
const output = await readAllFiles(folder); | |
console.log("promies", { output }); |
This file contains hidden or 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
import assert from "assert"; | |
import test from "node:test"; | |
async function* asyncGenerator() { | |
yield "file1.json"; | |
yield "file2.json"; | |
} | |
test("should return files content", async (t) => { | |
t.mock.module("node:fs/promises", { | |
namedExports: { | |
glob: asyncGenerator, | |
readFile: (path) => `{"${path}": "value"}`, | |
}, | |
}); | |
const { readAllFiles } = await import("./readAllFiles.ts"); | |
const output = await readAllFiles(""); | |
assert.deepEqual(output, { "file1.json": "value", "file2.json": "value" }); | |
}); |
This file contains hidden or 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
import { glob, readFile } from "fs/promises"; | |
import type { Dictionnary } from "./type.ts"; | |
export const readAllFiles = async (folder: string) => { | |
let output: Dictionnary = {}; | |
for await (let path of glob(`${folder}/**/*.json`)) { | |
const content = await readFile(path); | |
output = { | |
...output, | |
...JSON.parse(content.toString()), | |
}; | |
} | |
return output; | |
}; |
This file contains hidden or 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
export type Dictionnary = Record<string, unknown>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment