Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy directories recursively with gulp?

People also ask

What is copy directories recursively?

Recursive means that cp copies the contents of directories, and if a directory has subdirectories they are copied (recursively) too. Without -R , the cp command skips directories.

How do you copy subdirectories?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option.

How do I copy files using Gulp?

var copy = require('gulp-copy'); gulp. task('copy-resources', function() { return gulp. src('./src/img/*. png') .

How do I copy a directory in Windows recursively?

To copy a single file or multiple files recursively with the Windows command prompt, use the xcopy command. The xcopy command is very similar to the copy command but it handles recursion and has many other options mainly related to recursion.


The following works without flattening the folder structure:

gulp.src(['input/folder/**/*']).pipe(gulp.dest('output/folder'));

The '**/*' is the important part. That expression is a glob which is a powerful file selection tool. For example, for copying only .js files use: 'input/folder/**/*.js'


Turns out that to copy a complete directory structure gulp needs to be provided with a base for your gulp.src() method.

So gulp.src( [ files ], { "base" : "." }) can be used in the structure above to copy all the directories recursively.

If, like me, you may forget this then try:

gulp.copy=function(src,dest){
    return gulp.src(src, {base:"."})
        .pipe(gulp.dest(dest));
};

So - the solution of providing a base works given that all of the paths have the same base path. But if you want to provide different base paths, this still won't work.

One way I solved this problem was by making the beginning of the path relative. For your case:

gulp.src([
    'index.php',
    '*css/**/*',
    '*js/**/*',
    '*src/**/*',
])
.pipe(gulp.dest('/var/www/'));

The reason this works is that Gulp sets the base to be the end of the first explicit chunk - the leading * causes it to set the base at the cwd (which is the result that we all want!)

This only works if you can ensure your folder structure won't have certain paths that could match twice. For example, if you had randomjs/ at the same level as js, you would end up matching both.

This is the only way that I have found to include these as part of a top-level gulp.src function. It would likely be simple to create a plugin/function that could separate out each of those globs so you could specify the base directory for them, however.