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-visitor

v0.2.0

Published

A Node.js library for recursively listing files and directories with customizable filtering and sorting options.

Downloads

17

Readme

fs-visitor

fs-visitor is a Node.js library that provides a simple API to recursively list files and directories with various filtering and sorting options.

Table of Contents

Installation

npm install fs-visitor

Usage

const { processVisitor } = require("fs-visitor");
// Example: List all files and directories in the current directory
const entries = processVisitor("./");
console.log(entries);
// Example: List only TypeScript files with a depth of 2
const tsFilter = (entry) => entry.type === "file" && entry.name.endsWith(".ts");
const options = { deep: 2, filterEntry: tsFilter };
const tsEntries = processVisitor("./src", options);
console.log(tsEntries);

API

Entry

type Entry = {
  type: "directory" | "file" | string;
  name: string;
  absolute: string;
  relative: string;
};

Represents an entry (file or directory) returned by processVisitor.

FilterEntry

type FilterEntry = (entry: Entry) => boolean;

A function used for filtering entries based on custom criteria.

Options

type Options = {
  deep?: number;
  filterEntry?: FilterEntry | FilterEntry[];
  withDirectory?: boolean;
  sorter?: (a: Entry, b: Entry) => number;
  relativePrefix?: string;
  useSlash?: boolean;
};

Options to customize the behavior of processVisitor.

  • deep: Maximum depth to scan.
  • filterEntry: A single or an array of FilterEntry functions.
  • withDirectory: Include directories in the result.
  • sorter: Custom function to sort entries.
  • relativePrefix: Prefix to add to relative paths.
  • useSlash: Use forward slash / as a directory separator.

processVisitor

export const processVisitor = (
     directory: string,
  options: Options = {}
) => Entry[];

The main method that lists files and directories based on the provided options.

Examples

Example 1: List All Files and Directories

import { processVisitor } from "fs-visitor";
const entries = processVisitor("./");
console.log(entries);

Example 2: List TypeScript Files Only (Depth 2)

import { processVisitor } from "fs-visitor";
const entries = processVisitor("./src", {
  deep: 2,
  filterEntry: (entry) => {
    return entry.type === "file" && entry.name.endsWith(".ts");
  },
});
console.log(entries);

Example 3: Include and Exclude

This example use minimatch and expose a simple function createFileEntry in filter file

import { processVisitor } from "fs-visitor";
import { createFileEntry } from "fs-visitor/filter";
const filterEntry = createFileEntry({
  include: "**/*.ts",
  exclude: ["**/Config*", "**/Defined*"],
});
const entries = processVisitor("./src/widget", { filterEntry });
console.log(entries);

Feel free to contribute to this project, report issues, or suggest improvements. Your feedback is highly appreciated.

License

This project is licensed under the MIT License - see the LICENSE file for details.