utils-99

const fs = require('fs');

Usage no npm install needed!

<script type="module">
  import utils99 from 'https://cdn.skypack.dev/utils-99';
</script>

README

文件夹转json

const fs = require('fs');

function dirtojson(dir) {

    let stat = fs.statSync(dir);
    if (stat.isFile()) {
        return {
            name: dir,
            isfile: true,
            content: fs.readFileSync(dir, 'utf8'),
            ext: dir.slice(dir.lastIndexOf('.')),

        }
    } else {
        return {
            name: dir,
            isfile: false,
            content: fs.readdirSync(dir).map(item => {
                return dirtojson(dir + '/' + item);
            })
        }
    }
}
let data = JSON.stringify(dirtojson('2'));
fs.writeFileSync('index.json', data);