问题描述
我想使用 gulp 同步两个文件夹.我已经设法通过一个名为 gulp-directory-sync 的包来实现这一点(请参阅下面包含的任务),但现在我需要根据文件或目录名称是否以__"开头(两个下划线)从同步中排除一些文件).我试图找到一个具有忽略或排除功能的同步插件,但没有成功,所以我现在想知道是否有人知道解决此问题的方法.
I want to sync two folders using gulp. I have managed to achieve this with the a package called gulp-directory-sync (see included task below), but now I need to exclude some files from the sync based on if the file or directory name starts with "__" (two underscores). I have tried to find a sync plugin with an ignore or exclude feature, but without success, so I am now wondering if anyone might know a workaround for this.
var gulp = require('gulp');
var dirSync = require('gulp-directory-sync');
gulp.task('sync', function () {
return gulp.src('')
.pipe(dirSync('./one', './two'));
});
推荐答案
我总是非常怀疑那些在第一行就破坏 gulp-way 的插件".如果您不需要 gulp-src
中的文件,那么您可能不需要 gulp 来完成该任务,或者它还有其他问题.
I'd always be very suspicious of "plugins" who break the gulp-way in the very first line. If you don't need files in gulp-src
, you probably don't need gulp for that task, or there's something else fishy about it.
Gulp 在将文件复制到其他地方(包括异常)方面已经相当出色了,那么为什么不使用这个内置功能呢?使用 gulp-newer
插件,我们可以确保我们只复制那些已更新的文件:
Gulp is already pretty good in copying files to someplace else, including exceptions, so why not use this built-in functionality? With the gulp-newer
plugin we can make sure that we just copy those files that have been updated:
var gulp = require('gulp');
var newer = require('gulp-newer');
var merge = require('merge2');
gulp.task('sync', function(done) {
return merge(
gulp.src(['./one/**/*', '!./one/**/__*'])
.pipe(newer('./two'))
.pipe(gulp.dest('./two')),
gulp.src(['./two/**/*', '!./two/**/__*'])
.pipe(newer('./one'))
.pipe(gulp.dest('./one'))
);
});
这是带有一些注释的相同代码:
Here's the same code with some annotations:
var gulp = require('gulp');
var newer = require('gulp-newer');
var merge = require('merge2');
gulp.task('sync', function(done) {
return merge(
// Master directory one, we take all files except those
// starting with two underscores
gulp.src(['./one/**/*', '!./one/**/__*'])
// check if those files are newer than the same named
// files in the destination directory
.pipe(newer('./two'))
// and if so, copy them
.pipe(gulp.dest('./two')),
// Slave directory, same procedure here
gulp.src(['./two/**/*', '!./two/**/__*'])
.pipe(newer('./one'))
.pipe(gulp.dest('./one'))
);
});
这也可以解决问题,不需要插件 ;-)
That might also do the trick, no plugins required ;-)
更新
假设你只是想从 one
复制到 two
,并且你可能有一个 watcher 正在运行,你可以使用 gulp 中的deleted"事件来检查哪个文件消失了:
Assuming that you just want to copy from one
to two
, and that you might have a watcher running, you can use the "deleted" event from gulp to check which file has gone:
var gulp = require('gulp');
var newer = require('gulp-newer');
var path = require('path');
var del = require('del');
gulp.task('sync', function(done) {
return gulp.src(['./one/**/*', '!./one/**/__*'])
.pipe(newer('./two'))
.pipe(gulp.dest('./two'));
});
gulp.task('default', function() {
var watcher = gulp.watch('./one/**/*', ['sync']);
watcher.on('change', function(ev) {
if(ev.type === 'deleted') {
// path.relative gives us a string where we can easily switch
// directories
del(path.relative('./', ev.path).replace('one','two'));
}
});
});
这篇关于gulp 如何在忽略某些模式的同时使用 gulp 同步文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!