gulp如何在忽略某些模式的同时使用gulp同步文件夹 [英] gulp how to sync folders using gulp while ignoring some patterns

查看:106
本文介绍了gulp如何在忽略某些模式的同时使用gulp同步文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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-src中的文件,则可能不需要执行该任务,或者还有其他麻烦之处.

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,并且您可能正在运行观察程序,则可以使用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同步文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆