使用gulp选择和移动目录和它们的文件

时间:2021-10-20 04:42:56

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/目录,并将适当的文件移动到clean目录。我希望用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 dist results 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/文件夹中?

2 个解决方案

#1


147  

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

您需要包含src的基本选项,它将按照您希望的方式保存文件结构:

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.

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

If you can, I'd recommend you use a single src/ folder and move all your 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.

如果您这样做,那么简单地替换所有发生的。/使用src/在上面的例子中。

#2


1  

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

最初的问题只针对咽部的目录(即文件夹)。src,即饮而尽。src([_local,…在本例中,_locales是目录的名称。

The accepted answer uses a glob pattern in its gulp.src to target files anywhere in those directories, i.e. gulp.src(['./_locales/**/*.*', ..., (notice the double-asterisks, and the filename.extension asterisks). The accepted answer works...

公认的答案是大口大口地喝。src将目标文件放在这些目录中的任何位置,即gulp.src(['./_locales/**/*)。*’,……注意双星号和文件名。扩展星号)。接受答案的作品……

...but the accepted answer only emphasizes the base option:

…但公认的答案只强调基本选项:

You need to include the base option to src...

你需要包括基本选项到src…

I experimented and found:

我尝试,发现:

  1. Strictly speaking, it is unnecessary to use the base option to achieve what the OP asked: "...and moves the appropriate files to the clean directory." The base option does indeed preserve the folder+file structure (as described in the accepted answer), but the base option is not sufficient to 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...

    严格地说,没有必要使用基本选项来实现OP所要求的:“……”并将适当的文件移动到clean目录。基本选项确实保留了文件夹+文件结构(如所接受的答案所述),但是基本选项不足以按照OP的要求移动文件。保存文件夹+文件结构可能是OP所期望的,所以公认的答案是好的,但是……

  2. Just to reiterate what does move the files, it's the glob patterns:

    再重申一下是什么移动了文件,这是glob模式:

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

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

    2. Filename.extension asterisks (.../*.*) finds files of 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 ./

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

Let me know if I'm mistaken...

如果我错了,请告诉我……

#1


147  

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

您需要包含src的基本选项,它将按照您希望的方式保存文件结构:

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.

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

If you can, I'd recommend you use a single src/ folder and move all your 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.

如果您这样做,那么简单地替换所有发生的。/使用src/在上面的例子中。

#2


1  

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

最初的问题只针对咽部的目录(即文件夹)。src,即饮而尽。src([_local,…在本例中,_locales是目录的名称。

The accepted answer uses a glob pattern in its gulp.src to target files anywhere in those directories, i.e. gulp.src(['./_locales/**/*.*', ..., (notice the double-asterisks, and the filename.extension asterisks). The accepted answer works...

公认的答案是大口大口地喝。src将目标文件放在这些目录中的任何位置,即gulp.src(['./_locales/**/*)。*’,……注意双星号和文件名。扩展星号)。接受答案的作品……

...but the accepted answer only emphasizes the base option:

…但公认的答案只强调基本选项:

You need to include the base option to src...

你需要包括基本选项到src…

I experimented and found:

我尝试,发现:

  1. Strictly speaking, it is unnecessary to use the base option to achieve what the OP asked: "...and moves the appropriate files to the clean directory." The base option does indeed preserve the folder+file structure (as described in the accepted answer), but the base option is not sufficient to 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...

    严格地说,没有必要使用基本选项来实现OP所要求的:“……”并将适当的文件移动到clean目录。基本选项确实保留了文件夹+文件结构(如所接受的答案所述),但是基本选项不足以按照OP的要求移动文件。保存文件夹+文件结构可能是OP所期望的,所以公认的答案是好的,但是……

  2. Just to reiterate what does move the files, it's the glob patterns:

    再重申一下是什么移动了文件,这是glob模式:

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

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

    2. Filename.extension asterisks (.../*.*) finds files of 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 ./

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

Let me know if I'm mistaken...

如果我错了,请告诉我……