copy icon indicating copy to clipboard operation
copy copied to clipboard

Weird behaviour if first glob in array is using '**'.

Open sazulo opened this issue 8 years ago • 4 comments

if I do a thing like

const copy = require('copy');

const files = ['dist/**/*','README.md','LICENSE.md'];
copy(files, '/tmp/folder', (err, files) => {
 // handle result here 
})

Result :

  • README.md and LICENSE.md are located in /tmp
  • The content of the dist folder is in '/tmp/folder'

Should be :

  • README.md and LICENSE.md are located in /tmp/folder
  • The content of the dist folder is in /tmp/folder/dist

Note that the order of the globs is important as if i declare it like :

const files = ['README.md','LICENSE.md','dist/**/*'];

It behaves as intended.

sazulo avatar Sep 26 '16 17:09 sazulo

strange, I'll look into it. I'm guessing it has to do with how the glob parent is created. thanks for the issue

jonschlinkert avatar Sep 26 '16 19:09 jonschlinkert

Honestly, I just switched to using vinyl-fs.

const vfs = require('vinyl-fs');

function copyFiles(glob, dest) {
  return new Promise((resolve, reject) => {
    // the base is important, if not set it will put all 
    // files directly in the 'dest' folder
    const stream = vfs.src(glob, { base: '.' })
      .pipe(vfs.dest(dest));
    stream.on('finish', () => {
      resolve();
    });
    stream.on('error', (error) => {
      reject(err);
    });
  });
}

sazulo avatar Sep 27 '16 17:09 sazulo

Honestly, I just switched to using vinyl-fs.

That's great. we use vinyl-fs quite a lot, and we contribute a lot to that ecosystem (gulp and vinyl). It's a bit overkill for simple copying, but I don't blame you after having issues with this lib.

jonschlinkert avatar Sep 27 '16 17:09 jonschlinkert

FYI for anyone that comes across this issue... You can set the srcBase option to specify the base property used when creating file objects. This is similar to setting the base option on vfs.src in the example above.

updated example from original comment

const copy = require('copy');

const files = ['dist/**/*','README.md','LICENSE.md'];
copy(files, '/tmp/folder', {srcBase: '.'}, (err, files) => {
 // handle result here 
})

I'm going to keep this open for now since this is something we need to update in the documentation.

doowb avatar Sep 01 '17 16:09 doowb