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
/await
or promises
either, 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
/await
implementation.
Example outcomes
Before I wrapped the routine in a promise, the report of total bytes archived displayed after the next action started.
Preparing archive: ../archives/example-archive.zip
Trigger next action
291302 total bytes
now we can work
After I wrapped the routine in a promise, the report of total bytes archived displays before triggering the next action.
Preparing archive: ../archives/example-archive.zip
291302 total bytes
now we can work
Trigger next action
Demo code with Promise
const fs = require('fs-extra')
const archiver = require('archiver-promise')
/**
* Creates an archive of a folder of files. Could be modified to
* archive specific files from the source folder.
* @param {string} source_path - Full path to source folder
* @param {string} archive_filename - Full path to archive name. Must end with `.zip`
*/
const archive_folder = (source_path, archive_filename) => {
return new Promise((resolve, reject) => {
console.log(`Preparing archive: ${archive_filename}`)
const archive = archiver(archive_filename, {
store: true
})
archive.directory(source_path)
archive.finalize()
.then(() => {
console.log(`${archive.pointer()} total bytes`)
// Resolve with a status object.
resolve('now we can work')
})
.catch((err) => {
reject(err)
})
})
}
// Calling routine
archive_folder('../example/source/folder', '../archives/example-archive.zip')
.then((response) => {
console.log(response)
})
.then(() => {
console.log('Trigger next action')
// continue next actions...
})
.catch((err) => {
console.error(err)
})
Demo code without promise
/**
* Creates an archive of a folder of files without promises.
* @param {string} source_path - Full path to source folder
* @param {string} archive_filename - Full path to archive name. Must end with `.zip`
*/
const archive_folder_np = (source_path, archive_filename) => {
console.log(`Preparing archive: ${archive_filename}`)
const archive = archiver(archive_filename, {
store: true
})
archive.directory(source_path)
archive.finalize()
.then(() => {
console.log(`${archive.pointer()} total bytes`)
console.log('now we can work')
})
.catch((err) => {
console.error(err)
})
}
// Calling routine
archive_folder_np('../example/source/folder', '../archives/example-archive.zip')
console.log('Trigger next action')