1 Star 0 Fork 0

辰風依恛/pieces-ui

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
generateFileToJson.js 5.31 KB
一键复制 编辑 原始数据 按行查看 历史
辰風依恛 提交于 2024-12-05 17:09 . 生成json
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const fsPromises = fs.promises;
// 获取项目根目录路径
const projectRoot = __dirname;
// 指定要读取的目录
const viewDir = path.resolve(projectRoot, 'src/project');
const prefix = '/src/'
const mainFileList = ['index.html', 'Index.html', 'index.vue', 'Index.vue', 'index.md', 'Index.md', 'index.js', 'Index.js',]
/**
* 获取相对路径
* @param {*} baseDir 根路径
* @param {*} fullPath 文件路径
* @returns 相对路径
*/
function getRelativePath(baseDir, fullPath) {
const relativePath = path.relative(baseDir, fullPath);
return '/' + relativePath.split(path.sep).join('/'); // 使用单斜杠
}
/**
* 获取文件目录结构
* @param {*} baseDir 根路径
* @param {*} fullPath 文件/文件夹路径
* @param {*} rank 层级
* @returns 数组
*/
function getFilePath(baseDir, fullPath, rank = 1) {
--rank
const entries = fs.readdirSync(fullPath, { withFileTypes: true });
const contents = entries.map((dirent) => {
const childrenFullPath = path.resolve(fullPath, dirent.name);
const stats = fs.statSync(childrenFullPath);
const item = {
name: dirent.name, // 名称
relativePath: getRelativePath(baseDir, childrenFullPath), // 统一使用相对路径
isDirectory: dirent.isDirectory(), // 是否是目录
type: path.extname(dirent.name).substring(1), // 后缀
};
if (rank !== 0) {
item.children = getFilePath(baseDir, childrenFullPath, rank)
}
return item;
});
return contents
}
const fileArr = getFilePath(projectRoot, viewDir, 2)
/**
* 获取json中的内容
* @param {*} data
* @returns
*/
function getJSONContent(baseDir, data, fileTypeList) {
return Promise.all(
data.map(async (el) => {
if (el.children && el.children.length > 0) {
return {
...el,
children: await getJSONContent(baseDir, el.children, fileTypeList)
}
} else {
const fullPath = baseDir + el.relativePath
return {
...el,
info: await getFileInfo(baseDir, fullPath, fileTypeList)
}
}
})
)
}
/**
* 获取json中的内容
* @param {*} baseDir
* @param {*} fullPath
* @param {*} fileTypeList
* @returns
*/
async function getFileInfo(baseDir, fullPath, fileTypeList) {
try {
const entries = await fsPromises.readdir(fullPath, { withFileTypes: true });
let item = {
json: '',
other: []
};
for (let el of entries) {
const childrenFullPath = path.resolve(fullPath, el.name);
const fileType = path.extname(el.name).substring(1)
if (fileType === 'json') {
const data = await fsPromises.readFile(childrenFullPath, 'utf8');
item.json = JSON.parse(data);
}
if (fileTypeList.includes(el.name)) {
item = {
...item,
name: el.name, // 名称
relativePath: getRelativePath(baseDir, childrenFullPath), // 统一使用相对路径
isDirectory: el.isDirectory(), // 是否是目录
type: path.extname(el.name).substring(1), // 后缀
};
} else if (!el.isDirectory()) {
item.other.push({
name: el.name, // 名称
relativePath: getRelativePath(baseDir, childrenFullPath), // 统一使用相对路径
isDirectory: el.isDirectory(), // 是否是目录
type: path.extname(el.name).substring(1), // 后缀
})
}
}
return item;
} catch (err) {
return {
error: err.message
};
}
}
const fileJosn = await getJSONContent(projectRoot, fileArr, mainFileList)
// const fileJosnPath = path.resolve(projectRoot, 'public/fileJosn.json');
// fs.writeFileSync(fileJosnPath, JSON.stringify(fileJosn));
/**
* 处理前缀
* @param {*} data 数据
* @returns
*/
function removePrefix(data) {
return data.map((e) => {
if (e.children && e.children.length > 0) {
return {
...e,
relativePath: e.relativePath.replace(prefix, ''),
children: removePrefix(e.children)
}
} else {
return {
...e,
relativePath: e.relativePath.replace(prefix, ''),
info: {
...e.info,
relativePath: e.info.relativePath.replace(prefix, ''),
other: e.info.other.map(item => {
return {
...item,
relativePath: item.relativePath.replace(prefix, '')
}
})
}
}
}
})
}
const newData = removePrefix(fileJosn)
// const newDataPath = path.resolve(projectRoot, 'public/newData.json');
// fs.writeFileSync(newDataPath, JSON.stringify(newData));
/**
* 生成json文件 root.json存放其他的json的路径
* @param {String} baseDir 项目根目录
* @param {Array} data 数据
* @returns root.json 文件
*/
function generateJsonFile(baseDir, data) {
const root = data.map(e => {
return {
name: e.name,
path: `/${e.name}.json`
}
})
const rootFilePath = path.resolve(baseDir, 'public/root.json');
fs.writeFileSync(rootFilePath, JSON.stringify(root));
data.forEach(e => {
const filePath = path.resolve(baseDir, `public/${e.name}.json`);
fs.writeFileSync(filePath, JSON.stringify(e.children));
})
}
generateJsonFile(projectRoot, newData)
console.log('生成json完成!!!');
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sk20020228/pieces-ui.git
git@gitee.com:sk20020228/pieces-ui.git
sk20020228
pieces-ui
pieces-ui
master

搜索帮助