Socket
Socket
Sign inDemoInstall

@danmasta/walk

Package Overview
Dependencies
3
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@danmasta/walk


Version published
Maintainers
1
Created

Readme

Source

Walk

Directory and file walking utility for node apps

Features:

  • Easy to use
  • Sync and Async api
  • Returns bluebird promises
  • Simple filtering with glob pattern matching
  • Doesn't use streams or events
  • Require file contents or read them
  • Can resolve require-style path strings
  • Normalized file objects with helper methods
  • Fast pattern matching via micromatch

About

We needed a better way to walk directories and read files during build and/or start time. I wanted an api that was simple, supported glob pattern matching like gulp, and returned objects with a similar format as vinyl. This package allows you to simply read any directory (or file), return an array of objects, and filter results with glob pattern matching. It can also require file contents, and resolve require-style path strings.

Usage

Add walk as a dependency for your app and install via npm

npm install @danmasta/walk --save

Require the package in your app

const walk = require('@danmasta/walk');

Options

nametypedescription
cwdstringBase directory to start walk from. Default is process.cwd()
rootstringDirectory or file path to walk. This gets normalized as path.join(cwd, root). Default is /
excludearrayArray of directory names to exclude from walk. Defaults to ['.git', 'node_modules', 'bower_components']. Directory names are excluded before reading them, this helps it stay fast
requirebooleanWhether to require file contents instead of reading them. Default is false
readbooleanWhether to read|require file contents when using each(). Defaults to true
srcArray|String|RegExpMicromatch pattern for result filtering. Can be a path string, glob pattern string, regular expression, or an array of strings. Defaults to **/*
dotbooleanWhether or not to ignore dot files when matching. Default is true

Methods

NameDescription
walk(opts)Get a list of files based on specified options. Returns a promise that resolves with an array of file objects
walkSync(opts)Sync version of walk()
contents(opts)Get the contents of files based on specified options. Returns a promise that resolves with an array of file objects
contentsSync(opts)Sync version of contents()
each(opts, cb)Runs a callback for each file based on specified options. Returns a promise that resolves with an array of file objects. Callback takes one argument file
eachSync(opts, cb)Sync version of each()

File Objects

Each file returned from walk has the following signature

Properties

nametypedescription
cwdstringCurrent working directory. Defaults to process.cwd()
rootstringBase directory to use for relative pathing. Defaults to process.cwd
pathstringAbsolute path of the file on disk
relativestringRelative path of file based normalized from root
dirstringParent directory where file is located
basestringFile name with extension
namestringFile name without extension
extstringFile extension
statobjectThe fs.stat object for the file
contentsstring|objectContents of the file. If require is true, will be resolved object, otherwise string. Default is null

Methods

namedescription
isBufferReturns true if file.contents is a buffer
isStreamReturns true if file.contents is a stream
isNullReturns true if file.contents is null
isDirectoryReturns true if the file is a directory
isSymbolicReturns true if the file is a symbolic link

Examples

Walk

const walk = require('@danmasta/walk').walk;

Walk the current directory, exclude all .json files

walk({ src: '**/*.!(json)' }).then(res => {
    console.log('files:', res);
});

Walk a child directory, include only .json files

walk({ root: './config', src: '**/*.json' }).then(res => {
    console.log('files:', res);
});

Contents

const contents = require('@danmasta/walk').contents;

Read the contents of all pug files in ./views

contents({ root: './views', src: '**/*.pug' }).then(res => {
    console.log('templates:', res);
});

Each

const each = require('@danmasta/walk').each;

Require all js files in the ./routes directory and run a callback for each one

each({ root: './routes', src: '**/*.js', require: true }, route => {
    app.use(route());
}).then(res => {
    console.log('all routes loaded');
});

Synchronous Methods

To use the sync version of any method just append Sync to the end of the method name

const contents = require('@danmasta/walk').contentsSync;

Load all templates from the ./views directory

const templates = contents({ root: './views', src: '**/*.pug' });
console.log('templates:', templates);

Contact

If you have any questions feel free to get in touch

Keywords

FAQs

Last updated on 15 Jan 2018

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

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc