Skip to content

Instantly share code, notes, and snippets.

@EmmanuelDemey
Created March 18, 2025 10:30
Show Gist options
  • Save EmmanuelDemey/c4e41b654dbc38be89aca5b579d337d5 to your computer and use it in GitHub Desktop.
Save EmmanuelDemey/c4e41b654dbc38be89aca5b579d337d5 to your computer and use it in GitHub Desktop.
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 });
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" });
});
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;
};
export type Dictionnary = Record<string, unknown>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment