fs-structure
v1.1.0
Published
Create and delete files and folders in any structure using object syntax or JSON.
Downloads
234
Maintainers
Readme
fs-structure
Create and delete files and folders in any structure using object syntax or JSON.
Installation
Synopsis
import { load, create, remove, flat, symlink } from "fs-structure";
const tree = {
// Flat style (path as key)
"README.md": "Write here README content.",
"src/index.js": "console.log('Hello');",
"src/helper/util.js": "...",
"src/helper/main.js": "...",
"some-link": symlink({ target: "/path/to/some" }), // Using helepr function.
"other-link": { $type: "Symlink", target: "/path/to/other" }, // Using object.
// Below files are added to "test" directory". Also paths can be used too.
test: {
"index.test.js": "...",
"helper/util.test.js": "...",
"helper/main.test.js": "...",
},
};
await create(tree, { cwd: "/path/to/project" });
const loadedTree = await load("/path/to/project");
await remove(tree, { cwd: "/path/to/project" });
const flatTree = flat(tree);
expect(flatTree).toEqual(loadedTree);
Details
fs-structure
is a basic module to make it easier to create and delete file and folder structure. Structre can be defined as JS object or loaded from JSON.
- Ignores system files such as
.DS_Store
andThumbs.db
. Change withload(path, { ignoreJunk: false })
- Deletes empty directories. Change with `remove(tree, { rmUp: undefined });
- Loads tree from file system.
- Provides
flat()
function for easy comparison in tests.
fs-structure
fs-structure
Table of contents
Type aliases
Functions
Type aliases
Tree
Ƭ Tree: ItemLike<Root>
Defined in: main.ts:17
Functions
create
▸ create(input
: Tree, options?
: CreateOptions): Promise<void>
Creates files and directories in file system using given tree.
Example
await create({ a: 1, src: { b: 2, c: 2 } });
Parameters:
| Name | Type | Default value | Description |
| --------- | --------------- | ------------- | ------------------------------------------ |
| input
| Tree | - | is the file tree to create in file system. |
| options
| CreateOptions | ... | - |
Returns: Promise<void>
Defined in: main.ts:119
flat
▸ flat(input
: Tree, __namedParameters?
: { cwd?
: string ; includeDirs?
: boolean }): Tree
Converts given tree to a flat structure. May be used to compare two file tree easily.
Example
const tree = {
a: "1"
src: {
b: "2",
c: "3",
},
};
const flatObject = flat(tree); // { a: 1, "src/b": 2, "src/c": 2 }
Parameters:
| Name | Type | Default value | Description |
| ------------------- | ------------------------------------------------ | ------------- | ------------------ |
| input
| Tree | - | is the input tree. |
| __namedParameters
| { cwd?
: string ; includeDirs?
: boolean } | ... | - |
Returns: Tree
flat object for file system.
Defined in: main.ts:155
load
▸ load(path
: string, __namedParameters?
: { ignoreJunk?
: boolean ; includeDirs?
: boolean }): Promise<Tree>
Loads file tree from file system and makes it flat.
Parameters:
| Name | Type | Default value | Description |
| ------------------- | -------------------------------------------------------- | ------------- | ----------------------------------- |
| path
| string | - | is the path to load file tree from. |
| __namedParameters
| { ignoreJunk?
: boolean ; includeDirs?
: boolean } | ... | - |
Returns: Promise<Tree>
file tree.
Defined in: main.ts:103
remove
▸ remove(input
: Tree, options?
: RemoveOptions): Promise<void>
Removes files and directories from file system using given tree. Also deletes empty directories.
Example
await remove({ a: 1, src: { b: 2, c: 2 } });
Parameters:
| Name | Type | Default value | Description |
| --------- | --------------- | ------------- | -------------------------------------------- |
| input
| Tree | - | is the file tree to remove from file system. |
| options
| RemoveOptions | ... | - |
Returns: Promise<void>
Defined in: main.ts:133
symlink
▸ symlink(options
: PlainItemOptions<Symlink>): PlainItem<Symlink>
Generates a symlink to be used in file tree.
Example
await create({
"src/index.js": "console.log('a')";
"node_modules": symlink({ target: "./node_modules.nosync" });
})
Parameters:
| Name | Type | Description |
| --------- | ---------------------------- | ---------------- |
| options
| PlainItemOptions<Symlink> | are the options. |
Returns: PlainItem<Symlink>
object to create a symlink.
Defined in: main.ts:170
tempDir
▸ tempDir(): Promise<string>
Creates a random named directory in OS temporary directory.
Example
let TEMPDIR: string;
beforeAll(async () => {
TEMPDIR = await tempDir();
});
Returns: Promise<string>
Defined in: main.ts:184