mercredi 18 février 2015

Gulp - copy and rename a file


I'm extremely new to Gulp. I'm basically trying to watch for a modified JavaScript file, and then make a new copy of it with a new name. (eventually there'll be some processing on it, but Rome wasn't built in a day).


My (naive) attempt is this:



gulp.task('default', function() {

return gulp.watch('../**/**.js', function(obj){
gulp.src(obj.path)
.pipe(gulp.dest('foobar.js'));
});

});


This takes the modified file and successfully copies it into a folder now called foobar.js. Is there anything simple I can replace gulp.dest('foobar.js') with that will simply copy and rename the src file in place?




EDIT


By copy in place, I mean I want to take the modified file, and make a copy of it right where it currently is with a new name. The equivalent of clicking the file (in windows) and hitting control-c control-v, then renaming the resulting file.


I've come up with this



gulp.task('default', function() {
return gulp.watch('../**/**.js', function(obj){
if (obj.type === 'changed') {
gulp.src(obj.path, { base: './' })
.pipe(rename(function (path) {
path.basename = path.basename + 'modified';
}))
.pipe(gulp.dest('./'));
}
});
});


It's very, very close, but it only works once. The second time I modify the (same) file, I wind up with blahmodifiedmodified, the next time I get blahmodifiedmodifiedmodified and so on.


Is it refusing to overwrite an existing file, or is something else wrong?





Aucun commentaire:

Enregistrer un commentaire