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

recursive-readdir-files

v2.3.2

Published

Node.js module to list all files in a directory or any subdirectories.

Downloads

3,374

Readme

recursive-readdir-files

Buy me a coffee Coverage Status npm version NPM Download

Node.js module to list all files in a directory or any subdirectories.

Installation

This package is ESM only: Node 12+ is needed to use it and it must be import instead of require.

npm install recursive-readdir-files

If you still want to use in CommonJS, you can use dynamic import() to load.

const recursiveReaddirFiles = await import('recursive-readdir-files');

// Fix compiling in typescript.
// https://github.com/microsoft/TypeScript/issues/43329#issuecomment-922544562
const { getExt, recursiveReaddirFiles } = await (Function('return import("recursive-readdir-files")')()) as Promise<typeof import("recursive-readdir-files")>;

Usage

import recursiveReaddirFiles from 'recursive-readdir-files';

const files = await recursiveReaddirFiles(process.cwd(), {
  ignored: /\/(node_modules|\.git)/
});

// `files` is an array
console.log(files);
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// [
//   {
//     dev: 16777233,
//     mode: 33188,
//     nlink: 1,
//     uid: 501,
//     gid: 20,
//     rdev: 0,
//     blksize: 4096,
//     ino: 145023089,
//     size: 89,
//     blocks: 8,
//     atimeMs: 1649303678077.934,
//     mtimeMs: 1649303676847.1777,
//     ctimeMs: 1649303676847.1777,
//     birthtimeMs: 1649301118132.6782,
//     atime: 2022-04-07T03:54:38.078Z,
//     mtime: 2022-04-07T03:54:36.847Z,
//     ctime: 2022-04-07T03:54:36.847Z,
//     birthtime: 2022-04-07T03:11:58.133Z,
//     name: 'watch.ts',
//     path: '/Users/xxx/watch.ts',
//     ext: 'ts'
//   },
//   // ...
// ]

Or

recursiveReaddirFiles(process.cwd(), {
  ignored: /\/(node_modules|\.git)/
}, (filepath, state) => {
  console.log(filepath);
  // 👉 /Users/xxx/watch.ts
  console.log(state.isFile());      // 👉 true
  console.log(state.isDirectory()); // 👉 false
  console.log(state);
  // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  // {
  //   dev: 16777233,
  //   mode: 33188,
  //   nlink: 1,
  //   uid: 501,
  //   gid: 20,
  //   rdev: 0,
  //   blksize: 4096,
  //   ino: 145023089,
  //   size: 89,
  //   blocks: 8,
  //   atimeMs: 1649303678077.934,
  //   mtimeMs: 1649303676847.1777,
  //   ctimeMs: 1649303676847.1777,
  //   birthtimeMs: 1649301118132.6782,
  //   atime: 2022-04-07T03:54:38.078Z,
  //   mtime: 2022-04-07T03:54:36.847Z,
  //   ctime: 2022-04-07T03:54:36.847Z,
  //   birthtime: 2022-04-07T03:11:58.133Z,
  //   name: 'watch.ts',
  //   path: '/Users/xxx/watch.ts',
  //   ext: 'ts'
  // }
})

Options

export interface RecursiveReaddirFilesOptions {
  /**
   * Ignore files
   * @example `/\/(node_modules|\.git)/`
   */
  ignored?: RegExp;
  /**
   * Specifies a list of `glob` patterns that match files to be included in compilation.
   * @example `/(\.json)$/`
   */
  include?: RegExp;
  /**
   * Specifies a list of files to be excluded from compilation.
   * @example `/(package\.json)$/`
   */
  exclude?: RegExp;
  /** Provide filtering methods to filter data. */
  filter?: (item: IFileDirStat) => boolean;
}

Result

import fs from 'node:fs';
export interface IFileDirStat extends Partial<fs.Stats> {
  /**
   * @example `/a/sum.jpg` => `sum.jpg`
   */
  name: string;
  /**
   * @example `/basic/src/utils/sum.ts`
   */
  path: string;
  /**
   * @example `/a/b.jpg` => `jpg`
   */
  ext?: string;
}
declare type Callback = (filepath: string, stat: IFileDirStat) => void;
export default function recursiveReaddirFiles(rootPath: string, options?: RecursiveReaddirFilesOptions, callback?: Callback): Promise<IFileDirStat[]>;
export { recursiveReaddirFiles };
export declare const getStat: (filepath: string) => Promise<IFileDirStat>;
/**
 * Get ext
 * @param {String} filePath `/a/b.jpg` => `jpg`
 */
export declare const getExt: (filePath: string) => string;

Contributors

As always, thanks to our amazing contributors!

Made with action-contributors.

License

Licensed under the MIT License.