UNPKG

4.07 kBMarkdownView Raw
1# node-walk-sync [![CI](https://github.com/joliss/node-walk-sync/workflows/CI/badge.svg)](https://github.com/joliss/node-walk-sync/actions/workflows/ci.yml)
2
3Return an array containing all recursive files and directories under a given
4directory, similar to Unix `find`. Follows symlinks. Bare-bones, but
5very fast.
6
7Similar to [`wrench.readdirSyncRecursive`](https://github.com/ryanmcgrath/wrench-js#synchronous-operations),
8but adds trailing slashes to directories.
9
10Not to be confused with [node-walk](https://github.com/coolaj86/node-walk),
11which has both an asynchronous and a synchronous API.
12
13## Installation
14
15```bash
16yarn add walk-sync
17```
18
19## Usage
20
21```js
22const walkSync = require('walk-sync');
23const paths = walkSync('project')
24```
25
26Given `project/one.txt` and `project/subdir/two.txt`, `paths` will be the following
27array:
28
29```js
30['one.txt', 'subdir/', 'subdir/two.txt']
31```
32
33Directories come before their contents, and have a trailing forward-slash (on
34all platforms).
35
36Symlinks are followed.
37
38### Entries
39
40Sometimes, it is important to get additional information from a walk of a
41directory; for instance if the downstream consumer needs to stat the files we
42can leverage the stats from the walk.
43
44To accommodate, `walkSync.entries(path [, options])` is also provided, instead
45of returning a list of files and/or directories it returns an array of objects
46which correspond to a given file or directory, except with more data.
47
48```
49entry.relativePath
50entry.mode // => fs.statSync(fullPath).mode
51entry.size // => fs.statSync(fullPath).size
52entry.mtime // => fs.statSync(fullPath).mtime.getTime()
53
54entry.isDirectory() // => true if directory
55```
56
57### Options
58
59* `globs`: An array of globs. Only files and directories that match at least
60 one of the provided globs will be returned.
61
62 ```js
63 const paths = walkSync('project', { globs: ['subdir/**/*.txt'] });
64 // => ['subdir/two.txt']
65 ```
66
67 As an alternative to string globs, you can pass an array of precompiled
68 [`minimatch.Minimatch`](https://github.com/isaacs/minimatch#minimatch-class)
69 instances. This is faster and allows to specify your own globbing options.
70
71* `directories` (default: true): Pass `false` to only return files, not
72 directories:
73
74 ```js
75 const paths = walkSync('project', { directories: false })
76 // => ['one.txt', 'subdir/two.txt']
77 ```
78
79* `ignore`: An array of globs. Files and directories that match at least one
80 of the provided globs will be pruned while searching.
81
82 ```js
83 const paths = walkSync('project', { ignore: ['subdir'] })
84 // => ['one.txt']
85 ```
86
87 As an alternative to string globs, you can pass an array of precompiled
88 [`minimatch.Minimatch`](https://github.com/isaacs/minimatch#minimatch-class)
89 instances. This is faster and allows to specify your own globbing options.
90
91* `includeBasePath` (default: false): Pass `true` to include the basePath in the output.
92 *note: this flag is only for `walkSync(..)` not `walkSync.entries(..)`*
93
94 ```js
95 const paths = walkSync('project', { includeBasePath: true });
96 // => ['project/one.txt', 'project/subdir/two.txt']
97 ```
98
99* `fs`: Allows an alternative implementation of [fs](https://nodejs.org/api/fs.html) to be supplied.
100 *examples of alternative file systems include [memfs](https://github.com/streamich/memfs) or [graceful-fs](https://github.com/isaacs/node-graceful-fs#readme)*
101
102 ```js
103 import {Volume, createFsFromVolume} from 'memfs'
104 const fs = createFsFromVolume(Volume.fromJSON({'aDir/aFile': 'some-contents'}))
105 const paths = walkSync('project', { fs });
106 // => ['aDir/', 'aDir/aFile']
107 ```
108
109* `globOptions`: Pass any options for [Minimatch](https://www.npmjs.com/package/minimatch) that will be applied to all items in `globs` and `ignore` that are strings.
110
111 If items in `globs` or `ignore` are instances of `minimatch.Minimatch`, the `globOptions` will not be applied.
112
113## Background
114
115`walkSync(baseDir)` is a faster substitute for
116
117```js
118glob.sync('**', {
119 cwd: baseDir,
120 dot: true,
121 mark: true,
122 strict: true
123})
124
\No newline at end of file