@jrc03c/fs-extras
v0.0.31
Published
**fs-extras** is a little package of extra tools to supplement Node's `fs` module.
Downloads
218
Readme
introduction
fs-extras is a little package of tools to supplement node's fs module.
installation
npm install --save-dev @jrc03c/fs-extrasusage
import * as fsx from "@jrc03c/fs-extras"
// get all files in a directory and its subdirectories
const allFiles = fsx.getFilesDeepSync("/some/directory")
// get all subdirectories of a directory
const allDirs = fsx.getDirsDeepSync("/some/directory")
// add an optional depth argument
// (e.g., in this case, only go 3 levels down)
const shallowFiles = fsx.getFilesDeepSync("/some/directory", 3)
const shallowDirs = fsx.getDirsDeepSync("/some/directory", 3)all of the above functions have asynchronous versions. simply omit the "Sync" at the end of the function name. for example, the asynchronous version of getFilesDeepSync would be getFilesDeep.
the async functions return a Promise, but you can also pass a callback to them; either style is fine! for example, both of these styles work as expected:
const fsx = require("@jrc03c/fs-extras")
// version 1: using a callback
fsx.getFilesDeep("/some/directory", files => {
console.log(files)
})
// version 2: using a Promise
fsx.getFilesDeep("/some/directory").then(files => {
console.log(files)
})caveats
this has only been tested on linux. i suspect it probably won't work on windows unless you're using wsl or similar.
api
createFileStreamReader(file : string, progress : function)
returns an object that can read from a file one line at a time, which is useful for very large files; can optionally accept a progress callback function
for example:
const { createFileStreamReader } = require("@jrc03c/fs-extras")
!(async () => {
const reader = createFileStreamReader("path/to/my-big-file.txt")
for await (const line of reader.read()) {
// do something with `line`
}
reader.close()
})()createFileStreamWriter(file : string)
returns an object that can write to a file one ~line~ chunk at a time, which may in some cases be preferable to writing an entire file to disk all at once
for example:
const { createFileStreamWriter } = require("@jrc03c/fs-extras")
!(async () => {
const writer = createFileStreamWriter("path/to/my-file.txt")
for (let i = 0; i < 1e20; i++) {
await writer.write(Math.random().toString() + "\n")
}
writer.close()
})()note that the example above writes one line at a time, but that's only because of the "\n" at the end
findAsync(dir : string, matcher : RegExp | string | function, depth? : int, callback? : function)
asynchronously returns an array of directories and files matched by matcher to an optional depth of depth
findIter(dir : string, matcher : RegExp | string | function, depth? : int)
returns an iterator over the directories and files matched by matcher to an optional depth of depth
for example:
const dir = "some/dir"
const matcher = f => f.endsWith(".md")
const depth = Infinity
const iterator = findIter(dir, matcher, depth)
for (const result of iterator) {
// do something with `result`
}findSync(dir : string, matcher : RegExp | string | function, depth? : int)
synchronously returns an array of directories and files matched by matcher to an optional depth of depth
getAllDeep(dir : string, depth? : int, callback? : function)
asynchronously returns an array of all directories and files in dir to an optional depth of depth
getAllDeepIter(dir : string, depth? : int)
returns an iterator over the directories and files in dir to an optional depth of depth
getAllDeepSync(dir : string, depth? : int)
returns an array of all directories and files in dir to an optional depth of depth
getDirsDeep(dir : string, depth? : int, callback? : function)
asynchronously returns all subdirectories of dir to an optional depth of depth
getDirsDeepIter(dir : string, depth? : int)
returns an iterator over all subdirectories of dir to an optional depth of depth
getDirsDeepSync(dir : string, depth? : int)
synchronously returns all subdirectories of dir to an optional depth of depth
getFilesDeep(dir : string, depth? : int, callback? : function)
asynchronously returns all files in dir and all its subdirectories to an optional depth of depth
getFilesDeepIter(dir : string, depth? : int)
returns an iterator over all files in dir and all its subdirectories to an optional depth of depth
getFilesDeepSync(dir : string, depth? : int)
synchronously returns all files in dir and all its subdirectories to an optional depth of depth
