4

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.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Kevin Glier
  • 1,346
  • 2
  • 14
  • 30

1 Answers1

12

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'));
});
Community
  • 1
  • 1
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
  • Thanks for your answer. But this copies the contents of the first 4 folders, but not the folders themselves. How can I archive this? – Kevin Glier May 09 '17 at 13:47
  • @KevinGlier I updated the answer to reflect your expected behaviour. – Emile Bergeron May 09 '17 at 14:02
  • Is there no way to get it to work like the linux cp command? cp -r app src bin tests composer.json dist/ ? – Kevin Glier May 09 '17 at 14:18
  • @KevinGlier with the `base` set to `./`, is it working? – Emile Bergeron May 09 '17 at 14:24
  • 1
    Thank you, "Moving the directories themselves" did it for me. I just changed the *.* in the paths to a single *, so that also the shell scripts within the bin folder get copied. – Kevin Glier May 09 '17 at 14:28
  • Gulp newbie here, I have followed official instructions [here](https://gulpjs.com/docs/en/getting-started/quick-start/#install-the-gulp-command-line-utility) and can successfully execute their first `default` example. However when I replace contents of `gruntfile.js` with contents of your second code snippet (with relevant file names) and run `gulp` from the command line, I get `ReferenceError: gulp is not defined`. Can anyone please tell me what I am doing wrong? **Edit**: Oh, I had to add `var gulp = require('gulp'); ` in `gruntfile.js` – user1063287 May 19 '20 at 03:14
  • @user1063287 it looks like you're confusing grunt and gulp which are both similar libs, but they don't share the same API. Gulp won't work with the gruntfile directly, and you need to install gulp as a dependency as well. – Emile Bergeron May 19 '20 at 04:32
  • apologies, it was a typo, i did mean to write `gulpfile.js` instead of `gruntfile.js`, sorry. – user1063287 May 19 '20 at 04:44