Socket
Socket
Sign inDemoInstall

walker

Package Overview
Dependencies
2
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

walker

A simple directory tree walker.


Version published
Maintainers
1
Weekly downloads
21,241,592
increased by1.07%
Install size
17.0 kB

Weekly downloads

Package description

What is walker?

The `walker` npm package is a tool designed for efficiently walking (traversing) file trees in a Node.js environment. It allows developers to easily perform operations on file systems, such as listing all files in a directory and its subdirectories, filtering files based on certain criteria, and processing each file individually. This package is particularly useful for tasks that involve bulk file operations, such as building a search index, file analysis, or automated file organization.

What are walker's main functionalities?

File Traversal

This feature allows the user to traverse all files starting from a specified directory. It emits events for each file found, enabling the execution of custom logic such as logging or processing of each file.

const walker = require('walker');
const path = '/path/to/start/directory';

walker(path)
  .on('file', function(file, stat) {
    console.log('Found file: ' + file);
  })
  .on('error', function(err, entry) {
    console.error('Error at: ' + entry + ' - ' + err);
  })
  .on('end', function() {
    console.log('All files have been processed');
  });

Directory Filtering

This feature demonstrates how to filter directories during traversal, allowing the user to skip certain directories (e.g., 'node_modules'). This is useful for focusing on relevant files and improving performance by avoiding unnecessary directories.

const walker = require('walker');
const path = '/path/to/start/directory';

walker(path)
  .filterDir(function(dir, stat) {
    // Only traverse directories that are not named 'node_modules'
    return dir.indexOf('node_modules') === -1;
  })
  .on('file', function(file, stat) {
    console.log('Found file: ' + file);
  });

Other packages similar to walker

Readme

Source

walker Build Status

A nodejs directory walker. Broadcasts events for various file types as well as a generic "entry" event for all types and provides the ability to prune directory trees. This shows the entire API; everything is optional:

Walker('/etc/')
  .filterDir(function(dir, stat) {
    if (dir === '/etc/pam.d') {
      console.warn('Skipping /etc/pam.d and children')
      return false
    }
    return true
  })
  .on('entry', function(entry, stat) {
    console.log('Got entry: ' + entry)
  })
  .on('dir', function(dir, stat) {
    console.log('Got directory: ' + dir)
  })
  .on('file', function(file, stat) {
    console.log('Got file: ' + file)
  })
  .on('symlink', function(symlink, stat) {
    console.log('Got symlink: ' + symlink)
  })
  .on('blockDevice', function(blockDevice, stat) {
    console.log('Got blockDevice: ' + blockDevice)
  })
  .on('fifo', function(fifo, stat) {
    console.log('Got fifo: ' + fifo)
  })
  .on('socket', function(socket, stat) {
    console.log('Got socket: ' + socket)
  })
  .on('characterDevice', function(characterDevice, stat) {
    console.log('Got characterDevice: ' + characterDevice)
  })
  .on('error', function(er, entry, stat) {
    console.log('Got error ' + er + ' on entry ' + entry)
  })
  .on('end', function() {
    console.log('All files traversed.')
  })

You specify a root directory to walk and optionally specify a function to prune sub-directory trees via the filterDir function. The Walker exposes a number of events, broadcasting various file type events a generic error event and finally the event to signal the end of the process.

Keywords

FAQs

Last updated on 23 Oct 2021

Did you know?

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc