-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
29 lines (27 loc) · 1020 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const fs = require('fs')
module.exports.update = async function update() {
const sorter = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' })
const checkFolders = async (folder) => {
folder = folder.replace('//', '/')
const files = await fs.promises.readdir(folder)
let newJson = {}
let hasSubFolders = false
await Promise.all(files.map(async file => {
if (!file.includes('.')) {
hasSubFolders = true
newJson[file] = await checkFolders(`${folder}/${file}`)
}
}))
if (!hasSubFolders) {
newJson = files.filter(file => file.includes('.png')).sort(sorter.compare)
} else {
const tempJson = newJson
const sortedKeys = Object.keys(newJson).sort(sorter.compare)
newJson = {}
sortedKeys.forEach(key => (newJson[key] = tempJson[key]))
}
fs.writeFile(`./${folder === './' ? '' : `${folder}/`}index.json`, JSON.stringify(newJson, null, 2), 'utf8', () => {})
return newJson
}
await checkFolders('./')
}