From 7edbf25d5b4c4d7fdf99ff2439cfd9b2cb1cfe0d Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Fri, 10 Mar 2023 16:58:37 +0800 Subject: [PATCH] fix(box): check for invalid file (#5173) --- lib/box/index.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/box/index.js b/lib/box/index.js index ebea4c3864..2975570b8c 100644 --- a/lib/box/index.js +++ b/lib/box/index.js @@ -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)); @@ -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); @@ -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); + } } }); }