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

als-file-list

v0.4.0

Published

Generates filtered and sorted list of all file paths in a directory and its subdirectories.

Downloads

22

Readme

als-file-list

als-file-list is a Node.js package providing functions to generate filtered and sorted list of all file paths in a given directory and its subdirectories up to defined level. It supports both synchronous and asynchronous operations and is compatible with ES Modules and CommonJS.

Installation

Install als-file-list using npm:

npm install als-file-list

Basic usage

The fileList and fileListSync functions return an array of file paths relative to the specified directory, providing a convenient way to get a snapshot of all files within a directory structure.

import { fileList, fileListSync } from "als-file-list";
// or
const { fileList, fileListSync } = require("als-file-list");

const dirPath = "/path/to/directory"

// Asynchronous
(async () => {
  const files = await fileList(dirPath);
  console.log(files); // array of file paths relative to dirPath
})();

// Synchronous
const files = fileListSync(dirPath);
console.log(files); // array of file paths relative to dirPath

Advanced usage

The fileList function has two more parameters in addition to dirPath:

  • options: Object with following properties
    • level (number, optional): The maximum depth level of directory traversal. Defaults to Infinity, which means all subdirectories are explored.
    • level=0 - root
    • where (function, optional): A filter function applied to each file and directory. It must return true to include the file/directory in the result.
      • Function parameters:
        • relativePath (string) - The relative path to dirPath
        • stats (object) - The file system statistics object
        • isDir (boolean) - true if the path is a directory, false if it's a file
    • select (string, optional): A space-separated string defining which file attributes should be included in the result.
      • If specified, the result is an array of objects with {relativePath} and the selected attributes.
    • limit (number, optional): The maximum number of files to return. Defaults to Infinity.
    • sort (string, optional): The attribute to sort the results by.
    • skip (number, optional): Number of files to skip before starting to add files to the result.
  • errors (array, optional): An array to which any errors encountered during the function's execution will be added.

Example:

const { fileList } = require('als-file-list');

async function exampleUsage() {
  const dirPath = '/path/to/directory';
  const errors = [];
  const options = {
    level: 2, // Traverse up to level 2
    where: (relativePath, stats, isDir) => isDir || stats.size > 1024, // Only files larger than 1024 bytes
    select: 'atimeMs mtimeMs', // Return {relativePath, atimeMs, mtimeMs}
    limit: 10,
    sort: 'mtimeMs',
    skip: 2
  };
  const files = await fileList(dirPath, options, errors);
  if (errors.length > 0) {
    console.error(errors);
  }
  console.log(files);
}

exampleUsage();

On select use, returned result include array of objects with {relativPath,...selectedKeys} In the where function, you receive both folders and files. You must return true for isDir if you want to include folders. The stats object includes all file system statistics properties (like mtime, birthtime, ino, etc.)

Typically, the stats object includes properties such as:

[
  'dev',     'mode',
  'nlink',   'uid',
  'gid',     'rdev',
  'blksize', 'ino',
  'size',    'blocks',
  'atimeMs', 'mtimeMs',
  'ctimeMs', 'birthtimeMs',
  'atime',   'mtime',
  'ctime',   'birthtime'
]