dmikester1
dmikester115mo ago

dmikester1 – 03-42 May 1

I have some code that I think I need to promisify.
await compressFolder(PNGFolder, PNGFolder + '/slides.zip');
while (!fs.existsSync(PNGFolder + '/slides.zip')) {
console.log('Zip file does not exist');
}

await compressFolder(PNGFolder, PNGFolder + '/slides.zip');
while (!fs.existsSync(PNGFolder + '/slides.zip')) {
console.log('Zip file does not exist');
}

The compressFolder function is supposed to create that zip file. But when I run my code, it just sits and continuously outputs 'Zip file does not exist' in what looks like a forever loop.
2 Replies
dmikester1
dmikester115mo ago
I think the issue is that I need to promisify the compressFolder function. compressFolder looks like this:
export const compressFolder = async (srcDir, destFile) => {
const start = Date.now();
try {
fs.readdir(srcDir, async function (error, files) {
var totalFiles = files.length; // return the number of files
const zip = await createZipFromFolder(srcDir);
return zip
.generateNodeStream({ streamFiles: true, compression: 'DEFLATE' })
.pipe(fs.createWriteStream(destFile))
.on('error', (err) => console.error('Error writing file', err.stack))
.on('finish', () => {
console.log('Zip written successfully:', Date.now() - start, 'ms');
return { message: `Zip written successfully: ${Date.now() - start} ms`};
}
);
});
} catch (ex) {
console.error('Error creating zip', ex);
}
};
export const compressFolder = async (srcDir, destFile) => {
const start = Date.now();
try {
fs.readdir(srcDir, async function (error, files) {
var totalFiles = files.length; // return the number of files
const zip = await createZipFromFolder(srcDir);
return zip
.generateNodeStream({ streamFiles: true, compression: 'DEFLATE' })
.pipe(fs.createWriteStream(destFile))
.on('error', (err) => console.error('Error writing file', err.stack))
.on('finish', () => {
console.log('Zip written successfully:', Date.now() - start, 'ms');
return { message: `Zip written successfully: ${Date.now() - start} ms`};
}
);
});
} catch (ex) {
console.error('Error creating zip', ex);
}
};
reactibot
reactibot15mo ago
This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1102439766328348722