struct-fsraw
Version:
Creates a structured representation of a directory, including file metadata, file size, symlinks.
107 lines (73 loc) • 3.36 kB
Markdown
for Node.js
**struct-fsraw** retrieves a structured representation of a directory, including file metadata, file size, symlinks.
- **Recursive Scanning:** Optional recursive traversal.
- **Symlink Support:** Detect and resolve symbolic links, with an option to prune unnecessary symlink data.
- **Metadata Inclusion:** Optionally include file creation and modification timestamps.
- **Size Inclusion:** Optionally include file sizes.
- **Custom Key Mapping:** Easily switch between full key names and a compact representation using a key mapping object.
```sh
npm install struct-fsraw
```
```js
import struct from 'struct-fsraw';
const structure = await struct('./my-directory');
console.log(JSON.stringify(structure, null, 2));
```
**Output:**
```json
{
"path": "/absolute/path/to/my-directory",
"type": "d",
"symlink": false,
"children": [
{
"path": "/absolute/path/to/my-directory/file.txt",
"type": "f",
"symlink": false
}
]
}
```
```js
import { struct, getKeys } from 'struct-fsraw';
const keyMapping = getKeys(); // Retrieve the default compact key mapping
const fileTree = await struct('./path/to/directory', {
meta: true, // Include creation and modification times
size: true, // Include file sizes
recursive: true, // Recursively scan directories (default)
prune: false, // Do not prune symlink data
keys: keyMapping, // Use custom/compact key names
});
console.log(JSON.stringify(fileTree, null, 2));
```
Scans a directory and returns its structured representation.
| Parameter | Type | Default | Description |
| --------- | ------ | ---------- | ------------------------------ |
| `dir` | string | (Required) | Path of the directory to scan. |
| `options` | object | `{}` | Optional configuration. |
| Option | Type | Default | Description |
| ----------- | ------- | ------- | ---------------------------------------------------- |
| `meta` | boolean | `false` | Include file creation and modification timestamps. |
| `size` | boolean | `false` | Include file sizes in bytes. |
| `recursive` | boolean | `true` | Recursively scan subdirectories. |
| `prune` | boolean | `false` | Remove unnecessary symlink metadata unless required. |
| `keys` | object | `{}` | Customize key names for output. |
- [https://github.com/alexstevovich/struct-fs](https://github.com/alexstevovich/struct-fs) – A lighter fs struct that returns only file/dir names in a nested hierarchy and respects .ignore directives.
<sub>_These links might be suffixed with "-node" in the future if conflicts arise._</sub>
[ ](https://github.com/alexstevovich/struct-fsraw)
<sub>_This link might be suffixed with "-node" in the future if conflicts arise._</sub>
Licensed under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0).
> **Archetype:** Node.js package
Filesystem Structure Extraction