# sync-directory2

> sync two directories by copying or creating hardlink

Latest version **2.2.22** (published 2021-05-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install sync-directory2
pnpm add sync-directory2
yarn add sync-directory2
bun add sync-directory2
```

Provides the command `syncdir`.

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.22 |
| Published | 2021-05-22 |
| First published | 2021-05-22 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 21.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | https://github.com/hoperyy |
| Maintainers | zamiell |

## Links

- npm: https://www.npmjs.com/package/sync-directory2
- Repository: https://github.com/IsaacScript/sync-directory
- Homepage: https://github.com/hoperyy/sync-directory#readme
- Issues: https://github.com/hoperyy/sync-directory/issues
- npm.io page: https://npm.io/package/sync-directory2

## Dependencies (5)

- [chokidar](https://npm.io/package/chokidar.md) ^3.5.1
- [fs-extra](https://npm.io/package/fs-extra.md) ^10.0.0
- [commander](https://npm.io/package/commander.md) ^7.2.0
- [is-absolute](https://npm.io/package/is-absolute.md) ^1.0.0
- [readdir-enhanced](https://npm.io/package/readdir-enhanced.md) ^6.0.4

## Recent versions

- 2.2.22 (latest) — 2021-05-22
- 2.2.21 — 2021-05-22

## README

## Description

`sync-directory` can sync files from src directory to target directory.

We have two ways to sync files: `hardlink` and `copy`.

If type is `copy`, `sync-directory` will copy files from src directory to target directory.

If type is `hardlink`, `sync-directory` can create hardlink files in target directory from src directory.

Apparently, the type `hardlink` is quicker than type `copy`, and `sync-directory` uses `hardlink` by default.

## Cli

```bash
npm i sync-directory -g
```

```bash
syncdir <from> <to> [options]
```

Example: `syncdir aaa bbb -w`

options:

+   `-w, --watch`

    Watch changes. `false` as default.
    
    Same as api `watch`.

+   `--quiet`

    Disable unnecessary logs.

+   `-do, --deleteOrphaned`

    Delete orphaned files/folders in target folder. `false` as default.

    Same as api `deleteOrphaned`.

+   `-c, --copy`

    Sync with type `copy`, `hardlink` as default.

    Same as api `type: 'copy'`.

+   `-symlink, --symlink`

    support symlink while sync running. `false` as default.

    Same as api `supportSymlink`.

## API

```js
require('sync-directory')(srcDir, targetDir[, config]);
```

+   parames

    name | description | type | values | default
    ---- | ---- | ---- | ---- | ----
    `srcDir` | src directory | String | absolute path | -
    `targetDir` | target directory | String | absolute path | -
    `config.watch` | watch files change | Boolean | - | false
    `config.type` | way to sync files | String | `'copy' / 'hardlink'` | `'hardlink'`
    `config.deleteOrphaned` | Decide if you want to delete other files in targetDir when srcDir files are removed | Boolean | - | true
    `config.afterSync` | callback function when files synced | Function | - | blank function
    `config.supportSymlink` | ensure symlink in target if src has symlinks | Boolean | - | false
    `config.exclude` | files that should not sync to target directory. | RegExp / String / Array (item is RegExp / String) | - | null
    `config.forceSync` | some files must be synced even though 'excluded' | Function | - | `(file) => { return false }`
    `config.filter` | callback function to filter synced files. Sync file when returning `true` | Function | - | `filepath => true`
    `config.onError` | callback function when something wrong | Function | - | `(err) => { throw new Error(err) }`

+   return

    ```js
    const watcher = require('sync-directory')(A, B);
    ```

    `watcher` is `undefined`.

    ```js
    const watcher = require('sync-directory')(A, B, {
        watch: true
    });
    ```

    `watcher` is a [chokidar watcher](https://github.com/paulmillr/chokidar).

## Params & Examples

+   `watch`

    ```js
    require('sync-directory')(srcDir, targetDir, {
        watch: true
    });
    ```

+   `afterSync`

    ```js
    require('sync-directory')(srcDir, targetDir, {
        afterSync({ type, relativePath }) {
            // type: add / change / unlink / unlinkDir
            // relativePath: relative file path
        }
    });
    ```

+   `type`

    copy

    ```js
    require('sync-directory')(srcDir, targetDir, {
        type: 'copy'
    });
    ```

    hardlink (default)

    ```js
    require('sync-directory')(srcDir, targetDir);
    ```

+   `exclude`

    exclude `node_modules`

    +   String

        ```js
        require('sync-directory')(srcDir, targetDir, {
            exclude: 'node_modules'
        });
        ```

    +   RegExp

        ```js
        require('sync-directory')(srcDir, targetDir, {
            exclude: /node\_modules/
        });
        ```

    +   Array

        ```js
        require('sync-directory')(srcDir, targetDir, {
            exclude: [/node\_modules/]
        });
        ```

        ```js
        require('sync-directory')(srcDir, targetDir, {
            exclude: ['node_modules']
        });
        ```

+   `forceSync`

    ```js
    require('sync-directory')(srcDir, targetDir, {
        exclude: 'node_modules',
        forceSync(file) {
            // all files in "node_modules" will be synced event though "exclude" is configed
            return /node_modules/.test(file);
        }
    });
    ```


+   `supportSymlink`

    ```js
    // srcFolder:
    //     a/     a is symlink
    //      1.js

    // targetFolder:
    //     a/     a is not symlink
    //      1.js
    require('sync-directory')(srcDir, targetDir, {
        supportSymlink: false,
    });
    ```

    ```js
    // srcFolder:
    //     a/     a is symlink
    //      1.js

    // targetFolder:
    //     a/     a is the same symlink
    //      1.js
    require('sync-directory')(srcDir, targetDir, {
        supportSymlink: true,
    });
    ```

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