Socket
Book a DemoInstallSign in
Socket

recfs

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recfs

Utility functions used to list all files in a directory recursively, synchronously or asynchronously.

1.0.1
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

recfs

This npm module provides utility functions for listing all files in a directory either synchronously or asynchronously.

Installation

$ npm install recfs

Usage

First, we need to require the module:

const recfs = require('recfs')

Inside the recfs module, there are two functions: readdir(directory[, options[, regex]]) and readdirSync(directory[, options[, regex]]).

  • The directory is the directiory we would like to list all files recursively from.
  • The options is the same options as provided to fs.readdir and fs.readdirSync (by having this set to undefined, the character encoding will default to utf8).
  • The regex allows us to specify a search pattern. If a file does not match this pattern, it will not be returned.

Furthermore, readdirSync returns a Promise, whilst readdir returns the list of files located.

Directory Structure

For the following two examples, let us assume we have the directory structure:

cache/
	more/
		love.js
		test.htm
	test.c
	test.js

readdir

To read a directory asynchronously, we can do:

recfs.readdir('cache').then((files) => {
	console.log(files)
}).catch((err) => {
	throw err
})

// Expected output: [ 'cache/test.c', 'cache/test.js', 'cache/more/love.js', 'cache/more/test.htm' ]

Furthermore, if we are only interested in files ending in '.js', we can add a regular expression:

recfs.readdir('cache', undefined, /^.*\.js$/).then((files) => {
	console.log(files)
}).catch((err) => {
	throw err
})

// Expected output: [ 'cache/test.js', 'cache/more/love.js' ]

This allows us to retrieve a more specific set of files.

readdirSync

To read a directory synchronously, we can do:

let files = recfs.readdirSync('cache')

console.log(files)

// Expected output: [ 'cache/test.c', 'cache/test.js', 'cache/more/love.js', 'cache/more/test.htm' ]

Like with the asynchronous version of the function, we can provide it with a regular expression to retrieve a more specific set of files:

let files = recfs.readdirSync('cache', undefined, /^.*\.js$/)

console.log(files)

// Expected output: [ 'cache/test.js', 'cache/more/love.js' ]

License

This module, and the code therein, is licensed under ISC.

Keywords

recursive

FAQs

Package last updated on 11 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.