su-wenjianjiashujuhua
v1.0.0
Published
```javascript const fs = require("fs"); const path = require("path"); const dirTOJSON = (dir) => { //完成代码 const isFile = fs.statSync(dir).isFile(); if (isFile) { //是文件 return { name: path.basename(dir),
Downloads
1
Readme
子目录数据化
const fs = require("fs");
const path = require("path");
const dirTOJSON = (dir) => {
//完成代码
const isFile = fs.statSync(dir).isFile();
if (isFile) {
//是文件
return {
name: path.basename(dir),
type: "files",
}
} else {
//是文件夹
return {
name: path.basename(dir),
patgname: dir,
type: "dir",
children: fs.readdirSync(dir).map(item => {
const realPath = path.join(dir, item)
return dirTOJSON(realPath)
})
}
}
}
let o = dirTOJSON("d");
fs.writeFileSync("data.json", JSON.stringify(o))