简体   繁体   中英

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.

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'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM