manifest-directory
v0.0.2
Published
Turns a directory of files into a dictionary of file contents
Downloads
6
Readme
Manifest Directory
Turns a directory of files into a dictionary of file contents.
Installation
npm install manifest-directory
Usage
import manifestDirectory from "manifest-directory";
const data = await manifestDirectory("path/to/directory");
console.log(data);
Transforming file contents
You can pass a function as the second argument to transform the file contents. It receives the file path and the file content as arguments.
import manifestDirectory from "manifest-directory";
const data = manifestDirectory("path/to/directory", {
transform: (path, content) => {
return content.toUpperCase();
},
});
console.log(data);
Pre-defined transformations
You can use one of the pre-defined transformations by passing a string as the second argument.
import manifestDirectory from "manifest-directory";
const data = manifestDirectory("path/to/directory", { transform: "binary" });
console.log(data);
Available transformations:
textAllowList
- If file matches a predefined list of extensions, returns the content as text. Otherwise, returns as Uint8Array.textDenyList
- If file matches a predefined list of extensions, returns the content as Uint8Array. Otherwise, returns as text.text
- Returns the content as text.utf8
- Returns the content as utf8 text.binary
- Returns the content as Uint8Array.base64
- Returns the content as a base64 string.
Modes
You can pass a mode
as the third argument to change the behavior of the function.
- direct-content - Returns the content of the file directly. Suitable for applications like ffs: the file fileystem.
import { writeFileSync } from "node:fs";
import manifestDirectory from "manifest-directory";
const data = manifestDirectory("path/to/directory", {
transform: "string",
mode: "direct-content",
});
writeFileSync("path/to/output.json", JSON.stringify(data, null, 2));
ffs path/to/output.json
cd /output
Reverse Usage
Use directoryManifested
to write the dictionary back to the filesystem.
import { directoryManifested } from "manifest-directory";
const data = {
"readme.md": "# hello\n",
};
directoryManifested("path/to/directory", data);