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

walk-sync

v3.0.0

Published

Get an array of recursive directory contents

Downloads

6,737,091

Readme

node-walk-sync CI

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']

    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.

  • 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 file systems 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']
  • globOptions: Pass any options for Minimatch that will be applied to all items in globs and ignore that are strings.

    If items in globs or ignore are instances of minimatch.Minimatch, the globOptions will not be applied.

Background

walkSync(baseDir) is a faster substitute for

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