Gulp copy all files inside a directory , without directory

Viewed 1630

This is my gulpfile

const gulp = require('gulp');
const runSequence = require('run-sequence');
const gulpCopy = require('gulp-copy');

//move client side library from client-lib to public folder
gulp.task('move-file',function(){
    console.log("Move-files");
    return gulp
        .src(['./client-lib/*.js'])        
        .pipe(gulpCopy('./public'))        

});

gulp.task('default',function(){   
    runSequence('move-file');
});

I need to copy all js files inside client-lib folder and copy to public folder . But this code copy with folder and my public folders look like

public->client-lib->myjsfiles

But I need

public->myjsfiles

3 Answers

You dont need to use any NPM for this ,

remove `const gulpCopy = require('gulp-copy');` ,

and gulpCopy this command from task as well .

and simply try

gulp.task('move-file',function(){ 
gulp .src('client-lib/*.js') .pipe(rename({dirname: ''})) 
.pipe(gulp.dest('./public')) 
});

You can use a library called gulp-rename and here's simple example:

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

   gulp.task('html', function(){
     return gulp.src('app/client/**/*.html')
         .pipe(rename({dirname: ''}))
        .pipe(gulp.dest('./dist'));
    });

More answers:
Can you remove a folder structure when copying files in gulp?

gulp.task('move-file',function(){
    console.log("Move-files");
    return gulp
        .src(['./client-lib/*.js'])        
        .pipe(gulpCopy('./public', { prefix: 1 }))        

})

Here's explanation: https://www.npmjs.com/package/gulp-copy#options

gulp-copy has an options object, where you could use prefix value. If you set it to 1, it would remove 1 part separated by "/" of your path. In your case 1 is enough. If you want to remove all directories, then use 99.

Related