Question

I am learning to use gulp.I took a eg scenario in which I tried to copy the files based on the name if it is odd move it to odd folder otherwise move it to even. But some where destination folder is messed up. here is the folder structure and code.

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

It is copying the even file names to odd folder.It is about the destination folder being remembered(closure I think)

Était-ce utile?

La solution

If you literally only have two output destinations, the simplest solution might be to use the gulp-if plugin, like so (I don't use CoffeeScript, so you'll have to convert it yourself):

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')))

What this does is process the individual file with the fileIsOdd function. If the function returns a truthy value, then the first argument is processed, otherwise the second argument is processed.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top