What is tree-sync?
The tree-sync npm package is designed to synchronize directories, ensuring that the contents of one directory match another. It is particularly useful for build systems and deployment scripts where maintaining consistent directory structures is crucial.
What are tree-sync's main functionalities?
Synchronize Directories
This feature allows you to synchronize the contents of a source directory with a destination directory. The code sample demonstrates how to use TreeSync to ensure that the destination directory matches the source directory.
const TreeSync = require('tree-sync');
const sourceDir = 'path/to/source';
const destDir = 'path/to/destination';
const tree = new TreeSync(sourceDir, destDir);
tree.sync();
Custom Filter Function
This feature allows you to provide a custom filter function to exclude certain files or directories from being synchronized. The code sample shows how to exclude files with a .tmp extension from the synchronization process.
const TreeSync = require('tree-sync');
const sourceDir = 'path/to/source';
const destDir = 'path/to/destination';
const tree = new TreeSync(sourceDir, destDir);
tree.sync({ filter: (relativePath) => !relativePath.endsWith('.tmp') });
Dry Run Mode
This feature allows you to perform a dry run to see what changes would be made without actually applying them. The code sample demonstrates how to use the dry run mode to preview the synchronization changes.
const TreeSync = require('tree-sync');
const sourceDir = 'path/to/source';
const destDir = 'path/to/destination';
const tree = new TreeSync(sourceDir, destDir);
tree.sync({ dryRun: true });
Other packages similar to tree-sync
rsync
rsync is a widely-used utility for keeping directories in sync. It is highly efficient and supports a variety of options for file transfer and synchronization. Unlike tree-sync, rsync is a command-line tool and not a Node.js package, but it can be used in Node.js scripts via child processes.
fs-extra
fs-extra is a Node.js package that extends the native fs module with additional methods for file and directory manipulation. While it does not provide a direct synchronization feature like tree-sync, it offers methods like copy and move that can be used to achieve similar results.
chokidar
chokidar is a Node.js package for watching file and directory changes. It can be used to trigger synchronization actions when changes are detected. While it does not perform synchronization itself, it can be combined with other tools to achieve directory synchronization.
TreeSync
A module for repeated efficient synchronizing two directories.
const tree = new TreeSync('input', 'output');
tree.sync();
fs.unlink('/input/a/b.js');
tree.sync();
Under the hood, this library uses walk-sync to traverse files and folders. You may optionally pass in options to selectively include or ignore files and directories. These whitelisted properties (ignore
and globs
) will be passed to walk-sync.
const tree = new TreeSync('input', 'output', {
ignore: ['**/b']
});
tree.sync();
const tree = new TreeSync('input', 'output', {
globs: ['foo', 'foo/bar.js']
});
tree.sync();