I want to zip a folder and then I have to delete it in a Gulp task. I'm trying to use archiver in a synchronous way.
I use the code like in the quick start guide in the npm page of archiver and it successfully create the zip file.
But if i try also to delete the zipped folder, then archiver has no enough time to create the zip.
const output = fs.createWriteStream(__dirname + '/example.zip');
//
// code as in https://www.npmjs.com/package/archiver
// ...
archive.finalize();
execSync(
`rm -rf ${config.paths.dest}`,
);
Now I switched to zip-local package and it works in a synch way so I "solved" the problem, but now I'm forced to zip the entire folder, instead of selecting specific files and folders and the output zip is too much bigger.
I'm not an expert and I'm sure this is a problem caused only from my limits in understanding how archiver works.
Could someone help me?
Using archiver, I succeded in doing what I want, deleting the folder inside the "close" event:
output.on('close', function () {
console.log(`Zip created`);
execSync(
`rm -rf ${config.paths.dest}`,
);
});
But I don't like very much this solution.
I tried to use promises and watched a lot o videos but it is a concept that can not remain in my brain and I'm a little bit frustrated.
I don't know if this helps you, but I found archiver-promise from 2016/11/23 that wraps archiver in a promise.
I'm no expert in
async/awaitorpromiseseither, so my code might not be the greatest implementation. But it allows me to wrap the archiver to return a promise that I can watch before proceeding to the next step.Posting here for others to validate. I'd really prefer an
async/awaitimplementation.Example outcomes
Before I wrapped the routine in a promise, the report of total bytes archived displayed after the next action started.
After I wrapped the routine in a promise, the report of total bytes archived displays before triggering the next action.
Demo code with Promise
Demo code without promise