Skip to content

Commit

Permalink
perf: ⚡ 优化脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
fxzer committed Dec 5, 2023
1 parent d3800a7 commit 8101ae6
Showing 1 changed file with 58 additions and 55 deletions.
113 changes: 58 additions & 55 deletions scripts/generate-sidebar.cjs
Original file line number Diff line number Diff line change
@@ -1,60 +1,9 @@
/******** 读取 zerdocs/docs/ 下的所有文件夹,自动生成侧边栏sidebar.ts文件 ***********/


const fs = require('fs')
const path = require('path')

let sidebarObj = {}

//生成一级分组
let topDirArr = fs.readdirSync(path.resolve(__dirname, '../docs')).filter((item) => isArticleDir(item))
topDirArr.forEach((item) => {
if (!sidebarObj[`/${item}/`]) {
sidebarObj[`/${item}/`] = []
}
})

const sidebars = deepGetFile(path.resolve(__dirname, '../docs')) // 读取 docs 目录下的所有文件夹
const sidebarlist = deepGenerateSidebar(sidebars)

//写入到docs/.vitepress/sidebar/index.ts

//把数组里面的每个对象合并到一个对象里面
let sidebar = sidebarlist.reduce((pre, cur) => Object.assign(pre, cur), {})
Object.entries(sidebar).forEach(([key, items]) => {
let keyArr = splitPath(key)
let text = keyArr[keyArr.length - 1]
if(sidebarObj[`/${keyArr[0]}/`]){
sidebarObj[`/${keyArr[0]}/`].push({
text,
collapsible: true,
collapsed: false,
items,
})
}else{
sidebarObj[`/${keyArr[0]}/`] = [{
text,
collapsible: true,
collapsed: false,
items,
}]
}
})

const sidebarStr = JSON.stringify(sidebarObj, null, 2)
//把sidebarStr写入到docs/.vitepress/sidebar/index.ts
const docsPath = path.resolve(__dirname, '../docs')
const sidebarPath = path.resolve(__dirname, '../docs/.vitepress/sidebar/index.ts')

//没有则创建
if (!fs.existsSync(sidebarPath)) {
fs.mkdirSync(path.resolve(__dirname, '../docs/.vitepress/sidebar'))
}

const res = fs.writeFile(sidebarPath, `export default ${sidebarStr}`, (err) => {
if (err) console.log(err)
console.log('===> 侧边栏生成成功! <===\n')
})

//判断是否是文章文件
function isArticleDir(dir) {
let isExclude = /^(public|index.md|.*\.ts|demo.*|\..*)$/.test(dir)
Expand All @@ -64,17 +13,18 @@ function isArticleDir(dir) {
function splitPath(path) {
return path.split('/').filter((item) => item !== '')
}

function deepGetFile(dir) {
let backList = []
let list = fs.readdirSync(dir).filter((item) => isArticleDir(item))
let list = fs.readdirSync(dir).filter(isArticleDir)

for (let index in list) {
let item = path.resolve(dir, list[index])
if (fs.statSync(item).isDirectory()) {
backList = backList.concat(deepGetFile(item))
} else {
//输出相对路径
item = item.replace(path.resolve(__dirname, '../docs'), '').replace(/\\/g, '/')
item = item.replace(docsPath, '').replace(/\\/g, '/')
backList.push(item)
}
}
Expand All @@ -85,13 +35,17 @@ function deepGetFile(dir) {
function deepGenerateSidebar(arr) {
//递归按照最后一级目录生成侧边栏
const sidebar = {}
sidebars.forEach((item) => {
arr.forEach((item) => {
const [dir, ...rest] = splitPath(item)
if (!sidebar[dir]) {
sidebar[dir] = []
}
sidebar[dir].push(item)
})
return sidebar
}

function generateSidebarList(sidebar) {
let sidebarList = []
//按最后一级目录分组
for (let key in sidebar) {
Expand All @@ -109,4 +63,53 @@ function deepGenerateSidebar(arr) {
return sidebarList
}

function generateSidebarObj(topDirArr, sidebarList) {
const sidebarObj = {}
topDirArr.forEach((item) => {
if (!sidebarObj[`/${item}/`]) {
sidebarObj[`/${item}/`] = []
}
})

let sidebar = sidebarList.reduce((pre, cur) => Object.assign(pre, cur), {})
Object.entries(sidebar).forEach(([key, items]) => {
let keyArr = splitPath(key)
let text = keyArr[keyArr.length - 1]
if (sidebarObj[`/${keyArr[0]}/`]) {
sidebarObj[`/${keyArr[0]}/`].push({
text,
collapsible: true,
collapsed: false,
items,
})
} else {
sidebarObj[`/${keyArr[0]}/`] = [{
text,
collapsible: true,
collapsed: false,
items,
}]
}
})

return sidebarObj
}

function writeToFile(sidebarObj) {
const sidebarStr = JSON.stringify(sidebarObj, null, 2)
// 检查目录是否存在,如果不存在则创建
if (!fs.existsSync(path.dirname(sidebarPath))) {
fs.mkdirSync(path.dirname(sidebarPath), { recursive: true })
}

fs.writeFile(sidebarPath, `export default ${sidebarStr}`, (err) => {
})
}

//生成一级分组
let topDirArr = fs.readdirSync(docsPath).filter(isArticleDir)
const sidebars = deepGetFile(docsPath) // 读取 docs 目录下的所有文件夹
const sidebar = deepGenerateSidebar(sidebars)
const sidebarList = generateSidebarList(sidebar)
const sidebarObj = generateSidebarObj(topDirArr, sidebarList)
writeToFile(sidebarObj)

0 comments on commit 8101ae6

Please sign in to comment.