What is merge-trees?
The merge-trees npm package is used to merge multiple directories into one. It is particularly useful in build systems and asset pipelines where you need to combine the contents of several directories into a single output directory.
What are merge-trees's main functionalities?
Basic Directory Merge
This feature allows you to merge multiple directories into a single output directory. In this example, the contents of 'src' and 'lib' directories are merged into the 'output' directory.
const MergeTrees = require('merge-trees');
const tree = new MergeTrees(['src', 'lib'], { overwrite: true });
tree.merge('output');
Custom Merge Function
This feature allows you to define a custom merge function to handle conflicts or special merging logic. The customMerge function is called for each file that needs to be merged.
const MergeTrees = require('merge-trees');
const tree = new MergeTrees(['src', 'lib'], {
overwrite: true,
customMerge: (srcDir, destDir, relativePath) => {
// Custom merge logic here
}
});
tree.merge('output');
Excluding Files
This feature allows you to exclude certain files from being merged. In this example, all files with a .test.js extension are excluded from the merge.
const MergeTrees = require('merge-trees');
const tree = new MergeTrees(['src', 'lib'], {
overwrite: true,
exclude: ['**/*.test.js']
});
tree.merge('output');
Other packages similar to merge-trees
broccoli-merge-trees
broccoli-merge-trees is a similar package used in the Broccoli build system. It provides similar functionality for merging directories but is specifically designed to work within the Broccoli ecosystem.
gulp-merge
gulp-merge is a plugin for the Gulp build system that allows you to merge multiple streams into one. While it is not specifically for merging directories, it can be used to achieve similar results in a Gulp-based workflow.
webpack-merge
webpack-merge is a utility for merging webpack configurations. While it is not directly related to merging directories, it provides similar functionality for combining multiple configuration objects, which can be useful in complex build setups.
node-merge-trees
Symlink or copy multiple trees of files on top of each other, resulting in a single merged tree.
Optimized for repeated (incremental) merging.
Installation
npm install --save merge-trees
Usage
-
new MergeTrees(inputPaths, outputPath, options)
:
-
inputPaths
: An array of paths to the input directories
-
outputPath
: The path to the output directory. Must exist and be empty.
-
options
: A hash of options
-
mergeTrees.merge()
: Merge the input directories into the output directory.
Can be called repeatedly for efficient incremental merging.
Options
-
overwrite
: By default, node-merge-trees throws an error when a file
exists in multiple nodes. If you pass { overwrite: true }
, the output
will contain the version of the file as it exists in the last input
directory that contains it.
-
annotation
: A note to help with logging.
Example
var MergeTrees = require('merge-trees');
var mergeTrees = new MergeTrees(
['public', 'scripts'],
'output-dir',
{ overwrite: true });
mergeTrees.merge()
mergeTrees.merge()
Say the directory structure is as follows:
.
├─ public
│ ├─ index.html
│ └─ images
│ └─ logo.png
├─ scripts
│ └─ app.js
├─ output-dir
…
Running mergeTrees.merge()
will generate this folder:
.
├─ …
└─ output-dir
├─ app.js
├─ index.html
└─ images
└─ logo.png
The parent folders, public
and scripts
in this case, are not included in the output. The output tree contains only the files within each folder, all mixed together.
Contributing
Clone this repo and run the tests like so:
npm install
npm test
Issues and pull requests are welcome. If you change code, be sure to re-run
npm test
. Oftentimes it's useful to add or update tests as well.