fix: 修改readFileList文件获取文件的一下问题
This commit is contained in:
parent
7cc62dd399
commit
31ea46dced
|
|
@ -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,7 +22,7 @@ function main() {
|
|||
fs.writeFileSync(urlsRoot, DOMAIN)
|
||||
const files = readFileList(); // 读取所有md文件数据
|
||||
|
||||
files.forEach( file => {
|
||||
files.forEach(file => {
|
||||
const { data } = matter(fs.readFileSync(file.filePath, 'utf8'));
|
||||
|
||||
if (data.permalink) {
|
||||
|
|
|
|||
|
|
@ -7,35 +7,37 @@ 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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue