Skip to content

Commit

Permalink
Fix the CI failing issue on Hexo (#27)
Browse files Browse the repository at this point in the history
* Revert "Reduce scope such as recursive function"

* Update fs.js

* Update fs.js
  • Loading branch information
NoahDragon authored Dec 20, 2018
1 parent a6c9ba0 commit df52002
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down

0 comments on commit df52002

Please sign in to comment.