-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4809174
commit e684512
Showing
6 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/downloadLatestAppAndOpenWindowInBackground/extractZip.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @file Attempt to extract the contents of the zip file | ||
*/ | ||
|
||
const _7z = require('7zip-min'); | ||
|
||
const { | ||
OPTIONS | ||
} = require('../../api-type-definitions.cjs'); | ||
const helpers = require('../helpers.cjs'); | ||
|
||
/** | ||
* Extracts the zip file contents, retries in case of failure. | ||
* | ||
* @param {OPTIONS} options The user's options object | ||
* @param {string} latestRemote The latest version number of the app we are downloading | ||
* @return {boolean} True = successful extract, False = failed to unzip | ||
*/ | ||
async function extractZip (options, latestRemote) { | ||
const zipFilePath = helpers.getZipFilePath(latestRemote); | ||
const extractFilePath = helpers.getExtractPath(latestRemote); | ||
const maxRetries = options.autoUpdate.extractRetries; | ||
|
||
let unzippedSuccessfully = false; | ||
|
||
function unpack () { | ||
return new Promise((resolve, reject) => { | ||
_7z.unpack(zipFilePath, extractFilePath, (error) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
async function unzip (attempts) { | ||
attempts = attempts || 0; | ||
try { | ||
const success = await unpack(); | ||
if (success === true) { | ||
unzippedSuccessfully = true; | ||
} | ||
} catch (error) { | ||
const lastFailure = attempts === maxRetries; | ||
|
||
if (lastFailure) { | ||
const message = 'Could not extract contents of zip file'; | ||
const details = { | ||
attempts, | ||
error, | ||
extractFilePath, | ||
zipFilePath | ||
}; | ||
if (options.autoUpdate.onError) { | ||
options.autoUpdate.onError(message, details); | ||
} | ||
helpers.throwError(message, details); | ||
} else { | ||
unzip(attempts + 1); | ||
} | ||
} | ||
} | ||
|
||
await unzip(); | ||
|
||
return unzippedSuccessfully; | ||
}; | ||
|
||
module.exports = extractZip; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters