Kevin Glier
Kevin Glier

Reputation: 1386

Copy multiple specific files and folders to another directory using gulp

I'm trying to copy multiple files and folders using gulp.src and gulp.dest from my base directory . to a new subdirectory dist.

The base directory is the root of a Symfony project.

Here's my gulpfile:

var files = [
  './app**/*.*',
  './bin**/*.*',
  './src**/*.*',
  './tests**/*.*',
  './var/',
  './vendor/',
  './composer.json']

gulp.task('distribute', function() {
  return gulp.src(files , { base: '.' }).pipe(gulp.dest('./dist'));
});

I just don't understand, why this isn't working. As result, I get a dist folder with the contents of the specified folders. So app, bin, src etc. are missing as root folders.

Upvotes: 4

Views: 8468

Answers (1)

Emile Bergeron
Emile Bergeron

Reputation: 17430

Moving the directories themselves

Based on Use gulp to select and move directories and their files, use ./ for the base option and make sure the paths are correct:

var files = [
  './app/**/*.*',
  './bin/**/*.*',
  './src/**/*.*',
  './tests/**/*.*',
  './var/',
  './vendor/',
  './composer.json'
];

gulp.task('distribute', function() {
  return gulp.src(files , { base: './' })
    .pipe(gulp.dest('dist'));
});

Moving the files within each directories

Don't use the base option and change your paths a little.

var files = [
  'app/**/*.*',
  'bin/**/*.*',
  'src/**/*.*',
  'tests/**/*.*',
  'var/',
  'vendor/',
  'composer.json'
];

gulp.task('distribute', function() {
    return gulp.src(files)
        .pipe(gulp.dest('./dist'));
});

Flexible way to threat each directory as a task

If you want to move the folder themselves, just make a task for each one using a task generator function.

function moveDirTask(dir, dest) {
    return function() {
        return gulp.src(`${dir}/**/*`)
            .pipe(gulp.dest(`${dest}/${dir}/`));
    };
}

gulp.task('move-app', moveDirTask('app', 'dest'));
gulp.task('move-bin', moveDirTask('bin', 'dest'));
gulp.task('move-src', moveDirTask('src', 'dest'));
gulp.task('move-tests', moveDirTask('tests', 'dest'));
// etc.

gulp.task('distribute', [
    'move-app',
    'move-bin',
    'move-src',
    'move-tests'
    // etc.
], function() {
    return gulp.src('composer.json')
        .pipe(gulp.dest('dest'));
});

Upvotes: 13

Related Questions