如何用gulp同步本地文件夹?

时间:2019-07-27 10:23:56

标签: javascript node.js gulp

我正在使用WordPress插件,并将所有文件保存在我的工作目录中,并在该项目文件夹中运行gulp。现在,我想执行一个监视任务,将所有更改复制到本地WP安装中进行测试。

因此,我正在寻找一种方法(仅在一个方向上)将项目文件夹与WP的插件文件夹同步。

我设法使其与gulp-directory-sync一起使用

assetable_type

但是,该插件已经四年没有更新了,根据answer,它没有按照正确的“ gulp方式”(例如不使用gulp-src)进行操作,因此应该可以完成此任务以及其他基本的功能。

复制更改的文件非常容易,但是跟踪已删除的文件也更加复杂。我也希望只更新已更改/删除/新的文件,而不是每次都在处理所有文件之前清除文件夹。

从上述答案中的更新代码开始,我尝试实现它并进行了更改以使其起作用。

...

var dirSync = require("gulp-directory-sync");

var localDir = "../newDir/";
var buildDir = "./buildDir/";

...

function copy_to_local_folder() {
    return pipeline(
        gulp.src(buildDir+'**/*'),
        dirSync( buildDir, localDir, { printSummary: true } )
    );
}

function watch_local() {
    gulp.watch(buildDir+'**/*', copy_to_local_folder);


exports.default = watch_local;

使用此代码,当我更改或删除文件时,该文件夹会更新,但是当我删除整个文件夹时,它不会触发。这可能是因为我使用... var newer = require("gulp-newer"); var pipeline = require("readable-stream").pipeline; var del = require("del"); var localDir = "../newDir/"; var buildDir = "./buildDir/"; function copy_to_local_folder() { return pipeline( gulp.src([buildDir+'**/*']), newer(localDir), gulp.dest(localDir), ); } function watch_local() { var watcher = gulp.watch(buildDir + '**/*', copy_to_local_folder ); watcher.on('unlink', function(path) { console.log(path); var newPath = './'+path; newPath = newPath.replace(buildDir, localDir); console.log(newPath); (async () => { const deletedPaths = await del(newPath, {dryRun: true, force: true}); console.log('Deleted files and directories:\n', deletedPaths.join('\n')); })(); }); } exports.default = watch_local; 而不是unlink。但是,即使我使用下面的函数版本,删除文件夹(包含文件)也不会触发它。

unlinkDir

我做错了什么?
还是总有一种更好的方法来实现这一目标?

PS:我正在使用
节点v11.15.0
gulp v4.0.2

0 个答案:

没有答案