# grunt-sync

> Task to synchronize two directories. Similar to grunt-copy but updates only files that have been changed.

Latest version **0.8.2** (published 2020-01-14) · MIT license · 0 weekly downloads

## Install

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

## 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.8.2 |
| Published | 2020-01-14 |
| First published | 2013-02-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 <7 \|\| >=8 |
| Dependencies | 3 |
| Unpacked size | 19 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 76 |
| Author | Tomasz Drwiega |
| Maintainers | tomusdrw |
| Keywords | gruntplugin, sync, synchronize, copy |

## Links

- npm: https://www.npmjs.com/package/grunt-sync
- Repository: https://github.com/tomusdrw/grunt-sync
- Homepage: https://github.com/tomusdrw/grunt-sync.git
- Issues: https://github.com/tomusdrw/grunt-sync/issues
- npm.io page: https://npm.io/package/grunt-sync

## Dependencies (3)

- [glob](https://npm.io/package/glob.md) ^7.0.5
- [fs-extra](https://npm.io/package/fs-extra.md) ^6.0.1
- [md5-file](https://npm.io/package/md5-file.md) ^2.0.3

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 0.8.2 (latest) — 2020-01-14
- 0.8.1 — 2019-05-20
- 0.8.0 — 2018-06-22
- 0.7.0 — 2017-09-24
- 0.6.2 — 2016-08-09
- 0.5.2 — 2016-02-02
- 0.5.1 — 2015-12-11
- 0.5.0 — 2015-12-10
- 0.4.1 — 2015-08-31
- 0.4.0 — 2015-08-31
- 0.3.0 — 2015-08-21
- 0.2.4 — 2015-07-26
- 0.2.3 — 2015-01-11
- 0.2.2 — 2014-11-23
- 0.2.1 — 2014-11-08
- … 10 more at https://npm.io/package/grunt-sync/versions

## README

# Grunt-sync

A [grunt](http://github.com/gruntjs/grunt/) task to keep directories in sync.
It is very similar to [grunt-contrib-copy](https://github.com/gruntjs/grunt-contrib-copy) but
tries to copy only those files that has actually changed.


## Deprecation notice

This package is not actively developed any more.

## Usage

```bash
npm install grunt-sync --save
```

Within your grunt file:

```javascript
grunt.initConfig({
  sync: {
    main: {
      files: [{
        cwd: 'src',
        src: [
          '**', /* Include everything */
          '!**/*.txt' /* but exclude txt files */
        ],
        dest: 'bin',
      }],
      pretend: true, // Don't do any IO. Before you run the task with `updateAndDelete` PLEASE MAKE SURE it doesn't remove too much.
      verbose: true // Display log messages when copying files
    }
  }
});

grunt.loadNpmTasks('grunt-sync');
grunt.registerTask('default', 'sync');
```

## More examples
```javascript
sync: {
  main: {
    files: [
      {src: ['path/**'], dest: 'dest/'}, // includes files in path and its subdirs
      {cwd: 'path/', src: ['**/*.js', '**/*.css'], dest: 'dest/'}, // makes all src relative to cwd
    ],
    verbose: true, // Default: false
    pretend: true, // Don't do any disk operations - just write log. Default: false
    failOnError: true, // Fail the task when copying is not possible. Default: false
    ignoreInDest: "**/*.js", // Never remove js files from destination. Default: none
    updateAndDelete: true, // Remove all files from dest that are not found in src. Default: false
    compareUsing: "md5" // compares via md5 hash of file contents, instead of file modification time. Default: "mtime"

  }
}
```

## Installation
```
npm install grunt-sync --save
```

## How it works?
In the first phase the plugin compares modification times of files in `src` and `dest`. It only copies files with newer modification time. Second phase deletes files that exists in `dest` but have not been found in `src`.

Details:

1. [1st phase] Read modification time of all files in `src`.
1. [1] Overwrite destination if modification time is newer or destination is directory not file.
1. [2nd phase]* Get a list of the files in `dest` and calculate difference between destination and source.
1. [2]* Delete all files (and directories) that have been found in `dest` but are not found `src` excluding ignored files.

*second phase only occurs if updateAndDelete is set to true (whitch is set to false by default)

## Changelog
* 0.7.0 - Pass down grunt-copy options
* 0.6.2 - Bumped major versions of important dependencies
* 0.5.0 - Synchronous removal of directories (to avoid race conditions)
* 0.4.0 - Adding better patterns support for `ignoreInDest`
* 0.3.0 - Comparison using md5 hash of file contents or modification time
* 0.2.4 - `failOnError` option
* 0.2.3 - Fixed issue with files defined as array when using `updateAndDelete`.
* 0.2.2 - Fixed issue with `updateAndDelete` when source patterns matches only files.
* 0.2.1 - Fixed grunt Compact Format.
* 0.2.0 - Default configuration will not remove any files any more. You have to specify `updateAndDelete` option to remove any files from destination.
* 0.1.2 - Deleting all files in destination on Windows solved.
* 0.1.1 - Fixed issue with trailing slash in destination.
* 0.1.0 - Files missing that are not in `src` are deleted from `dest` (unless you specify `updateOnly`)


## Migration 0.1.x -> 0.2.x
In version 0.2 you have to explicitly specify that you want the plugin to remove files from destination. See `updateAndDelete` option and run with `pretend:true` first to make sure that it doesn't remove any crucial files. You can tune what files should be left untouched with `ignoreInDest` property.

If you have `updateOnly:true` in your 0.1 config you can remove this option. For those who used `updateOnly:false` you have to include `updateAndDelete:true` in 0.2 config to keep the same behavior.


## Contributors
* Michael Mifsud ([xzyfer](https://github.com/xzyfer))
* Erwan Jegouzo ([erwanjegouzo](https://github.com/erwanjegouzo))
* Janek Lasocki-Biczysko ([janeklb](https://github.com/janeklb))


## TODO
* Research if it's possible to have better integration with `grunt-contrib-watch` - update only changed files instead of scanning everything.
* Some tests for common problems
* Some tests to assure performance
* Rewrite `updateAndDelete` in more elegant way (maybe use patterns from source?)

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