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

@jrc03c/fs-extras

v0.0.21

Published

**fs-extras** is a little package of extra tools to supplement Node's `fs` module.

Downloads

18

Readme

Introduction

fs-extras is a little package of tools to supplement Node's fs module.

Installation

npm install --save @jrc03c/fs-extras

Usage

const fsx = require("@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 = fs.getFilesDeepSync("/some/directory", 3)
const shallowDirs = fs.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

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

find(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

getDirsDeepSync(dir : string, depth? : int)

synchronously returns all subdirectories of 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

getFilesDeepSync(dir : string, depth? : int)

synchronously returns all files in dir and all its subdirectories 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