Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why shut my copy task when i wanna to copy a dir with use filter function #552

Closed
moonrailgun opened this issue Feb 26, 2018 · 9 comments
Closed
Assignees

Comments

@moonrailgun
Copy link

function handleFilter (onInclude, src, dest, opts, cb) {
Promise.resolve(opts.filter(src, dest))
.then(include => {
if (include) return onInclude(src, dest, opts, cb)
return cb()
}, error => cb(error))
}

example:

fs.copy(srcDir, destDir, {
  filter: () => {
    console.log('run filter')
    return false
  },
}, (err) => {
  if (err) return console.error(err);

  console.log('success!');
});

shut down in first file copy!

i have no idea why about use cb() when run handleFilter . maybe i just wanna copy a dir and hope to filter some file which i dont want to copy
is this a reasonable design?

@RyanZim
Copy link
Collaborator

RyanZim commented Feb 26, 2018

You're consistently returning false from your filter, which indicates you don't want to copy anything, so fs-extra just stops.

@moonrailgun
Copy link
Author

@RyanZim ,i mean although i always return false, it should log run filter many times if my srcDir has many files.
but just log once and done

@manidlou
Copy link
Collaborator

manidlou commented Feb 27, 2018

@moonrailgun you are strictly returning false in your filter function. The way that copy handles filter is that if filter function returns false, it doesn't do anything. If you want to use a filter function, you essentially need to specify some sorts of conditions. IDK, at least you can set your filter function return true on your src directory, if you wanna see some logs!

@moonrailgun
Copy link
Author

@manidlou @RyanZim
Well above code is just a demo. and i write another demo to express my mind.

now , i have a dir like this:

.
├── index.js
└── src
    ├── a
    ├── b
    └── c

and the index.js is a test script like this:

const fs = require('fs-extra');

fs.copy('./src', './dist', {
  filter: function(src) {
    if(src.indexOf('b') >= 0) return false;//  i dont want to copy file b
    return true;
  }
}, (err) => {
  if (err) return console.error(err);

  console.log('success!');
})

Then , i cd to this dir and run node index.js
console output success! but the tree is not changed

├── index.js
└── src
    ├── a
    ├── b
    └── c

and then, i change my index.js line 5
from if(src.indexOf('b') >= 0) return false; to if(src.indexOf('d') >= 0) return false;(always return true in filter function), everything will be okay.

├── dist
│   ├── a
│   ├── b
│   └── c
├── index.js
└── src
    ├── a
    ├── b
    └── c

my fs-extra is latest version in npm:5.0.0

@manidlou
Copy link
Collaborator

Mmm...! @moonrailgun I guess you don't get your desired results because your filter condition is based on the src path string, and we overwrite the src and dest with their full paths while we shouldn't! I blame myself on that!

In order to make sure about this, will you please change your filter condition to something like this? If my guess is true, your code should work with these changes.

const fs = require('fs-extra');
const path = require('path');

fs.copy('./src', './dist', {
  filter: function(src) {
    const basename = path.basename(src)
    if(basename.indexOf('b') >= 0) return false;//  i dont want to copy file b
    return true;
  }
}, (err) => {
  if (err) return console.error(err);

  console.log('success!');
})

@moonrailgun
Copy link
Author

@manidlou well this work, but not my mind.
its not a good demo because some external cause. like the file path, you know.
its my fault, I apologize.

but plz look at the follow code:

const fs = require('fs-extra');

fs.copy('./src', './dist', {
  filter: () => {
    const isPass = Math.random() < 0.5;
    console.log(isPass);
    return isPass;
  },
}, (err) => {
  if (err) return console.error(err);

  console.log('success!');
  return null;
});

if the pass return false in first times, or second times. the task is done. and or return false not the first times, its run.

and i find why its done . because the first times and second times the src is the root dir. if my filter function not pass it, its throw a error and done my copy task.

i just want to copy dir with .gitignore file. and the root path not to pass it.

maybe add some tips in readme file? hopes can help others who happened same problem like me.

thanks for your help

@manidlou
Copy link
Collaborator

manidlou commented Mar 3, 2018

@moonrailgun the proposed fix #554 is under review. It will be most likely published in the next release.

@moonrailgun
Copy link
Author

Good change! same path for filter func can avoid some problem and avoid use path.relative in every function loop every times!
thanks for your change

@manidlou manidlou self-assigned this Mar 13, 2018
@RyanZim
Copy link
Collaborator

RyanZim commented Apr 6, 2018

Fixed in #554, will be in the next release later this month.

@RyanZim RyanZim closed this as completed Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants