2

我组织了我的代码,以便它在文件夹名称上有一些关键字,例如_DIRECTIVE. 我的意图是编写一个识别文件夹命名模式的 gulp 任务,使用gulp.src("**/*_DIRECTIVE/**/*"). 我想复制包含_DIRECTVE关键字的所有文件夹(及其子文件夹)并将它们放在单独的文件夹中。

我有一个看起来像的文件夹结构

src
   |-folder-name_1_DIRECTVE/
   | |-subfolder/
   | | |-subsubfolder/
   | |   |-file.js
   | |
   | |-file.js
   |
   |-folder/
   | |-folder-name_2_DIRECTVE/
   |   |-subfolder/
   |   | |-subsubfolder/
   |   |   |-file.js
   |   |
   |   |-file.js
   |
   |
   |-folder/
     |-subfolderfolder/
       |-folder-name_3_DIRECTVE/
         |-subfolder/
         | |-subsubfolder/
         |   |-file.js
         |
         |-file.js

使用gulp我希望输出为

output
       |-folder-name_1_DIRECTVE/
       | |-subfolder/
       | | |-subsubfolder/
       | |   |-file.js
       | |
       | |-file.js
       |
       |-folder-name_2_DIRECTVE/
       | |-subfolder/
       | | |-subsubfolder/
       | |   |-file.js
       | |
       | |-file.js
       |
       |-folder-name_3_DIRECTVE/
         |-subfolder/
         | |-subsubfolder/
         |   |-file.js
         |
         |-file.js

我尝试使用gulp-flatten,但它没有帮助,因为它也使我文件夹下的子文件夹变平_DIRECTIVE

请帮忙

4

1 回答 1

2

您可以使用带有函数回调的gulp-rename来删除相应的父文件夹:

gulp.task('copy', function() {
  return gulp.src(['src/**/*_DIRECTIVE/**/*.js'])
    .pipe(rename(function(path) {
      path.dirname = path.dirname.replace(/^(.+[\\\/])?(?=[^\\\/]+_DIRECTIVE)/, '');
    }))
    .pipe(gulp.dest('output'));
});

假设与您的问题类似的文件夹结构,为清楚起见更改了文件名:

$ find src -type f
src/top-level_DIRECTIVE/directive1.js
src/top-level_DIRECTIVE/subfolder1/subsubfolder1/directive1-sub.js
src/folder-2/nested-2_DIRECTIVE/directive2.js
src/folder-2/nested-2_DIRECTIVE/subfolder2/subsubfolder2/directive2-sub.js
src/folder-3/double-nested/nested-3_DIRECTIVE/directive3.js
src/folder-3/double-nested/nested-3_DIRECTIVE/subfolder3/subsubfolder3/directive3-sub.js

$ gulp copy

$ find output -type f
output/top-level_DIRECTIVE/directive1.js
output/top-level_DIRECTIVE/subfolder1/subsubfolder1/directive1-sub.js
output/nested-2_DIRECTIVE/directive2.js
output/nested-2_DIRECTIVE/subfolder2/subsubfolder2/directive2-sub.js
output/nested-3_DIRECTIVE/directive3.js
output/nested-3_DIRECTIVE/subfolder3/subsubfolder3/directive3-sub.js

解释:

path.dirname将传递相对于源模式的“基础”的文件名:

  • 对于gulp.src('src/**/*.js')

    src/a.jspath.dirname == "a.js"
    src/nested/folder/b.jspath.dirname == "nested/folder/b.js"

  • 对于gulp.src('src/directives/**')

    src/directives/tab-directive/tabs.jspath.dirname == "tab-directive/tabs.js"

当您在重命名回调中更改传递的对象时,文件路径将附加到输出路径:

.pipe(gulp.rename(function (path) {
    path.basename = 'flat-' + path.basename;
});
// renames to:
output/some-directory/flat-file1.js
output/other-directory/flat-file2.js

要从路径中删除目录的父目录,请*_DIRECTIVE使用正则表达式:

path.dirname = path.dirname.replace(/(.+[\\\/])?(?=[^\\\/]+_DIRECTIVE)/, '');
/^(.+[\\\/])?(?=[^\\\/]+_DIRECTIVE)/

 ^                                    at the start of the string
  (        )?                         match an optional group
   .+                                 with 1 or more characters
     [\\\/]                           and ending with a directory separator (\ or /)
             (?=                  )   followed by a group which is excluded from the match
                [^\\\/]+              which has a folder-name (no \, /)
                        _DIRECTIVE    followed by a literal _DIRECTIVE
于 2016-04-01T03:02:37.330 回答