Skip to content

Run `fs.lstat()` for all contents in a given directory

License

Notifications You must be signed in to change notification settings

shinnn/lstat-dir

Repository files navigation

lstat-dir

npm version Build Status codecov

Run fs.promises.lstat() for all contents in a given directory

const lstatDir = require('lstat-dir');

(async () => {
  await lstatDir('node_modules/lstat-dir'); /*=> Map {
    '/Users/example/node_modules/lstat-dir/LICENSE' => {mode: 33188, size: 1086, ...},
    '/Users/example/node_modules/lstat-dir/README.md' => {mode: 33188, size: 2060, ...}
    '/Users/example/node_modules/lstat-dir/index.js' => {mode: 33188, size: 124, ...}
    '/Users/example/node_modules/lstat-dir/package.json' => {mode: 33188, size: 922, ...}
  } */
})();

Installation

Use npm.

npm install lstat-dir

API

const lstatDir = require('lstat-dir');

lstatDir(dir, [options])

dir: string | Buffer | URL (directory path)
options: Object (readdir-sorted options)
Return: Promise<Map>

The returned Promise is fulfilled with a Map, whose keys are absolute paths of contents in the directory, and whose values are fs.Stats of contents.

/* my-dir
   ├── file.txt
   ├── symlink => file.txt
   └── tmp
       └── debug.log
*/

(async () => {
  const stats = await lstatDir('my-dir');

  stats instanceof Map; //=> true

  stats.keys(); /*=> MapIterator {
    '/Users/example/my-dir/file.txt',
    '/Users/example/my-dir/symlink',
    '/Users/example/my-dir/tmp'
  } */

  stats.get('/Users/example/my-dir/file.txt').isFile(); //=> true
  stats.get('/Users/example/my-dir/symlink').isSymbolicLink(); //=> true
  stats.get('/Users/example/my-dir/tmp').isDirectory()(); //=> true
})();

Options are directly passed to the underlying readdir-sorted to control the order of keys.

(async() => {
  [...(await lstatDir('/path/dir')).keys()];
  // => ['/path/dir/10.txt', '/path/dir/2.txt', '/path/dir/ä.txt', '/path/dir/z.txt']

  [...(await lstatDir('/path/dir', {
    locale: 'sv',
    numeric: true
  })).keys()];
  //=> ['/path/dir/2.txt', '/path/dir/10.txt', '/path/dir/z.txt', '/path/dir/ä.txt']
})();

License

ISC License © 2017 - 2019 Shinnosuke Watanabe