# diff-file-tree

> Compare two file-trees, get a list of changes, then apply left or right

Latest version **2.5.1** (published 2020-12-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install diff-file-tree
pnpm add diff-file-tree
yarn add diff-file-tree
bun add diff-file-tree
```

## 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 | 2.5.1 |
| Published | 2020-12-10 |
| First published | 2017-04-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 28.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Author | Paul Frazee |
| Maintainers | pfraze, pfrazee |
| Keywords | diff, compare, files |

## Links

- npm: https://www.npmjs.com/package/diff-file-tree
- Repository: https://github.com/pfrazee/diff-file-tree
- Homepage: https://github.com/pfrazee/diff-file-tree#readme
- Issues: https://github.com/pfrazee/diff-file-tree/issues
- npm.io page: https://npm.io/package/diff-file-tree

## Dependencies (6)

- [pump](https://npm.io/package/pump.md) ^1.0.2
- [debug](https://npm.io/package/debug.md) ^2.6.4
- [tempy](https://npm.io/package/tempy.md) ^0.1.0
- [streamx](https://npm.io/package/streamx.md) ^2.7.0
- [stream-equal](https://npm.io/package/stream-equal.md) ^1.0.0
- [es6-promisify](https://npm.io/package/es6-promisify.md) ^5.0.0

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 2.5.1 (latest) — 2020-12-10
- 2.5.0 — 2020-09-11
- 2.4.0 — 2020-06-11
- 2.3.2 — 2019-08-16
- 2.3.1 — 2019-08-16
- 2.3.0 — 2019-08-16
- 2.2.0 — 2018-11-08
- 2.1.2 — 2018-06-08
- 2.1.1 — 2017-08-03
- 2.1.0 — 2017-06-06
- 2.0.2 — 2017-04-27
- 2.0.1 — 2017-04-26
- 2.0.0 — 2017-04-26
- 1.0.0 — 2017-04-22

## README

# diff-file-tree

Compare two file-trees, get a list of changes, then apply left or right.

```
npm install diff-file-tree
```

## Usage

```js
var dft = require('diff-file-tree')

var changes = await dft.diff('/home/alice/stuff', '/home/alice/things')
console.log(changes) /* => [
  {change: 'mod', type: 'file', path: '/hello.txt'},
  {change: 'add', type: 'dir',  path: '/pics'},
  {change: 'add', type: 'file', path: '/pics/kitty.png'},
  {change: 'del', type: 'file', path: '/backup/hello.txt'},
  {change: 'del', type: 'dir',  path: '/backup'},
  {change: 'del', type: 'file', path: '/hello.txt'},
]*/

// make 'things' a copy of 'stuff' 
await dft.applyRight('/home/alice/stuff', '/home/alice/things', changes)
// -or-
// make 'stuff' a copy of 'things'
await dft.applyLeft('/home/alice/stuff', '/home/alice/things', changes)
```

## Known issues

Files with equal size and mtimes will show as equal, even if their content is different. This is caused by an optimization which can sometimes give false positives.

## API

#### `var changes = await dft.diff(left, right[, opts])`

Get the differences between `left` and `right`.

Options include:

```js
{
  filter: null // optional function to ignore files (function (path) => bool)
  shallow: false // dont recurse into folders that need to be added or removed
  sizeLimit: {
    maxSize: undefined // max number of bytes before comparison aborts
    assumeEq: false // assume == (true) or assume != (false)
  }
  compareContent: false // set to true to compare by content instead of mtime & size
  compareContentCache: undefined // provide an object to cache file equality tests in memory
}
```

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

```js
dft.diff({path: '/Users/alice/stuff', fs: customFs}, {path: '/', fs: hyperdriveArchive})
```

#### `await dft.applyRight(left, right, changes)`

Make `right` equivalent to `left` using the given `changes`. Both `left` and `right` can be an object with custom `{path:, fs:}` as with `diff()`.

#### `await dft.applyLeft(left, right, changes)`

Make `left` equivalent to `right` using the given `changes`. Both `left` and `right` can be an object with custom `{path:, fs:}` as with `diff()`.

#### `dft.applyRightStream(left, right, changes)`

Make `right` equivalent to `left` using the given `changes`. Both `left` and `right` can be an object with custom `{path:, fs:}` as with `diff()`.

Returns a stream which emits each operation as `{op: String, path: String}`. You can cancel the merge-operation by destroying the stream.

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