Giter Club home page Giter Club logo

node-walk-sync's Introduction

node-walk-sync

Build Status Build status

Return an array containing all recursive files and directories under a given directory, similar to Unix find. Follows symlinks. Bare-bones, but very fast.

Similar to wrench.readdirSyncRecursive, but adds trailing slashes to directories.

Not to be confused with node-walk, which has both an asynchronous and a synchronous API.

Installation

yarn add walk-sync

Usage

const walkSync = require('walk-sync');
const paths = walkSync('project')

Given project/one.txt and project/subdir/two.txt, paths will be the following array:

['one.txt', 'subdir/', 'subdir/two.txt']

Directories come before their contents, and have a trailing forward-slash (on all platforms).

Symlinks are followed.

Entries

Sometimes, it is important to get additional information from a walk of a directory; for instance if the downstream consumer needs to stat the files we can leverage the stats from the walk.

To accommodate, walkSync.entries(path [, options]) is also provided, instead of returning a list of files and/or directories it returns an array of objects which correspond to a given file or directory, except with more data.

entry.relativePath
entry.mode  // => fs.statSync(fullPath).mode
entry.size  // => fs.statSync(fullPath).size
entry.mtime // => fs.statSync(fullPath).mtime.getTime()

entry.isDirectory() // => true if directory

Options

  • globs: An array of globs. Only files and directories that match at least one of the provided globs will be returned.

    const paths = walkSync('project', { globs: ['subdir/**/*.txt'] });
    // => ['subdir/two.txt']

    As an alternative to string globs, you can pass an array of precompiled minimatch.Minimatch instances. This is faster and allows to specify your own globbing options.

  • directories (default: true): Pass false to only return files, not directories:

    const paths = walkSync('project', { directories: false })
    // => ['one.txt', 'subdir/two.txt']
  • ignore: An array of globs. Files and directories that match at least one of the provided globs will be pruned while searching.

    const paths = walkSync('project', { ignore: ['subdir'] })
    // => ['one.txt']
  • includeBasePath (default: false): Pass true to include the basePath in the output. note: this flag is only for walkSync(..) not walkSync.entries(..)

     const paths = walkSync('project', { includeBasePath: true });
     // => ['project/one.txt', 'project/subdir/two.txt']
  • fs: Allows an alternative implementation of fs to be supplied. examples of alternative filesystems include memfs or graceful-fs

     import {Volume, createFsFromVolume} from 'memfs'
     const fs = createFsFromVolume(Volume.fromJSON({'aDir/aFile': 'some-contents'}))
     const paths = walkSync('project', { fs });
     // => ['aDir/', 'aDir/aFile']

Background

walkSync(baseDir) is a faster substitute for

glob.sync('**', {
  cwd: baseDir,
  dot: true,
  mark: true,
  strict: true
})

node-walk-sync's People

Contributors

alexlafroscia avatar amiller-gh avatar chadhietala avatar dependabot[bot] avatar ef4 avatar elmpp avatar hjdivad avatar jakesjews avatar joliss avatar locks avatar lukemelia avatar rwjblue avatar stefanpenner avatar zeke avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.