diff --git a/utils/baiduPush.js b/utils/baiduPush.js index 7ed2df0..8689ecd 100644 --- a/utils/baiduPush.js +++ b/utils/baiduPush.js @@ -9,13 +9,12 @@ const readFileList = require('./modules/readFileList'); const urlsRoot = path.join(__dirname, '..', 'urls.txt'); // 百度链接推送文件 const DOMAIN = process.argv.splice(2)[0]; // 获取命令行传入的参数 -if (!DOMAIN) { +if (DOMAIN) { + main(); +} else { console.log(chalk.red('请在运行此文件时指定一个你要进行百度推送的域名参数,例:node utils/baiduPush.js https://xugaoyi.com')) - return } -main(); - /** * 主体函数 */ @@ -23,8 +22,8 @@ function main() { fs.writeFileSync(urlsRoot, DOMAIN) const files = readFileList(); // 读取所有md文件数据 - files.forEach( file => { - const { data } = matter(fs.readFileSync(file.filePath, 'utf8')); + files.forEach(file => { + const { data } = matter(fs.readFileSync(file.filePath, 'utf8')); if (data.permalink) { const link = `\r\n${DOMAIN}${data.permalink}`; diff --git a/utils/modules/readFileList.js b/utils/modules/readFileList.js index 8eb97c6..1e90082 100644 --- a/utils/modules/readFileList.js +++ b/utils/modules/readFileList.js @@ -7,37 +7,39 @@ const docsRoot = path.join(__dirname, '..', '..', 'docs'); // docs文件路径 function readFileList(dir = docsRoot, filesList = []) { const files = fs.readdirSync(dir); - files.forEach( (item, index) => { - let filePath = path.join(dir, item); - const stat = fs.statSync(filePath); - if (stat.isDirectory() && item !== '.vuepress') { - readFileList(path.join(dir, item), filesList); //递归读取文件 - } else { - if(path.basename(dir) !== 'docs'){ // 过滤docs目录级下的文件 + files.forEach((item, index) => { + let filePath = path.join(dir, item); + const stat = fs.statSync(filePath); + if (stat.isDirectory() && item !== '.vuepress') { + readFileList(path.join(dir, item), filesList); //递归读取文件 + } else { + if (path.basename(dir) !== 'docs') { // 过滤docs目录级下的文件 - const fileNameArr = path.basename(filePath).split('.') - let name = null, type = null; - if (fileNameArr.length === 2) { // 没有序号的文件 - name = fileNameArr[0] - type = fileNameArr[1] - } else if (fileNameArr.length === 3) { // 有序号的文件 - name = fileNameArr[1] - type = fileNameArr[2] - } else { // 超过两个‘.’的 - log(chalk.yellow(`warning: 该文件 "${filePath}" 没有按照约定命名,将忽略生成相应数据。`)) - return - } - if(type === 'md'){ // 过滤非md文件 - filesList.push({ - name, - filePath - }); - } + const filename = path.basename(filePath); + const fileNameArr = filename.split('.'); + const firstDotIndex = filename.indexOf('.'); + const lastDotIndex = filename.lastIndexOf('.'); + let name = null, type = null; + if (fileNameArr.length === 2) { // 没有序号的文件 + name = fileNameArr[0] + type = fileNameArr[1] + } else if (fileNameArr.length >= 3) { // 有序号的文件(或文件名中间有'.') + name = filename.substring(firstDotIndex + 1, lastDotIndex) + type = filename.substring(lastDotIndex + 1) } - } + + if (type === 'md') { // 过滤非md文件 + filesList.push({ + name, + filePath + }); + } + + } + } }); return filesList; } -module.exports = readFileList; \ No newline at end of file +module.exports = readFileList;