Skip to content

Instantly share code, notes, and snippets.

@Saszr
Last active May 28, 2021 07:01
Show Gist options
  • Save Saszr/43e5c802f645c6b149484468e03ec497 to your computer and use it in GitHub Desktop.
Save Saszr/43e5c802f645c6b149484468e03ec497 to your computer and use it in GitHub Desktop.
node 生成目录树形结构
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const { v4: uuidv4 } = require('uuid');
const rootPath = path.resolve(__dirname, '../');
const tplDirPath = path.join(rootPath, 'lib');
const fileDisplay = (targetRootPath) => {
const fileArr = [];
/**
* @Description: 扁平化的树形数据
* @param {*} targetPath
* @param {*} parentId
* @return {*}
*/
const getFileInfo = (targetPath, parentId) => {
let files;
try {
files = fs.readdirSync(targetPath);
} catch (err) {
console.warn(err);
}
const childrenArr = files.map((file) => {
const fileDir = path.join(targetPath, file);
let fileStat;
try {
fileStat = fs.statSync(fileDir);
} catch (err) {
console.warn(err);
}
const isFile = fileStat.isFile();
const isDir = fileStat.isDirectory();
const fileInfo = {
name: file,
src: path.relative(__dirname, fileDir),
uuid: uuidv4(),
parentId: _.isUndefined(parentId) ? '' : parentId,
type: isFile ? path.extname(fileDir).slice(1, path.extname(fileDir).length) : 'dir',
};
if (isDir) {
let cFiles;
try {
cFiles = fs.readdirSync(fileDir);
} catch (err) {
console.warn(err);
}
if (!_.isEmpty(cFiles)) {
getFileInfo(fileDir, fileInfo.uuid);
}
}
return fileInfo;
});
fileArr.push(...childrenArr);
};
getFileInfo(targetRootPath);
const setFileTreeData = (fileData) => {
const map = fileData.reduce((acc, val) => {
acc[val.uuid] = val;
return acc;
}, {});
const tree = [];
fileData.forEach((region) => {
if (!_.isEmpty(region.parentId)) {
const parent = map[region.parentId];
if (!parent.children) {
parent.children = [region];
} else {
parent.children.push(region);
}
} else {
tree.push(region);
}
});
return tree;
};
const fileTreeArr = setFileTreeData(fileArr);
return fileTreeArr;
};
const t = JSON.stringify(fileDisplay(tplDirPath));
fs.writeFileSync('./config/file.json', t);
[
{
"name": "BusinessForm",
"src": "../lib/BusinessForm",
"uuid": "88ea1fae-93ca-47bb-94ef-6b195f77c757",
"parentId": "",
"type": "dir",
"children": [
{
"name": "test.js",
"src": "../lib/BusinessForm/test.js",
"uuid": "84409ba4-917d-41f9-bab8-fc2d0f97aaa1",
"parentId": "88ea1fae-93ca-47bb-94ef-6b195f77c757",
"type": "js"
}
]
},
{
"name": "Dashboard",
"src": "../lib/Dashboard",
"uuid": "5ff7a747-1262-461c-b71c-98317a6e9923",
"parentId": "",
"type": "dir",
"children": [
{
"name": "map",
"src": "../lib/Dashboard/map",
"uuid": "d908b670-7ad7-4aef-a267-60f598442236",
"parentId": "5ff7a747-1262-461c-b71c-98317a6e9923",
"type": "dir",
"children": [
{
"name": "test.js",
"src": "../lib/Dashboard/map/test.js",
"uuid": "130b6684-916f-43a4-9857-3c0e32fe2dda",
"parentId": "d908b670-7ad7-4aef-a267-60f598442236",
"type": "js"
}
]
},
{
"name": "test.js",
"src": "../lib/Dashboard/test.js",
"uuid": "42a792ee-9191-494e-b760-97cf3c0f8f65",
"parentId": "5ff7a747-1262-461c-b71c-98317a6e9923",
"type": "js"
}
]
},
{
"name": "DataSheet",
"src": "../lib/DataSheet",
"uuid": "3f7fd00d-18e9-45f4-a638-aeb8b7c26c78",
"parentId": "",
"type": "dir",
"children": [
{
"name": "test.js",
"src": "../lib/DataSheet/test.js",
"uuid": "663284a5-12fd-45a7-a119-a0f538858ad3",
"parentId": "3f7fd00d-18e9-45f4-a638-aeb8b7c26c78",
"type": "js"
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment