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

fs-recursive

v1.1.7

Published

Easily access your files in any folder or sub-folder.

Downloads

67

Readme

Recursive-fs

Easily access your files in any folder or sub-folder.

Objective

It is often difficult to retrieve and use files stored locally at your application, fs-recursive allows you to retrieve any file saved in a folder or sub-folder at a defined location.

How to use

Install the module in your project via YARN

yarn add fs-recursive

Or NPM

npm install fs-recursive

The fs-recursive is very simple to use, you need to use the followed function :

fetch(
  directory: string,
  extentions: Array<string>,
  encode?: Buffer | string | undefined,
  callback?: (file: File) => void
): Promise<Map<string, File>>

fetchExpression (
  path: string,
  filenamePattern: RegExp,
  encode: Encode,
  excludes?: Array<string>,
  callback?: (file: File) => void
): Promise<Map<string, File>>

fetchSortedExpression (
  path: string,
  filenamePattern: RegExp,
  sortedExtensions: Array<string>,
  encode: Encode,
  excludes?: Array<string>,
  callback?: (file: File) => void
): Promise<Array<File>>

An example is always more telling

import { fetch } from 'fs-recursive';

const directory = path.join(process.cwd(), 'folder');
const extensions = ['ts', 'json'];

const files = await fetch(directory, extensions, 'utf-8'); // return Map<string, File>
console.log(files.entries().next().value[1]);

/**
 * Log the followed result
 *
 * File {
 *   path: 'E:\\WindowsData\\Desktop\\folder`\\File.ts',
 *   filename: 'File',
 *   extension: 'ts',
 *   size: 1513   👈 expredded in bytes
 * }
 */

You can get the same result using RegExp

import { fetchExpression } from 'fs-recursive';

const directory = path.join(process.cwd(), 'folder');
const expression = /.*\.ts$/;

const files = await fetchExpression(directory, expression, 'utf-8'); // return Map<string, File>
console.log(files.entries().next().value[1]);

/**
 * Log the followed result
 *
 * File {
 *   path: 'E:\\WindowsData\\Desktop\\folder`\\File.ts',
 *   filename: 'File',
 *   extension: 'ts',
 *   size: 1513   👈 expredded in bytes
 * }
 */

Then you can access to other data like this

const files = await fetch(directory, extensions, 'utf-8'); // return Map<string, File>
const file = files.entries().next().value[1];
const stats = await file.getStats();

console.log(stats);
/**
 * Stats {
 *   dev: 310369320,
 *   mode: 33206,
 *   nlink: 1,
 *   uid: 0,
 *   gid: 0,
 *   rdev: 0,
 *   blksize: 4096,
 *   ino: 562949956735823,
 *   size: 72,
 *   blocks: 0,
 *   atimeMs: 1618523067313.032,
 *   mtimeMs: 1618521064609.6714,
 *   ctimeMs: 1618521064609.6714,
 *   birthtimeMs: 1618521033264.188,
 *   atime: 2021-04-15T21:44:27.313Z,
 *   mtime: 2021-04-15T21:11:04.610Z,
 *   ctime: 2021-04-15T21:11:04.610Z,
 *   birthtime: 2021-04-15T21:10:33.264Z
 * }
 */

You can sort results by their extension

import { fetchSortedExpression } from 'fs-recursive';

const extensions = ['ts', 'js'];
const expression = /.*\.(ts|js)$/;
const files = await fetchSortedExpression(
	process.cwd(),
	expression,
	extensions,
	'utf-8'
);

console.log(files);

/**
 * Log the followed result
 *
 * [
 * 	File {
 *   path: 'E:\\WindowsData\\Desktop\\folder`\\File.ts',
 *   filename: 'File',
 *   extension: 'ts',
 *   size: 1513   👈 expredded in bytes
 *	},
 * 	File {
 *   path: 'E:\\WindowsData\\Desktop\\folder`\\File.js',
 *   filename: 'File',
 *   extension: 'js',
 *   size: 1513   👈 expredded in bytes
 * 	}
 * ]
 */