102

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.

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 dist results in the the dist/ directory being populated with the correct directories but they are all empty

$ 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?

4

2 回答 2

168

您需要包含 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'));
});

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

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

如果您这样做,则只需将上面示例中所有出现的 替换./src/

于 2014-02-04T15:51:41.393 回答
6

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

接受的答案 在其中使用一种glob模式gulp.src来定位这些目录中任何位置的文件,即gulp.src(['./_locales/**/*.*', ...,(注意双星号filename.extension星号)。接受的答案有效...

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

您需要包含src的base选项...

我进行了实验,发现:

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

  2. 只是重申移动文件的原因,它是模式glob

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

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

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

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

于 2017-03-30T05:22:35.523 回答