使用gulp展平父目录但不展平子目录

时间:2015-10-28 04:23:02

标签: javascript node.js gulp gulp-rename

我组织了我的代码,以便在文件夹名称上有一些关键字,例如_DIRECTIVE。我的目的是使用gulp.src("**/*_DIRECTIVE/**/*")编写一个识别文件夹命名模式的gulp任务。我想复制包含_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文件夹下的子文件夹变平。

请帮忙

1 个答案:

答案 0 :(得分:1)

您可以将gulp-rename与函数回调一起使用,以删除相应的父文件夹:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="plsCheck">A
    <input type="text"value=""/>
    <input type="text"value="" />
    <select>
        <option value="">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</div>
<div class="plsCheck">B
    <input type="text"value=""/>
    <input type="text"value="" />
    <select>
        <option value="">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</div>
<div class="plsCheck">C
    <input type="text"value=""/>
    <input type="text"value="" />
    <select>
        <option value="">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</div>
<div class="plsCheck">D
    <input type="text"value=""/>
    <input type="text"value="" />
    <select>
        <option value="">Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</div>
<button id="check">
Check
</button>
</button>

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

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 将相对于源模式的“base”传递文件名:

  • path.dirname

    gulp.src('src/**/*.js')src/a.js
    path.dirname == "a.js"src/nested/folder/b.js

  • path.dirname == "nested/folder/b.js"

    gulp.src('src/directives/**')src/directives/tab-directive/tabs.js

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

path.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)/, '');