@semibran/fs-tree

speedy recursive reads and writes for simple file system trees

Usage no npm install needed!

<script type="module">
  import semibranFsTree from 'https://cdn.skypack.dev/@semibran/fs-tree';
</script>

README

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