how to make gulp.dest dynamic

coool

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)

OverZealous

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make Gulp wait until dest file is finished?

From Dev

How to ignore **/* in gulp.dest()?

From Dev

gulp - make gulp.pipe(gulp.dest) sync

From Dev

how base option affects gulp.src & gulp.dest

From Dev

Gulp dest.on is not a function

From Dev

How to locate the path of parent directory in gulp.dest()?

From Dev

How to set gulp.dest() in same directory as pipe inputs?

From Dev

How to locate the path of parent directory in gulp.dest()?

From Dev

How do I gulp.src an entire folder and its subfolders, and then gulp.dest based on files' location?

From Dev

How to use source file's path from gulp.src in gulp.dest?

From Dev

gulp.dest() ignoring subdirectory

From Dev

Gulp TypeError: dest.on is not a function

From Dev

Modify path of gulp.dest

From Dev

How to make a gulp task with param?

From Dev

Gulp - How to collect used files only and convert them as single files in `dest`

From Dev

Gulp - How to collect used files only and convert them as single files in `dest`

From Dev

Grunt dynamic dest location sass

From Dev

gulp.dest not creating destination folder

From Dev

Passing the output of an asynchronous function to gulp.dest?

From Dev

Gulp imagemin dest.on is not a function error

From Dev

gulp.dest() does not result in file updates

From Dev

gulp.dest does not print to file

From Dev

How to make a for loop dynamic?

From Dev

How to make a dynamic ImageView?

From Dev

How to make a for loop dynamic?

From Dev

How to make this code dynamic

From Dev

How to make a dynamic char?

From Dev

gulp pipe(gulp.dest()) does not create any results

From Dev

How to terminate ping <dest> &