npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fs-structure

v1.1.0

Published

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

Downloads

234

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

| 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