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

callback function for copy #119

Open
linojon opened this issue Mar 15, 2023 · 1 comment
Open

callback function for copy #119

linojon opened this issue Mar 15, 2023 · 1 comment

Comments

@linojon
Copy link

linojon commented Mar 15, 2023

I'm looking for a way to provide a progress bar, especially copying large files and directories. It could also be useful on inspectTree (with md5). Is there a way to do that with this package currently?

@szwacz
Copy link
Owner

szwacz commented Mar 16, 2023

Currently there is no such option and I think this is outside of the scope of this library. The problem with it, is that for progress calculation you need to first scan all the files to be able to calculate total size, what if implemented in the library will make operation slower for everybody, even if you don't need this option.

That being said, I think it can be coded pretty reasonably with the primitives of this library. Here is very primitive attempt (that I didn't run, so probably contains bugs).

let fileSizes = {};
let totalSize = 0;

let filesToCopy = jetpack.find("my_dir", {
  filter: (inspectObj) => {
    fileSizes[inspectObj.absolutePath] = inspectObj.size;
    totalSize += inspectObj.size;
    return true;
  }
});

let index = 0;
let alreadyCopiedSize = 0;

const copyFile = () => {
  let path = filesToCopy[index];
  jetpack.copyAsync(path).then(() => {
      alreadyCopiedSize += fileSizes[path];
      
      let progress = alreadyCopiedSize / totalSize;
      console.log(progress);
      
      index += 1;
      if (index < filesToCopy.length) {
        copyFile();
      }
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants