fixes asynchronous error with a pinky promise

pull/64/head
macniel 2025-11-01 11:08:29 +01:00
parent 7476c55e63
commit bb35d0d6e7
1 changed files with 24 additions and 17 deletions

View File

@ -46,8 +46,7 @@ function randomID(reference = "", length = 16) {
});
}
const convert = async function (from, to, ofType, overwrite = true) {
const convert = function (from, to, ofType, overwrite = true) {
const SOURCE = from;
const DEST = to;
const TYPE = ofType;
@ -60,6 +59,8 @@ const convert = async function (from, to, ofType, overwrite = true) {
mkdirSync(DEST)
}
let promises = []
const filewalker = async (source) => {
console.debug("entering directory", source);
for (let file of readdirSync(source)) {
@ -68,25 +69,31 @@ const convert = async function (from, to, ofType, overwrite = true) {
} else {
console.debug("processing file", join(source, file))
let originalSource = JSON.parse(readFileSync(join(source, file), {encoding: "utf8"}));
let id = await randomID("DSA_4-1" + TYPE + originalSource.name.trim())
promises.push(new Promise((resolve2) => {
randomID("DSA_4-1" + TYPE + originalSource.name.trim()).then(id => {
let targetSource = {
_id: id,
_key: "!items!" + id,
type: TYPE,
img: originalSource.image,
name: originalSource.name.trim(),
system: {...originalSource},
}
delete targetSource.system.image;
let target = JSON.stringify(targetSource, null, 2);
let newFileName = "./" + join(DEST, id + ".json");
writeFileSync(newFileName, target, {encoding: "utf8"});
resolve2()
})
let targetSource = {
_id: id,
_key: "!items!" + id,
type: TYPE,
img: originalSource.image,
name: originalSource.name.trim(),
system: {...originalSource},
}
delete targetSource.system.image;
let target = JSON.stringify(targetSource, null, 2);
let newFileName = "./" + join(DEST, id + ".json");
writeFileSync(newFileName, target, {encoding: "utf8"});
}
}))
}
}
}
return await filewalker(SOURCE)
filewalker(SOURCE)
return Promise.allSettled(promises)
}
function cleanDist() {