OnIIcE February 2016

gulp Copy from multiple locations, dont preserve structure

Im trying to copy files from multiple locations, into one central location using gulp. I dont want to preserve the directory path strucure they are being copied from EXCEPT for directories from within the one im copying from (hopefully that makes sense).

So in my example i only want to preserve any structure found in the images directory.

I have:

    var files = [
        'path/to/wp/my-mu-plugins/**/build/theme/images/**/*',
        'path/to/wptheme/build/theme/images/**/*',
        'bower_components/jquery-ui/themes/base/images/**/*',
        'bower_components/jquery-ui/themes/ui-lightness/images/**/*',
    ];

    return gulp.src(files)
        .pipe(gulp.dest('public_html/assets/images/'));

I have tried different combination's of altering basename, base, dir and using glob to pull them in - but I always end up with either either a flat structure or a the entire structure from where im copying from.

What combination of wizardy do i need to achieve this - or is this not possible?

Thanks =)

edit: I think my issue is with the path/to/wp/my-mu-plugins/** paths, that's where the trouble starts...

Answers


Sven Schoenung February 2016

When writing to the dest() location gulp strips away a base path from the path of every file. If no explicit base path is provided, gulp (actually vinyl-fs) assumes everything up to the first glob to be the base path.

In your case that means that 'path/to/wp/my-mu-plugins/' is assumed to be the base path for everything matching 'path/to/wp/my-mu-plugins/**/build/theme/images/**/*'.

While you can specify an explicit base path using the base option to src() this isn't of much use to you, since the base option doesn't support globbing and you don't know which or how many subdirectories match 'my-mu-plugins/**'.

However you can use the base option to disable the default behavior and then strip everything up to and including the /images/ folder from the path using gulp-rename:

gulp.task('default', function() {
  return gulp.src(files, { base:'./' })
    .pipe(rename(function(path) {
      path.dirname = path.dirname.replace(/(.*)?\/images\/?/, '');
    }))
    .pipe(gulp.dest('public_html/assets/images/'));
});

Post Status

Asked in February 2016
Viewed 2,574 times
Voted 6
Answered 1 times

Search




Leave an answer