Socket
Book a DemoInstallSign in
Socket

fs-visitor

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-visitor

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

0.2.7
latest
Source
npmnpm
Version published
Weekly downloads
12
Maintainers
1
Weekly downloads
 
Created
Source

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
  • Usage
  • API
  • Examples
  • Contributing
  • License

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 = {
  key: string;
  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.

Keywords

file system

FAQs

Package last updated on 20 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.