Skip to content

Commit

Permalink
getLatestLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaredWilcurt committed Jan 2, 2024
1 parent bb419bf commit 3eec70d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
26 changes: 18 additions & 8 deletions src/downloadLatestAppAndOpenWindowInBackground/getLatestLocal.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

const fs = require('fs');

const semver = require('semver');

const { OPTIONS } = require('../../api-type-definitions.cjs');
const { EXTRACTS_LOCATION } = require('../constants.cjs');
const helpers = require('../helpers.cjs');
Expand All @@ -19,19 +21,27 @@ const helpers = require('../helpers.cjs');
* @return {string} The most recent local version number, or false
*/
async function getLatestLocal (options) {
console.log('The getLatestLocal function is a stub and needs implemented');

let latestLocal = '0.0.0';
let latestLocal;
let error;
const errorMessage = 'Error getting latest local version number';

try {
if (fs.existsSync(EXTRACTS_LOCATION)) {
const folders = fs.readdirSync(EXTRACTS_LOCATION);
latestLocal = semver.maxSatisfying(folders, '>=0');
}
} catch (err) {
error = err;
}

if (fs.existsSync(EXTRACTS_LOCATION)) {
console.log(fs.readdirSync(EXTRACTS_LOCATION));
if (error) {
helpers.throwError(options, errorMessage, error);
}

if (latestLocal) {
return latestLocal;
} else {
let error;
helpers.throwError(options, 'Error getting latest local version number', error);
} else if (!error) {
helpers.throwError(options, errorMessage);
}
return false;
};
Expand Down
6 changes: 4 additions & 2 deletions src/downloadLatestAppAndOpenWindowInBackground/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ async function downloadLatestAppAndOpenWindowInBackground (options) {
const latestLocal = await getLatestLocal(options);

// confirm version
const latestRemote = await options.autoUpdate.confirmNewVersion(versionUrlResponse, latestLocal);
let latestRemote = await options.autoUpdate.confirmNewVersion(versionUrlResponse, latestLocal);
latestRemote = validation.validateLatestRemote(options, latestRemote);

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

Expand Down
27 changes: 27 additions & 0 deletions src/downloadLatestAppAndOpenWindowInBackground/validation.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* It also provides default values for options where possible.
*/

const semver = require('semver');

const { OPTIONS } = require('../../api-type-definitions.cjs');
const {
DEFAULT_CLOSE_SPLASH_AFTER,
Expand Down Expand Up @@ -186,6 +188,31 @@ const validation = {

return options;
},
/**
* Validates the latestRemote string is a valid semver value.
*
* @param {OPTIONS} options The user's options object
* @param {string} latestRemote The semver string for the latest remote version of the app
* @return {string} The latestRemote string, or false if string is invalid
*/
validateLatestRemote: function (options, latestRemote) {
if (!latestRemote) {
return false;
}
const isValid = !!semver.valid(latestRemote);
if (!isValid) {
const message = [
'The latest remote version',
'(' + String(latestRemote) + ')',
'must be a valid semver string',
'(Ex: \'1.0.0\').',
'See https://semver.org for details.'
].join(' ');
helpers.throwError(options, message);
return false;
}
return latestRemote;
},

// Generic
/**
Expand Down

0 comments on commit 3eec70d

Please sign in to comment.