# mirror-folder

> Small module to mirror a folder to another folder. Supports live mode as well.

Latest version **3.1.0** (published 2020-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install mirror-folder
pnpm add mirror-folder
yarn add mirror-folder
bun add mirror-folder
```

## 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 | 3.1.0 |
| Published | 2020-04-27 |
| First published | 2017-03-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 22.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 87 |
| Author | Mathias Buus |
| Maintainers | jhand, mafintosh |

## Links

- npm: https://www.npmjs.com/package/mirror-folder
- Repository: https://github.com/mafintosh/mirror-folder
- Issues: https://github.com/mafintosh/mirror-folder/issues
- npm.io page: https://npm.io/package/mirror-folder

## Dependencies (2)

- [fd-read-stream](https://npm.io/package/fd-read-stream.md) ^1.1.0
- [recursive-watch](https://npm.io/package/recursive-watch.md) ^1.1.1

## Recent versions

- 3.1.0 (latest) — 2020-04-27
- 3.0.1 — 2020-04-17
- 3.0.0 — 2018-04-24
- 2.2.0 — 2018-04-23
- 2.1.1 — 2017-05-24
- 2.1.0 — 2017-04-30
- 2.0.0 — 2017-04-11
- 1.2.2 — 2017-04-04
- 1.2.1 — 2017-03-31
- 1.2.0 — 2017-03-30
- 1.1.1 — 2017-03-30
- 1.1.0 — 2017-03-27
- 1.0.1 — 2017-03-24
- 1.0.0 — 2017-03-24

## README

# mirror-folder

[![npm][npm-image]][npm-url]
[![travis][travis-image]][travis-url]

Small module to mirror a folder to another folder.

Supports watch mode as well where it will continuously watch the src folder and mirror new entries as they are created/removed.

```
npm install mirror-folder
```

## Usage

``` js
var mirror = require('mirror-folder')

mirror('/Users/maf/cool-stuff', '/Users/maf/cool-stuff-mirror', function (err) {
  if (err) throw err
  console.log('Folder was mirrored')
})
```

## API

#### `var progress = mirror(src, dst, [options], [callback])`

Mirror `src` to `dst`. Returns a progress event emitter.

Options include:

``` js
{
  watch: false, // keep watching the src and mirror new entries, can also be a custom watch function
  dereference: false, // dereference any symlinks
  equals: fun, // optional function to determine if two entries are the same, see below
  ignore: null, // optional async function to ignore file paths on src or dest
  dryRun: false, // emit all events but don't write/del files,
  keepExisting: false, // whether to delete extra files in the destination that are not present in the source
  skipSpecial: true // skip any files that are not regular files,
  ensureParents: false // ensure that all parent directories exist before creating children.
}
```

The equals function looks like this:

``` js
function equals (src, dst, cb) {
  console.log('src.name', src.name)
  console.log('src.stat', src.stat)
  console.log('dst.name', dst.name)
  console.log('dst.stat', dst.stat)
  cb(null, true) // callback with true if they are the same or false if not
}
```

Per default the equals function will check if mtime is larger on the src entry or if the size is different

The ignore function looks like this:

``` js
function ignore (file, cb) {
  // ignore any files with secret in them
  if (file.indexOf('secret') > -1) return process.nextTick(cb, null, true)
  return process.nextTick(cb, null, false)
}
```

If you want to use a custom watch function on the src fs, the `watch` option should take the form:
```js
var unwatch = function watch (path, onwatch) { ... }
```

If you are using a custom fs module (like [graceful-fs](https://github.com/isaacs/node-graceful-fs)) you can pass that in
with the `src` or `dst` like this:

``` js
mirror({name: '/Users/maf/cool-stuff', fs: customFs}, {name: '/Users/maf/cool-stuff-mirror', fs: anotherFs})
```

#### `progress.on('pending', {name, live})`

Emitted when file/dir added to pending queue.

#### `progress.pending`

Array of items pending to be processed.

#### `progress.on('put', src, dst)`

Emitted when a file/folder is copied from the src to the dst folder.

#### `progress.on('put-data', data)`

Emitted when a file chunk is read from the src.

#### `progress.on('put-end', src, dst)`

Emitted at the end of a write stream (files only).

#### `progress.on('del', dst)`

Emitted when a file/folder is deleted from the dst folder.

#### `progress.on('ignore', src, dst)`

Emitted when a file/folder is ignored (either src or dst).

#### `progress.on('skip', src, dst)`

Emitted when a file/folder is skipped. Either src file already is `equal` to dst file or file does not exist in either place.

#### `progress.on('end')`

Emitted when the mirror ends (not emitted in watch mode). The mirror callback is called when this event is emitted as well

#### `progress.on('error', err)`

Emitted when a critical error happens. If you pass a mirror callback you don't need to listen for this.

#### `progress.destroy()`

Stop mirroring files. If using watch mode, close the file watcher.

## License

MIT

[npm-image]: https://img.shields.io/npm/v/mirror-folder.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/mirror-folder
[travis-image]: https://img.shields.io/travis/mafintosh/mirror-folder.svg?style=flat-square
[travis-url]: https://travis-ci.org/mafintosh/mirror-folder

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