可以进行完整文件操作同步(增加、删除、更新)的 Gulp 插件

1,392 阅读1分钟

Sync file It keeps your files synced between two directory. In other words, any change of files in one directory will automatically take place in another one.

Installation

npm install --save-dev gulp-file-sync

Usage

var gulp = require('gulp'),
    fileSync = require('gulp-file-sync');

gulp.task('sync', function() {
  gulp.watch(['src/*.*'], function() {
    fileSync('src', 'dest', {recursive: false});
  });
});

API

fileSync('source directory', 'destination directory', options)

'source directory' and 'destination directory'

type: String

Any change of files in 'source directory' will automatically take place in 'destination directory'.

options.recursive

type: Boolean

default: true

Synchronize subdirectories recursively.

options.ignore

type: string or array or regex or function

Either a string, array, regex, or function to exclude some specific files. For example:

// ignore all .log files
fileSync('source directory', 'destination directory', {
  ignore: '.log'  
})
fileSync('source directory', 'destination directory', {
  ignore: [/^\.log$/i, '.cache'] // Exclude all .log and .cache files
})
fileSync('source directory', 'destination directory', {
  ignore: /^\.log$/i 
})
fileSync('source directory', 'destination directory', {
  ignore: function(dir, file) {
            return file === '.log';
          } 
})

options.addFileCallback

type: function(fullPathSrc, fullPathDest)

default:

var gutil = require('gulp-util');
function(fullPathSrc, fullPathDest) {
  gutil.log('同步增加文件到 ' + fullPathDest);
}

This function is called when file was added on source directory.

  • fullPathSrc - is the path of file that was added on source directory.
  • fullPathDest - is the path of file that was copied to destination directory.

options.deleteFileCallback

type: function(fullPathSrc, fullPathDest)

default:

var gutil = require('gulp-util');
function(fullPathSrc, fullPathDest) {
  gutil.log('同步删除文件 ' + fullPathDest);
}

This function is called when file was deleted on source directory.

  • fullPathSrc - is the path of file that was deleted on source directory.
  • fullPathDest - is the path of file that was deleted on destination directory.

options.updateFileCallback

type: function(fullPathSrc, fullPathDest)

default:

var gutil = require('gulp-util');
function(fullPathSrc, fullPathDest) {
  gutil.log('同步修改文件 ' + fullPathDest);
}

This function is called when file was updated on source directory.

  • fullPathSrc - is the path of file that was updated on source directory.
  • fullPathDest - is the path of file that was copied to destination directory.

options.beforeAddFileCallback

type: function(fullPathSrc)

This function is called before file is added on source directory.

  • fullPathSrc - is the path of file that was added on source directory.

options.beforeDeleteFileCallback

type: function(fullPathSrc)

This function is called before file is deleted on source directory.

  • fullPathSrc - is the path of file that was deleted on source directory.

options.beforeUpdateFileCallback

type: function(fullPathSrc)

This function is called before file is updated on source directory.

  • fullPathSrc - is the path of file that was updated on source directory.

Changelog

License

MIT (c) 2015 Kayo Lee (330956999@qq.com)