https://github.com/marvin-j97/walk-it
Recursive file walk-iterator for Node.js, Bun and Deno
https://github.com/marvin-j97/walk-it
deno javascript nodejs npm npm-package typescript
Last synced: 10 months ago
JSON representation
Recursive file walk-iterator for Node.js, Bun and Deno
- Host: GitHub
- URL: https://github.com/marvin-j97/walk-it
- Owner: marvin-j97
- License: mit
- Created: 2022-01-18T17:18:16.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-29T20:14:18.000Z (over 1 year ago)
- Last Synced: 2024-10-29T22:39:38.475Z (over 1 year ago)
- Topics: deno, javascript, nodejs, npm, npm-package, typescript
- Language: TypeScript
- Homepage:
- Size: 359 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# walk-it
[]()
[]()
[]()
[]()
[](https://github.com/marvin-j97/walk-it/actions/workflows/node.yml)
[](https://www.npmjs.com/package/walk-it)
[](https://jsr.io/@svarta/walk-it)
[](https://bundlephobia.com/package/walk-it)
[](https://codecov.io/gh/marvin-j97/walk-it)

Recursive file **walk-it**erator. Requires Node 20+, Deno or Bun.
## Install (Node)
```bash
npm i walk-it
pnpm i walk-it
yarn add walk-it
bun install walk-it
```
## Install (JSR)
```bash
npx jsr add @svarta/walk-it
pnpm jsr add @svarta/walk-it
yarn jsr add @svarta/walk-it
bunx jsr add @svarta/walk-it
```
## Deno usage
```typescript
import { walk } from "npm:walk-it";
```
## Default behaviour
By default, all folders will be visited recursively starting at the given directory.
Note that subfolders are visited _eagerly_, meaning the _level_ is not sorted (aka. [pre-order traversal is used instead of level-order](https://en.wikipedia.org/wiki/Tree_traversal)).
## Usage & examples
#### Walk all folders recursively
```typescript
import { walk } from "walk-it";
for await (const x of walk(dir)) {
// x contains:
// dir : the scanned folder's absolute path
// files : files as directory entries (Dirent)
// folders: folders as directory entries (Dirent)
// level : the tree level (0 being the start directory)
console.log(x);
}
```
#### Get files only
```typescript
import { walkFiles } from "walk-it";
for await (const { file, path } of walkFiles(dir)) {
// file is a Dirent object
// path is the absolute path of the visited file
console.log(file, path);
}
```
#### Limit recursive descent
```typescript
import { walk } from "walk-it";
// 0 = Only output 'dir'
// 1 = Descent once
// 2 = Descent twice
// ...
for await (const x of walk(dir, { maxLevel: 2 })) {
console.log(x);
}
```
#### Disable recursive descent altogether
Same as maxLevel = 0
```typescript
import { walk } from "walk-it";
for await (const x of walk(dir, { recursive: false })) {
console.log(x);
}
```
#### Whitelist files by extension
```typescript
import { walk } from "walk-it";
for await (const x of walk(".", {
filterFile: ({ name }) => name.endsWith(".jpg"),
})) {
console.log(x);
}
```
#### Blacklist folders
```typescript
import { walk } from "walk-it";
for await (const x of walk(".", {
filterFolder: ({ name }) => !["node_modules", ".git"].includes(name),
})) {
console.log(x);
}
```
_filterFolder_ should be preferred over filtering after walking because it will stop the recursive descent, thus increasing performance.
#### Count files
```typescript
import { countFiles } from "walk-it";
const count = await countFiles(".");
console.log(`${count} files found`);
const count = await countFiles(".", {
// You can also use the same options as walk and walkFiles
filterFolder: ({ name }) => !["node_modules", ".git"].includes(name),
});
console.log(`${count} files found`);
```