Skip to content

Commit

Permalink
fix(box): check for invalid file (#5173)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Mar 10, 2023
1 parent cb19b29 commit 7edbf25
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ class Box extends EventEmitter {
}

_readDir(base, prefix = '') {
const { context: ctx } = this;
const results = [];
return readDirWalker(base, results, this.ignore, prefix)
return readDirWalker(ctx, base, results, this.ignore, prefix)
.return(results)
.map(path => this._checkFileStatus(path))
.map(file => this._processFile(file.type, file.path).return(file.path));
Expand Down Expand Up @@ -155,7 +156,7 @@ class Box extends EventEmitter {
path
});
}).catch(err => {
ctx.log.error({err}, 'Process failed: %s', magenta(path));
ctx.log.error({ err }, 'Process failed: %s', magenta(path));
}).finally(() => {
this._processingFiles[path] = false;
}).thenReturn(path);
Expand Down Expand Up @@ -245,21 +246,28 @@ function isIgnoreMatch(path, ignore) {
return path && ignore && ignore.length && isMatch(path, ignore);
}

function readDirWalker(base, results, ignore, prefix) {
function readDirWalker(ctx, base, results, ignore, prefix) {
if (isIgnoreMatch(base, ignore)) return Promise.resolve();

return Promise.map(readdir(base).catch(err => {
ctx.log.error({ err }, 'Failed to read directory: %s', base);
if (err && err.code === 'ENOENT') return [];
throw err;
}), async path => {
const fullpath = join(base, path);
const stats = await stat(fullpath);
const stats = await stat(fullpath).catch(err => {
ctx.log.error({ err }, 'Failed to stat file: %s', fullpath);
if (err && err.code === 'ENOENT') return null;
throw err;
});
const prefixdPath = `${prefix}${path}`;
if (stats.isDirectory()) {
return readDirWalker(fullpath, results, ignore, `${prefixdPath}/`);
}
if (!isIgnoreMatch(fullpath, ignore)) {
results.push(prefixdPath);
if (stats) {
if (stats.isDirectory()) {
return readDirWalker(ctx, fullpath, results, ignore, `${prefixdPath}/`);
}
if (!isIgnoreMatch(fullpath, ignore)) {
results.push(prefixdPath);
}
}
});
}
Expand Down

0 comments on commit 7edbf25

Please sign in to comment.