sync-directory can sync files from src directory to target directory.
We have two ways to sync files: hardlink and copy.
If type is copy, sync-directory will copy files from src directory to target directory.
If type is hardlink, sync-directory can create hardlink files in target directory from src directory.
Apparently, the type hardlink is quicker than type copy, and sync-directory uses hardlink by default.
npm i sync-directory -gsyncdir <from> <to> [options]Example: syncdir aaa bbb -w
options:
-
-w, --watchWatch changes.
falseas default.Same as api
watch. -
--quietDisable unnecessary logs.
-
-do, --deleteOrphanedDelete orphaned files/folders in target folder.
falseas default.Same as api
deleteOrphaned. -
-c, --copySync with type
copy,hardlinkas default.Same as api
type: 'copy'. -
-symlink, --symlinksupport symlink while sync running.
falseas default.Same as api
supportSymlink.
require('sync-directory')(srcDir, targetDir[, config]);-
parames
name description type values default srcDirsrc directory String absolute path - targetDirtarget directory String absolute path - config.watchwatch files change Boolean - false config.chokidarWatchOptionswatch options (chokidar is used for watching) Object - {}config.typeway to sync files String 'copy' / 'hardlink''hardlink'config.deleteOrphanedDecide if you want to delete other files in targetDir when srcDir files are removed Boolean - true config.afterSynccallback function when files synced Function - blank function config.supportSymlinkensure symlink in target if src has symlinks Boolean - false config.excludefiles that should not sync to target directory. RegExp / String / Array (item is RegExp / String) - null config.forceSyncsome files must be synced even though 'excluded' Function - (file) => { return false }config.filtercallback function to filter synced files. Sync file when returning trueFunction - filepath => trueconfig.onErrorcallback function when something wrong Function - (err) => { throw new Error(err) } -
return
const watcher = require('sync-directory')(A, B);
watcherisundefined.const watcher = require('sync-directory')(A, B, { watch: true });
watcheris a chokidar watcher.
-
watchrequire('sync-directory')(srcDir, targetDir, { watch: true });
-
chokidarWatchOptionsrequire('sync-directory')(srcDir, targetDir, { chokidarWatchOptions: { awaitWriteFinish: { stabilityThreshold: 2000, pollInterval: 100 } }, });
-
afterSyncrequire('sync-directory')(srcDir, targetDir, { afterSync({ type, relativePath }) { // type: init:hardlink / init:copy / add / change / unlink / unlinkDir // relativePath: relative file path } });
-
typecopy
require('sync-directory')(srcDir, targetDir, { type: 'copy' });
hardlink (default)
require('sync-directory')(srcDir, targetDir);
-
excludeexclude
node_modules-
String
require('sync-directory')(srcDir, targetDir, { exclude: 'node_modules' });
-
RegExp
require('sync-directory')(srcDir, targetDir, { exclude: /node\_modules/ });
-
Array
require('sync-directory')(srcDir, targetDir, { exclude: [/node\_modules/] });
require('sync-directory')(srcDir, targetDir, { exclude: ['node_modules'] });
-
-
forceSyncrequire('sync-directory')(srcDir, targetDir, { exclude: 'node_modules', forceSync(file) { // all files in "node_modules" will be synced event though "exclude" is configed return /node_modules/.test(file); } });
-
supportSymlink// srcFolder: // a/ a is symlink // 1.js // targetFolder: // a/ a is not symlink // 1.js require('sync-directory')(srcDir, targetDir, { supportSymlink: false, });
// srcFolder: // a/ a is symlink // 1.js // targetFolder: // a/ a is the same symlink // 1.js require('sync-directory')(srcDir, targetDir, { supportSymlink: true, });