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

v0.3.2

Published

Teardown API for testing file system-dependent code.

Downloads

12,201

Readme

fs-teardown

Teardown API for testing file system-dependent code.

Getting started

$ npm install fs-teardown -D
# or
$ yan add fs-teardown -D

API

createTeardown(options: CreateTeardownOptions): TeardownAPI

Calling the createTeardown function returns you the API to create the specified file system structure and clean it up on demand.

const api = createTeardown({
  rootDir: './example',
})

You can specify an optional paths property to describe an initial directory/file tree.

createTeardown({
  rootDir: './example',
  paths: {
    'empty.txt': null,
    'file.txt': 'hello world',
    'src/hooks': {
      'useField.js': null,
    },
  },
})

Providing a relative path to the rootDir creates the given path at the system's default directory for temporary files (os.tmpdir). Absolute paths are used as-is.

prepare(): Promise<string>

Creates files and directories specified in the operations of the teardown. Returns a Promise that resolves once all the operations have been executed.

create(tree: FileTree): Promise<void>

Creates a file tree relative to the root directory after the initial setup.

const api = createTeardown({
  rootDir: './example',
})

await api.create({
  'file.txt': 'hello world',
  'directory/another-file.txt': 'hello to you',
})

resolve(...segments: string[]): string

Returns an absolute path to the given file or directory relative to the rootDir of the teardown. Useful to reference a certain file or directory in the created file structure.

const api = createTeardown({
  rootDir: './example',
  paths: {
    'file.txt': 'hello world',
  },
})

const filePath = api.resolve('file.txt')
// "/Users/admin/example/file.txt"

readFile(filePath: string, encoding?: BufferEncoding): Promise<Buffer | string>

Reads a file at the given path.

By default, returns the Buffer of the read file. Provide the second encoding argument to convert the buffer to the given encoding.

const api = createTeardown({
  rootDir: './example',
  paths: {
    'file.txt': 'hello world'
  }
})

// Read the "file.txt" content as Buffer.
const buffer = await api.readFile('file.txt')

// Read the "file.txt" content as text.
const text = await api.readFile('file.txt', 'utf8)

edit(filePath: string): Promise<void>

Edits a file at the given path. Throws an error if the given file doesn't exist.

const api = createTeardown({
  rootDir: './example',
  paths: {
    'file.txt': 'hello world',
  },
})

await api.edit('file.txt', 'welcome to the jungle')

exec(command: string, options: ExecOptions?): Promise<{ stdout, stderr }>

Executes the given command in the mocked directory.

const api = createTeardown({
  rootDir: 'exec',
})

// Initiates a new Git repository at the mocked directory.
await api.exec('git init')

reset(): Promise<void>

Resets the root directory to its initial state.

const api = createTeardown({
  rootDir: './example',
  paths: {
    'file.txt': 'hello world',
    'dir/nested.txt': null,
  },
})

// Runtime actions.
await api.edit('file.txt', 'welcome to the jungle')
await api.remove('dir')

await api.restore()
// - "file.txt" was restored to "hello world";
// - "dir" and "dir/nested.txt" were restored.

cleanup(): Promise<void>

Removes the root directory of the teardown. Returns a Promise that resolves once the directory is removed.

await api.cleanup()

Recipes

Empty file

fsMocks.create({
  'filename.ext': null,
})

Text file

api.create({
  'file.txt': 'hello world',
})

JSON file

api.create({
  'file.json': JSON.stringify({ a: 1 }, null, 2),
})

Note that the JSON file's content must be a string. Object values are treated as nested files.

Empty directory

api.create({
  'empty-dir': null,
})

Keys without extensions are treated as directories.

Nested directories

api.create({
  'one/to/three': null,
})

Directory with multiple files

api.create({
  'deeply/nested/directory': {
    'one.txt': 'first file',
    'two.txt': 'second file',
  },
})