From 0e8324504ea5236d01139a4d83878d0e4ee70b17 Mon Sep 17 00:00:00 2001 From: v_yongztan Date: Thu, 5 Nov 2020 21:33:42 +0800 Subject: [PATCH] =?UTF-8?q?fix=20command=20=E5=85=BC=E5=AE=B9index=20?= =?UTF-8?q?=E4=B8=8D=E6=98=AF=E6=95=B0=E5=AD=97=E7=9A=84=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- createByUrl.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/createByUrl.js b/createByUrl.js index d68e439..7aa5b16 100644 --- a/createByUrl.js +++ b/createByUrl.js @@ -42,12 +42,29 @@ async function getPageInfo(url) { } }); const funcReg = /var\s(.*)\s=\sfunction/ - const index = questionTitle.split('.')[0] + let index = questionTitle.split('.')[0] const funcMatch = code.match(funcReg) let funcName = '' if (funcMatch && funcMatch.length === 2) { funcName = funcMatch[1] } + + // 文件前缀是否数字,不是则用leetcode作为前缀 + const numReg = /^[0-9]+?$/; + if (!numReg.test(index)) { + const leetCodeFileReg = new RegExp(`^leetcode_[0-9]+?_${funcName}.js$`) //; + const libDir = fs.readdirSync('lib/') + let i = 1 + for (const item of libDir) { + if (item.indexOf('leetcode') === 0) { + if (leetCodeFileReg.test(item)) { + throw new Error("file may exist - " + item); + } + i++ + } + } + index = `leetcode_${i}` + } const fileName = `${index}_${funcName}.js`