jonschlinkert / copy

Copy files using glob patterns. Sync, async, promise or streams. (node.js utility)
MIT License
94 stars 125 forks source link

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

Open sazulo opened 7 years ago

sazulo commented 7 years ago

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 :

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.

jonschlinkert commented 7 years ago

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

sazulo commented 7 years ago

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);
    });
  });
}
jonschlinkert commented 7 years ago

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.

doowb commented 7 years ago

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.