# fastify-walk

> Minimal Fastify interface to klaw.

Latest version **0.0.3** (published 2021-12-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-walk
pnpm add fastify-walk
yarn add fastify-walk
bun add fastify-walk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2021-12-27 |
| First published | 2021-10-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 7.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | galvez |

## Links

- npm: https://www.npmjs.com/package/fastify-walk
- Repository: https://github.com/terixjs/fastify-walk
- Issues: https://github.com/terixjs/fastify-walk/issues
- npm.io page: https://npm.io/package/fastify-walk

## Dependencies (5)

- [klaw](https://npm.io/package/klaw.md) ^4.0.1
- [globby](https://npm.io/package/globby.md) ^12.0.2
- [chokidar](https://npm.io/package/chokidar.md) ^3.5.2
- [minimatch](https://npm.io/package/minimatch.md) ^3.0.4
- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^3.0.0

## Recent versions

- 0.0.3 (latest) — 2021-12-27
- 0.0.2 — 2021-10-20
- 0.0.1 — 2021-10-20

## README

# fastify-walk

A minimal Fastify interface to [**klaw**](https://github.com/jprichardson/node-klaw) and [**chokidar**](https://github.com/paulmillr/chokidar).

## Install

```
npm install fastify-walk --save
```

## Basic Usage

```js
import Fastify from 'fastify'
import FastifyWalk from 'fastify-walk'

async function main () {
  const app = Fastify()
  await app.register(FastifyWalk, {
    path: '/path/to/search',
    watch: true, // enables chokidar integration, default false
  })

  // Will match both files and directories
  fastify.walk.onEntry((entry) => {})

  // Will match only directories
  fastify.walk.onDirectory((entry) => {})  
  // Will match only files
  fastify.walk.onFile((entry) => {})

  // Will match only files, and watch for changes
  fastify.walk.onFile({
    found (entry) {},
    changed (path) {}
  })

  // The first parameter can be a regular expression
  fastify.walk.onMatch(/\.md$/, (entry) => {})
  // Or a complete matcher function that also takes entry as parameter
  fastify.walk.onMatch((entry) => {
    return Math.random() * 1 > 0.5 ? true : false
  }, (randomEntry) => {
    return fs.readFile(randomEntry.path)
  })

  // Triggers actual filesystem read operation
  await fastify.ready()

  // Can be force-triggered ahead of time with:
  await fastify.walk.ready()
}
```

## Blog Example

```js
import Fastify from 'fastify'
import FastifyWalk from 'fastify-walk'
import { readFile } from 'fs/promises'
import { parse, add, sort, entries, archive } from './entry.js'

const app = Fastify()

app.decorate('blog', {
  index: null,
  entries,
  archive
})

app.register(FastifyWalk, {
  path: import.meta.url,
  pattern: 'posts/*.md',
  onFile,
  onReady
})

await app.ready()

async function onFile ({ path }) {
  const source = await readFile(path, 'utf8')
  const entry = await parse(source, path)
  if (!entry) {
    return
  }
  add(entry)
}

function onReady () {
  const entryList = Object.values(entries)
  this.blog.index = sort(entryList).slice(0, 10)
  for (const year in archive) {
    for (const month in archive[year]) {
      archive[year][month] = sort(archive[year][month])
    }
  }
}
```

## License

MIT

---
_Source: https://npm.io/package/fastify-walk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
