【问题标题】:how to make gulp.dest dynamic如何使 gulp.dest 动态化
【发布时间】:2014-05-31 03:54:56
【问题描述】:

我正在学习使用 gulp。我采用了一个示例场景,在该场景中,我尝试根据名称复制文件,如果奇怪则将其移动到奇数文件夹,否则将其移动到偶数。但是有些目标文件夹搞砸了。这是文件夹结构和代码。

1
--my
----new
-----2.txt
----1.txt
----2.txt


    g = gulp.src '**/*.txt', cwd: '1'
    g.pipe map (file,cb)->
        filename = path.basename file.path, path.extname(file.path)
        if filename % 2 
            dest = 'odd'
        else 
            dest = 'even' 
        debugger
        console.log 'destination ',dest
        g.pipe gulp.dest dest
        cb null,file

它正在将偶数文件名复制到奇数文件夹。它是关于被记住的目标文件夹(我认为是关闭)

【问题讨论】:

    标签: coffeescript gulp node.js-stream


    【解决方案1】:

    如果你实际上只有两个输出目标,最简单的解决方案可能是使用the gulp-if plugin,就像这样(我不使用 CoffeeScript,所以你必须自己转换它):

    var _if = require('gulp-if');
    
    function fileIsOdd(file) {
        return path.basename(file.path, path.extname(file.path)) % 2;
    }
    
    // in your task
    gulp.src('**/*.txt', {cwd: '1'})
        .pipe(_if(fileIsOdd, gulp.dest('odd'), gulp.dest('even')))
    

    它的作用是使用fileIsOdd 函数处理单个文件。如果函数返回真值,则处理第一个参数,否则处理第二个参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      相关资源
      最近更新 更多