mirror-directories
Usage
Example 1
Recursively copy all the directories matching the glob src/*
to out1
and out2
:
% mirror-directories -s 'src/*' -d out1 -d out2
If src/project1
and src/project2
exist then they will be copied to out1/project1
, out1/project2
, out2/project1
and out2/project2
.
Existing contents of the destination directories will be erased before the copying occurs such that the destination directories will mirror the source directories exactly.
Example 2
Recursively copy src1
and src2
to all the directories matching destinations/*
:
% mirror-directories -s src1 -s src2 -d 'destinations/*'
If destinations/birthday
and destinations/fear
exist then at the end destinations/{birthday,fear}/{src1,src2}
will be exact copies of their respective source directories.
Example 3
A trailing slash can be used to copy the contents of the source directory into the destination directory.
% mirror-directories -s 'src/' -d out
Creates out
as a mirror of src
instead of creating out/src
as a mirror of src
. This corresponds to the rename
option of the API.
Example 4
The -m
argument can be used to specify independent source/dest pairs.
% mirror-directories -m src:out -m friends/:enemies
This will mirror src
to out/src
and the contents of the friends
directory to the enemies
directory.
Example 5
The -m
argument can be used to specify multiple source directories for a single dest directory. Either all of none of the source directories must have a trailing slash. When all source directories have a trailing slash, meaning all of them relate to rename watches, files in latter source directories will take precedence over those specified earlier.
% mirror-directories -m src1/:src2/:out
In this circumstance if both src1/file
and src2/file
exist then out/file
will always mirror src2/file
. If -w
were used then any changes to src1/file
would be ignored. Removing src2/file
would lead to out/file
being a mirror of src1/file
until src2/file
is recreated.
Example 6
The -e
argument can be used to exclude directories
% mirror-directories -e blah -e hero/cat -m src:out -m friends/:enemies
This will mirror src
to out/src
and the contents of the friends
directory to the enemies
directory but will not mirror src/blah
or src/hero/cat
. If src/blah
and/or src/hero/cat
are directories then their contents would also be ignored.
Example 7
The -P
argument can be used to exclude paths matching a pattern (using micromatch
patterns internally with th matchBase
option set). It applies to each component in the entire source path (relative to the source directory), so ex*
would exclude all files and directories within the dir/exclude
directory and the file dir/include/exclude
.
% mirror-directories -P '*d' -m src:out
This will mirror src
to out/src
and will not copy any paths ending in the letter d
.
API
This library also exports an API:
import { mirrorDirectories, watchDirectoriesForChangesAndMirror } from 'mirror-directories'
// the default options are { keep: false, rename: false }
mirrorDirectories([
{ srcDirs: ['src1'], destDirs: ['dest1', 'dest2'] },
{ srcDirs: ['src2', 'src3'], destDirs: ['dest2', 'dest3'] },
])
const stopWatching = await watchDirectoriesForChangesAndMirror(
[{ srcDirs: ['src4'], destDirs: ['dest4', 'dest5'] }],
{
// keep existing contents in destination directories
keep: true,
// use watchman's "watch-project" command rather than "watch"
watchProject: true,
// mirrors `src4` to `dest4` and `dest5` instead of
// `dest4/src4` and `dest5/src4`
rename: true,
},
)
process.on('exit', stopWatching())