Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp Copy content from one file to another file

I am trying to copy the content from one file to another. I am trying the following code but its throwing me an error.

gulp
    .src('core/core.config.local.tpl.js')
    .pipe(gulp.dest(core/core.config.js));

Error: EEXIST: file already exists, mkdir 'C:\Users\krish\Documents\test\app\core\core.config.js' at Error (native)

Is there any other process that I could use to copy content?

like image 347
Krishh Avatar asked Jan 07 '23 13:01

Krishh


2 Answers

gulp.dest expects a directory as an argument. All files are written to this destination directory. If the directory doesn't exist yet, gulp tries to create it.

In your case gulp.dest tries to create a directory core/core.config.js, but fails since a regular file with the same name already exists.

If your goal is to have the regular file at core/core.config.js be overwritten with the contents of core/core.config.local.tpl.js on every build, you can do it like this:

var gulp = require('gulp');
var rename = require('gulp-rename');

gulp.task('default', function() {
  gulp.src('core/core.config.local.tpl.js')
    .pipe(rename({ basename: 'core.config'}))
    .pipe(gulp.dest('core'));
});
like image 88
Sven Schoenung Avatar answered Jan 18 '23 15:01

Sven Schoenung


I think you were missing the quotes only when entering the the question here, right?

gulp.dest() expects a folder to copy all the files in the stream, so you cannot use a single file name here (As the error says: "mkdir failed"). See: https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulpdestpath-options

When you do your gulp.src() you can set a base path to build the relative path from and so gulp can copy your single file to the given output folder. See: https://github.com/gulpjs/gulp/blob/master/docs/API.md#optionsbase

As you also want to rename the file, you need something like gulp-rename: https://www.npmjs.com/package/gulp-rename

like image 41
Andreas Jägle Avatar answered Jan 18 '23 15:01

Andreas Jägle