untio-hua-tojsonplus
v1.0.0
Published
```javascript const fs = require('fs'); function toJson(dirName) { //判断是否为文件 let stat = fs.statSync(dirName); if (stat.isFile()) { //是文件 return { name: dirName, isFile: true,
Downloads
6
Readme
##文件夹转Json
const fs = require('fs');
function toJson(dirName) {
//判断是否为文件
let stat = fs.statSync(dirName);
if (stat.isFile()) {
//是文件
return {
name: dirName,
isFile: true,
content: fs.readFileSync(dirName, 'utf8'),
ext: dirName.slice(dirName.lastIndexOf('.')),
}
} else {
//是文件夹
return {
name: dirName,
isFile: false,
children: fs.readdirSync(dirName).map(item => {
return toJson(dirName + '/' + item);
})
}
}
}
let data = toJson('1');
fs.writeFileSync('data.json', JSON.stringify(data));