demo-1809b
v1.0.1
Published
```javascript const fs = require("fs"); const path = require("path"); const dirToJSON = (dir) => { // 完成代码 const isFile = fs.statSync(dir).isFile();
Downloads
2
Readme
目录转化 json
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),
pathname: dir,
type: "dir",
children: fs.readdirSync(dir).map(item => {
const realPath = path.join(dir, item)
return dirToJSON(realPath)
})
}
}
}
let o = dirToJSON("node_modules");
fs.writeFileSync("data.json", JSON.stringify(o))