From df5200210f2d4f197d4f1f5484a161cbbc09ff98 Mon Sep 17 00:00:00 2001 From: Abner Chou Date: Thu, 20 Dec 2018 14:23:32 -0500 Subject: [PATCH] Fix the CI failing issue on Hexo (#27) * Revert "Reduce scope such as recursive function" * Update fs.js * Update fs.js --- lib/fs.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index c79c8f9..b897ba4 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -447,13 +447,20 @@ function watch(path, options, callback) { function _findUnusedPath(path, files) { const ext = extname(path); const base = basename(path, ext); - const regex = new RegExp(`^${escapeRegExp(base)}-(\\d+)${escapeRegExp(ext)}$`); + const regex = new RegExp(`^${escapeRegExp(base)}(?:-(\\d+))?${escapeRegExp(ext)}$`); + let num = -1; - const num = files.reduce((_num, item) => { - const match = regex.exec(item); + for (let i = 0, len = files.length; i < len; i++) { + const item = files[i]; + if (!regex.test(item)) continue; + + const match = item.match(regex); + const matchNum = match[1] ? parseInt(match[1], 10) : 0; - return match != null ? Math.max(parseInt(match[1], 10), _num) : _num; - }, -1); + if (matchNum > num) { + num = matchNum; + } + } return join(dirname(path), `${base}-${num + 1}${ext}`); }