New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fs-structure

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-structure

Create and delete files and folders in any structure using object syntax or JSON.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

fs-structure

Create and delete files and folders in any structure using object syntax or JSON.

  • Installation
  • Synopsis
  • Details
  • fs-structure

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 and Thumbs.db. Change with load(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:
NameTypeDefault valueDescription
inputTree-is the file tree to create in file system.
optionsCreateOptions...-

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:
NameTypeDefault valueDescription
inputTree-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:
NameTypeDefault valueDescription
pathstring-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:
NameTypeDefault valueDescription
inputTree-is the file tree to remove from file system.
optionsRemoveOptions...-

Returns: Promise<void>

Defined in: main.ts:133


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:
NameTypeDescription
optionsPlainItemOptions<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

Keywords

FAQs

Package last updated on 26 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc