jprichardson / node-klaw

A Node.js file system walker with a Readable stream interface. Extracted from fs-extra.
MIT License
317 stars 41 forks source link

Klaw stops on error #23

Open dzek69 opened 7 years ago

dzek69 commented 7 years ago

Hello,

I really appreciate this awesome package, but I cannot configure it to continue on error.

If directory access error occurs (not sure about file errors) then klaw just emits error and stops. end isn't called.

I'd like to continue seeking through directories.

Currently I cannot use klaw to scan whole readable filesystem, because it surely won't have access to some directories.

Any solutions?

bjorn-nesby commented 7 years ago

You could add a filter function, and "stat" each path using a try...catch. If it fails, returning false will allow you to skip that troublesome entry.

ponsoc commented 6 years ago

Hi,

I'm running into the same issue and I'm not sure how change the example code to keep it running while ignoring the error. Please advice :-)

briancampo commented 6 years ago

Depending on what you are doing, this might work. I just wrapped my klaw call with fs-extra's pathExists, and if you want to scan a list of folders just create an outer klaw call that checks only for directories and passes the current folder to the below.

  fs.pathExists(folder, (err, exists) => {
    if (exists) {
      klaw(folder, { depthLimit: depth })
          .pipe(excludeDirFilter)
          .on('data', item => {
            files.push(item.path);
          })
          .on('error', (err, item) => {
            console.log('klaw error: ', item);
            throw new Error(`klawError: ${err}`);
          })
          .on('end', () => {cb(files);}); 
    } else {
      cb({ error: `file/folder does not exist: ${folder}`});
    }
  });
CTimmerman commented 5 years ago

One solution is to use vanilla Node:

// Vanilla Node walker, hard to check done when using default async implementation.
let walk = dir => {
    fs.readdirSync(dir).forEach(item => {
        let itemPath = path.join(dir, item)
        try {
            var stats = fs.statSync(itemPath)
        } catch (e) {
            console.log(e)
            return
        }
        if (stats.isDirectory()) walk(itemPath)
        else if (stats.isFile()) patch(itemPath)
    })
}
walk(project_folder)
console.log("Done.")
emptydriptray commented 5 years ago

Did anyone find a solution to this issue? I would prefer to use Klaw over the vanilla Node (as suggested above). Any help is much appreciated !