# watch-dir-safe

> Watch a directory for changes

Latest version **1.0.2** (published 2020-10-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install watch-dir-safe
pnpm add watch-dir-safe
yarn add watch-dir-safe
bun add watch-dir-safe
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-10-04 |
| First published | 2020-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Connor White |
| Maintainers | bconnorwhite |

## Links

- npm: https://www.npmjs.com/package/watch-dir-safe
- Repository: https://github.com/bconnorwhite/watch-dir-safe
- Homepage: https://connorwhite.org/github/watch-dir-safe
- npm.io page: https://npm.io/package/watch-dir-safe

## Dependencies (1)

- [chokidar](https://npm.io/package/chokidar.md) ^3.4.2

## Recent versions

- 1.0.2 (latest) — 2020-10-04
- 1.0.1 — 2020-10-04
- 1.0.0 — 2020-10-04

## README

<div align="center">
  <a href="https://github.com/bconnorwhite/watch-dir-safe">
    <img alt="watch-dir-safe" src="assets/header.svg" />
  </a>
  <a href="https://npmjs.com/package/watch-dir-safe">
    <img alt="NPM" src="https://img.shields.io/npm/v/watch-dir-safe.svg">
  </a>
  <a href="https://github.com/bconnorwhite/watch-dir-safe">
    <img alt="TypeScript" src="https://img.shields.io/github/languages/top/bconnorwhite/watch-dir-safe.svg">
  </a>
  <a href='https://coveralls.io/github/bconnorwhite/watch-dir-safe?branch=master'>
    <img alt="Coverage Status" src="https://img.shields.io/coveralls/github/bconnorwhite/watch-dir-safe.svg?branch=master">
  </a>
  <a href="https://github.com/bconnorwhite/watch-dir-safe">
    <img alt="GitHub Stars" src="https://img.shields.io/github/stars/bconnorwhite/watch-dir-safe?label=Stars%20Appreciated%21&style=social">
  </a>
  <a href="https://twitter.com/bconnorwhite">
    <img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/bconnorwhite.svg?label=%40bconnorwhite&style=social">
  </a>
</div>

<br />

> Watch a directory for changes.

Provides a simplified wrapper around [chokidar](https://www.npmjs.com/package/chokidar) for watching a directory.

## Installation

```sh
yarn add watch-dir-safe
```

```sh
npm install watch-dir-safe
```

## API

### Usage
```ts
import watchDir from "watch-dir-safe";

const watcher = watchDir("/path/to/dir");

watcher.onReady(() => {
  console.log(`Ready`);
}).onAdd((path: string) => {
  console.log(`Added ${path}`);
}).onChange((path: string) => {
  console.log(`Changed ${path}`);
}).onRemove((path: string) => {
  console.log(`Removed ${path}`);
}).onAddDir((path: string) => {
  console.log(`Added dir ${path}`);
}).onRemoveDir((path: string) => {
  console.log(`Added dir ${path}`);
});

// To stop watching:
watcher.stop();
```

### Types
```ts
import watchDir, { Watcher, EventCallback } from "watch-dir-safe";

function watchDir(path: string): Watcher;

type EventCallback = (path: string) => void;

type Watcher = {
  onAdd: (cb: EventCallback) => Watcher;
  onRemove: (cb: EventCallback) => Watcher;
  onChange: (cb: EventCallback) => Watcher;
  onAddDir: (cb: EventCallback) => Watcher;
  onRemoveDir: (cb: EventCallback) => Watcher;
  onReady: (cb: () => void) => Watcher;
  stop: () => Promise<boolean>;
}
```

<br />

<h2>Dependencies<img align="right" alt="dependencies" src="https://img.shields.io/david/bconnorwhite/watch-file-safe.svg"></h2>

- [chokidar](https://www.npmjs.com/package/chokidar): A neat wrapper around node.js fs.watch / fs.watchFile / fsevents.

<br />

<h2>Dev Dependencies<img align="right" alt="David" src="https://img.shields.io/david/dev/bconnorwhite/watch-dir-safe.svg"></h2>

- [@bconnorwhite/bob](https://www.npmjs.com/package/@bconnorwhite/bob): undefined

<br />

<h2>License <img align="right" alt="license" src="https://img.shields.io/npm/l/watch-dir-safe.svg"></h2>

[MIT](https://opensource.org/licenses/MIT)

<br />

## Related Packages

- [fs-safe](https://www.npmjs.com/package/fs-safe): A simple fs wrapper that doesn't throw
- [watch-file-safe](https://www.npmjs.com/package/watch-file-safe): Watch a file for changes
- [read-dir-safe](https://www.npmjs.com/package/read-dir-safe): Read directories recursively or non-recursively
- [write-dir-safe](https://www.npmjs.com/package/write-dir-safe): Create directories and their parents recursively
- [remove-dir-safe](https://www.npmjs.com/package/remove-dir-safe): Remove directories recursively or non-recursively

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