Skip to content

Commit

Permalink
extract zip
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaredWilcurt committed Jan 1, 2024
1 parent 4809174 commit e684512
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 6 deletions.
5 changes: 3 additions & 2 deletions nw-testing/manual-testing.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ function manualTesting () {
* @return {boolean} true = continue, false = retry/stop
*/
validateZip: function (pathToZip) {
console.log('Validate zip exists', pathToZip, fs.existsSync(pathToZip));
const exists = fs.existsSync(pathToZip);
console.log('Validate zip exists', pathToZip, exists);
// This is just an example, you can put any logic you want here
return true;
return exists;
},
/**
* Optional function. You can run any code to validate
Expand Down
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test": "vitest --coverage"
},
"dependencies": {
"7zip-min": "^1.4.4",
"axios": "^1.6.2",
"easydl": "^1.1.1",
"nw-splasher": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

/**
* @file Checks the most recent local version of the app
* @file Attempt to download the zip file.
*/

const EasyDl = require('easydl');
Expand Down
73 changes: 73 additions & 0 deletions src/downloadLatestAppAndOpenWindowInBackground/extractZip.cjs
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;
7 changes: 4 additions & 3 deletions src/downloadLatestAppAndOpenWindowInBackground/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { OPTIONS } = require('../../api-type-definitions.cjs');
const helpers = require('../helpers.cjs');

const downloadZip = require('./downloadZip.cjs');
const extractZip = require('./extractZip.cjs');
const getLatestLocal = require('./getLatestLocal.cjs');
const getVersionUrl = require('./getVersionUrl.cjs');
const validation = require('./validation.cjs');
Expand Down Expand Up @@ -37,7 +38,7 @@ async function downloadLatestAppAndOpenWindowInBackground (options) {
// confirm version
const latestRemote = await options.autoUpdate.confirmNewVersion(versionUrlResponse, latestLocal);

if (!latestRemote) {
if (!latestRemote || !['number', 'string'].includes(typeof(latestRemote))) {
return;
}

Expand All @@ -50,12 +51,12 @@ async function downloadLatestAppAndOpenWindowInBackground (options) {

// download zip
await downloadZip(options, downloadPath, latestRemote);
const zipFilePath = helpers.getZipFilePath(latestRemote);

// validate zip
let zipIsValid = true;
if (options.autoUpdate.validateZip) {
try {
const zipFilePath = helpers.getZipFilePath(latestRemote);
zipIsValid = await options.autoUpdate.validateZip(zipFilePath);
} catch (error) {
zipIsValid = false;
Expand All @@ -68,7 +69,7 @@ async function downloadLatestAppAndOpenWindowInBackground (options) {
}

// extract zip
stub();
await extractZip(latestRemote);

// validate extraction
stub();
Expand Down

0 comments on commit e684512

Please sign in to comment.