node.js 使用 gulp 选择和移动目录及其文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21546931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:48:07  来源:igfitidea点击:

Use gulp to select and move directories and their files

node.jsglobgulp

提问by makenova

I'm currently using gulp to call a bash script that cleans my dist/directory and moves the appropriate files to the clean directory. I would like this to be done with gulp because I am not sure the script would work on a non *nix file system.
So far, I'm using the gulp-clean module to clean the dist/directory but when I try to move the required directories and their files to the dist folder, the directories are empty.

我目前正在使用 gulp 调用一个 bash 脚本来清理我的dist/目录并将适当的文件移动到干净的目录中。我希望使用 gulp 完成此操作,因为我不确定该脚本是否可以在非 *nix 文件系统上运行。
到目前为止,我正在使用 gulp-clean 模块来清理dist/目录,但是当我尝试将所需的目录及其文件移动到 dist 文件夹时,这些目录是空的。

var gulp = require('gulp'),
    clean = require('gulp-clean');

gulp.task('clean', function(){
  return gulp.src(['dist/*'], {read:false})
  .pipe(clean());
});

gulp.task('move',['clean'], function(){
  gulp.src(['_locales', 'icons', 'src/page_action', 'manifest.json'])
  .pipe(gulp.dest('dist'));
});

gulp.task('dist', ['move']);

calling gulp distresults in the the dist/directory being populated with the correct directories but they are all empty

调用gulp dist结果在dist/目录中填充了正确的目录,但它们都是空的

$ ls dist/*
dist/manifest.json

dist/_locales:

dist/icons:

dist/page_action:

How do I copy the directories and their contents to the dist/folder?

如何将目录及其内容复制到dist/文件夹?

回答by OverZealous

You need to include the baseoptionto src, which will preserve the file structure the way you want:

您需要在src 中包含该base选项,这将按照您想要的方式保留文件结构:

var filesToMove = [
        './_locales/**/*.*',
        './icons/**/*.*',
        './src/page_action/**/*.*',
        './manifest.json'
    ];

gulp.task('move',['clean'], function(){
  // the base option sets the relative root for the set of files,
  // preserving the folder structure
  gulp.src(filesToMove, { base: './' })
  .pipe(gulp.dest('dist'));
});


Also, you are probably going to have trouble down the road if you have all these source files in the root of your project.

此外,如果您在项目的根目录中拥有所有这些源文件,那么您可能会遇到麻烦。

Ifyou can, I'd recommend you use a single src/folder and move allyour application-specific files into there. This makes maintenance easier moving forward, and prevents your build-specific files from getting mixed up with your application-specific files.

如果可以,我建议您使用单个src/文件夹并将所有特定于应用程序的文件移动到那里。这使得维护更容易向前推进,并防止您的构建特定文件与您的应用程序特定文件混淆。

If you do this, then simply replace all occurrences of ./with src/in the example above.

如果你这样做,那么只需替换上面例子中所有出现的./with src/

回答by The Red Pea

The original question targets only directories(a.k.a. folders) in its gulp.src, i.e. gulp.src(['_locales', ...in this example, _localesis the name of a directory.

原始问题仅针对其 中的目录(又名文件夹)gulp.src,即gulp.src(['_locales', ...在此示例中,_locales目录的名称。

The accepted answer uses a globpatternin its gulp.srcto target filesanywhere in those directories, i.e. gulp.src(['./_locales/**/*.*', ..., (notice the double-asterisks, and the filename.extensionasterisks). The accepted answer works...

接受的答案使用glob模式中的gulp.src目标文件,这些目录中的任何地方,即gulp.src(['./_locales/**/*.*', ...,(注意双星号,以及filename.extension星号)。接受的答案有效...

...but the accepted answer only emphasizes the baseoption:

...但接受的答案只强调base选项

You need to include the baseoptionto src...

您需要包含srcbase选项...

I experimented and found:

我试验并发现:

  1. Strictly speaking, it is unnecessary to use the baseoption to achieve what the OP asked: "...and moves the appropriate filesto the clean directory." The baseoption doesindeed preserve the folder+filestructure(as described in the accepted answer), but the baseoption is not sufficientto move the files as the OP asked. Preserving the folder+file structure is probably what the OP expects, so the accepted answer is good, but...

  2. Just to reiterate what doesmove the files, it's the globpatterns:

    1. Double-asterisk (.../**/...) searches recursively throughout all subfolders, and subfolders' subfolders', etc.

    2. Filename.extension asterisks (.../*.*) finds filesof all names, and all extensions. So I think this part deserves the most emphasis!

  3. The accepted answer changes something else; it adds a prefix of ./to each path arguments passed to gulp.src. I think that's unnecessary/redundant; if there is no ./, (as in the OP question), the paths are resolved relative to the current directory -- resulting in the same behavior. But maybe it's good practice to be explicit with the ./

  1. 严格来说,没有必要使用该base选项来实现 OP 所要求的:“......并将适当的文件移动到干净的目录。” 该base选项确实保留文件夹+文件结构(如接受的答案所述),但base选项是不充分的OP移动的文件要求。保留文件夹+文件结构可能是 OP 所期望的,所以接受的答案是好的,但是......

  2. 只是为了重申移动文件,它的glob模式:

    1. 双星号 ( .../**/...) 递归搜索所有子文件夹,以及子文件夹的子文件夹等。

    2. Filename.extension asterisks( .../*.*) 查找所有名称和所有扩展名的文件所以我认为这部分最值得强调!

  3. 接受的答案改变了其他东西;它./为传递给的每个路径参数添加一个前缀gulp.src。我认为这是不必要的/多余的;如果没有./,(如在 OP 问题中),路径将相对于当前目录进行解析 -导致相同的行为。但也许明确指出./

Let me know if I'm mistaken...

如果我弄错了,请告诉我...