Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
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.
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 });
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 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 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.
A module for repeated efficient synchronizing two directories.
// input/a/{a.js,b.js}
// output/
var tree = new TreeSync('input', 'output');
tree.sync();
// output is now contains copies of everything that is in input
fs.unlink('/input/a/b.js');
// input / output have diverged
tree.sync();
// difference is calculated and efficient patch to update `output` is created and applied
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.
// Assume the following folder structure...
// input/
// foo/
// foo.js
// bar.js
// bar/
// foo-bar.js
var tree = new TreeSync('input', 'output', {
ignore: ['**/b']
});
tree.sync();
// We now expect output to contain foo/, but not bar/.
// Any changes made to bar/ won't be synced to output, and the contents of bar/ won't be traversed.
// Assume the following folder structure...
// input/
// foo/
// foo.js
// bar.js
// bar/
// foo-bar.js
var tree = new TreeSync('input', 'output', {
globs: ['foo', 'foo/bar.js']
});
tree.sync();
// We now expect output to contain foo/bar.js, but nothing else.
// Be careful when using this property! You'll need to make sure that if you're including a file, it's parent
// path is also included, or you'll get an error when tree-sync tries to copy the file over.
FAQs
A module for repeated efficient synchronizing two directories.
The npm package tree-sync receives a total of 185,703 weekly downloads. As such, tree-sync popularity was classified as popular.
We found that tree-sync demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.