28 lines
965 B
JavaScript
28 lines
965 B
JavaScript
import { readdir, readFile, writeFile, cp } from 'node:fs/promises';
|
|
|
|
try {
|
|
const dirs = await readdir('.', { withFileTypes: true });
|
|
for (const dir of dirs) {
|
|
if (dir.path.startsWith('.')) continue
|
|
if (dir.isDirectory()) {
|
|
let pack
|
|
try {
|
|
const packFile = (await readFile(dir.path + '/pack.json')).toString()
|
|
pack = JSON.parse(packFile).pack
|
|
} catch {
|
|
continue
|
|
}
|
|
const contents = await readdir(dir.path, { withFileTypes: true });
|
|
const files = Object.fromEntries(
|
|
contents
|
|
.map(x => [x.name.replace(/\.\w+$/, ''), x.name])
|
|
.filter(([k, v]) => v.match(/\.(a?png|jpe?g|gif)$/) != null)
|
|
)
|
|
cp(dir.path + '/pack.json', dir.path + '/pack_backup.json')
|
|
const output = { files, pack, files_count: Object.keys(files).length }
|
|
await writeFile(dir.path + '/pack.json', JSON.stringify(output, null, 2))
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|