Gulp src glob:多个文件模式不匹配

时间:2016-09-19 09:54:00

标签: gulp glob

我正在为Node应用程序创建构建脚本。

我创建了一个Powershell(PSake)脚本,但现在我需要将它移植到Gulp,因为我需要在Mac上运行它。

基本上,我将源代码复制并清理它们(删除所有不必要的文件,如readme和amp),以创建一个将安装在客户端PC上的软件包,因此我希望尽可能减少文件编号。

在一个地方,我的Powershell看起来像这样:

Get-ChildItem "$srcout\node_modules\" -Recurse | ? {
    $_.FullName -match "\\\.bin\\" `
        -or $_.Name -match "[\w]+\.md$" `
        -or $_.Name -match "licen[c|s]e" `
        -or $_.Name -match "authors" `
        -or $_.Name -match "bower.json" `
        -or $_.Name -match "gruntfile\.js" `
        -or $_.Name -match "makefile" `
        -or $_.Name -match "cakefile"
    } | % {
        Remove-Item "$($_.FullName)" -Force -Recurse
    }

到目前为止,我已经为Gulp写了这个:

var pump = require('pump');
var through = require('through2');

    pump([
        gulp.src([
            '**/node_modules/**/.bin/',
            '**/node_modules/**/*.md',
            '**/node_modules/**/licen+(s|c)e*',
            '**/node_modules/**/author*',
            '**/node_modules/**/bower.json',
            '**/node_modules/**/gruntfile.js',
            '**/node_modules/**/makefile',
            '**/node_modules/**/cakefile'
        ], { 
            cwd: srcout,
            nocase: true
        }),
        through.obj(function(f, e, cb) {

            if (fs.statSync(f.path).isFile()) {
                fs.unlinkSync(f.path);
            } else {
                rmdir.sync(f.path);
            }

            cb(null, f);
        })
    ],
    done);

/.bin/*.md整合都很好,但其余的都找不到......

我错过了什么或做错了什么?

谢谢

1 个答案:

答案 0 :(得分:1)

我已经通过fs-extra walk调用替换了glob,但它并没有真正回答为什么glob不起作用。

var fs = require('fs-extra');

gulp.task('cleanupapp', [ 'build' ], function(done) {
    fs.walk(path.join(srcout, 'node_modules'))
    .on('data', function (item) {

        // normalize folder paths (win/mac)
        var f = item.path.replace('/', '\\');

        if (f.match(/\\.bin\\/i) ||
            f.match(/\.md$/i) ||
            f.match(/licen[c|s]e/i) ||
            f.match(/author[s]?/i) ||
            f.match(/bower\.json$/i) ||
            f.match(/gruntfile\.js$/i) ||
            f.match(/makefile$/i) ||
            f.match(/cakefile$/i)) {

            if (fs.accessSync(f, fs.W_OK)) {
                fs.removeSync(f);
            }
        }
    })
    .on('end', function () {
        done();
    });
});