1.0.1 • Published 4 years ago

demo-1809b v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

目录转化 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))
1.0.1

4 years ago

1.0.0

4 years ago