Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 316
Adam Rackis Avatar asked Feb 18 '15 20:02

Adam Rackis


People also ask

How do I rename a file in Gulp?

gulp. dest() renames the directories between process. cwd() and dirname (i.e. the base relative to CWD). Use dirname to rename the directories matched by the glob or descendents of the base of option.

How do I copy files using Gulp?

var copy = require('gulp-copy'); gulp. task('copy-resources', function() { return gulp. src('./src/img/*. png') .

What is Gulp DEST?

dest() Creates a stream for writing Vinyl objects to the file system.

What is Gulp series?

series() Combines task functions and/or composed operations into larger operations that will be executed one after another, in sequential order.


1 Answers

I'm not 100% certain what you mean by

copy and rename ... in place

But, based on your current code, if you simply wish to:

  1. Watch all .js files in the parent directory and
  2. Copy them to the cwd (current working directory) and
  3. Name all copies, regardless of source file, the same thing

Then you could use gulp-rename to do just that:

var gulp = require('gulp'); var rename = require('gulp-rename');  gulp.task('default', function() {   return gulp.watch('../**/**.js', function(obj) {     gulp.src(obj.path)       .pipe(rename('newFileName.js'))       .pipe(gulp.dest('.'));   }); }); 

In this case, the output filename is newFileName.js

In order to use the module, you'll need to install the gulp-rename package with npm (ie: npm install gulp-rename).

More examples are available on the package details page on npm @ https://www.npmjs.com/package/gulp-rename#usage

like image 135
knksmith57 Avatar answered Sep 27 '22 15:09

knksmith57