@semibran/fs-tree

1.2.0 • Public • Published

fs-tree

speedy recursive reads and writes for simple file system trees

const { read, write } = require("@semibran/fs-tree")

read(src, (err, data) => {
  if (err) throw err
  write(dest, data, (err) => {
    if (err) throw err
    console.log(`copied from ${src} to ${dest}`)
  })
})

fs-tree provides fast recursive read and write operations for both files and folders by modelling them as strings and maps respectively. For example, consider the following folder structure:

foo
└── bar
    ├── hello.txt
    └── world.txt

In this scenario, read("foo", callback) might yield the following:

foo = {
  "bar": {
    "hello.txt": "greetings human",
    "world.txt": "welcome to mars"
  }
}

Furthermore, read("foo/bar/hello.txt", callback) would yield "greetings human", equivalent to the output of fs.readFile called with the "utf8" option.

Note that this module doesn't handle any kinds of links for simplicity's sake, although this property is subject to change.

usage

npm badge

read(path[, opts], callback(err, data))

Reads the file or folder specified by path and returns its data via callback.

read(path, (err, data) => {
  if (err) throw err
  console.log(path + ": " + JSON.stringify(data, null, 2))
})

opts

  • ignore: file names and file extensions to ignore, e.g. [ ".git", ".png" ]

write(path, data, callback(err))

Writes data to the given path, calling callback upon completion.

write(path, data, err => {
  if (err) throw err
  console.log("write successful")
})

related

Package Sidebar

Install

npm i @semibran/fs-tree

Weekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

5.12 kB

Total Files

4

Last publish

Collaborators

  • semibran