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.
D
dmikester1354d 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);
}
};
UU
Unknown User352d ago