Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaredWilcurt committed Dec 17, 2023
1 parent bd856a4 commit 8e5d00b
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 27 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

/**
* @file ESLint setup
* @author TheJaredWilcurt
* @file ESLint setup
*/

const path = require('path');
Expand Down
3 changes: 1 addition & 2 deletions api-type-definitions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* @file Type definitions for the API and reusable functions/objects
* @author TheJaredWilcurt
* @file Type definitions for the API and reusable functions/objects.
*/

/**
Expand Down
42 changes: 36 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

/**
* @file Entry point for the library. Exposes the external facing function that accepts the input defined in the API documentation.
* @author TheJaredWilcurt
* @file Entry point for the library. Exposes the external facing function that
* accepts the input defined in the API documentation.
*/

/* eslint-disable */
Expand All @@ -11,26 +11,56 @@ const { OPTIONS } = require('./api-type-definitions.js');

const validation = require('./src/validation.js');
const getVersionUrl = require('./src/getVersionUrl.js');
const getLatestLocal = require('./src/getLatestLocal.js');

function stub () {}

const nwSplasherAutoUpdate = {
/**
* [description].
* Checks for updates. Downloads zip and extracts it if
* new version is available. Launches a window pointed
* to the latest version.
*
* @param {OPTIONS} options [description].
* @param {OPTIONS} options The user's options object
*/
downloadLatestAppAndOpenWindowInBackground: async function (options) {
// Validate options
options = validation.validateDownloadLatestAppAndOpenWindowInBackgroundOptions(options);
const data = await getVersionUrl(options);
console.log({ data });

// Get remote version data
const versionUrlResponse = await getVersionUrl(options);

// Get latest local
const latestLocal = await getLatestLocal(options);

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

console.log({ latestRemote, latestLocal });

// get download path
stub();

// download zip
stub();

// validate zip
stub();

// extract zip
stub();

// validate extraction
stub();

// Update/Retry/Error/Complete
stub();

// close splash
stub();

// open app window
stub();
},
setCurrentWorkingDirectory: function () {},
closeSplashAndShowApp: function (options) {
Expand Down
163 changes: 160 additions & 3 deletions manual-testing.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict';

/**
* @file Used for manually testing the code
* @author TheJaredWilcurt
* @file Used for manually testing the code
*/

const library = require('./index.js');

Check failure on line 7 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

There should be at least one empty line between import groups
const semver = require('semver');

Check failure on line 8 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

`semver` import should occur before import of `./index.js`

const options = {
let options = {
autoUpdate: {
versionUrl: 'https://api.github.com/repos/scout-app/scout-app/releases',
confirmNewVersion: function (response, latestLocal) {
Expand All @@ -27,4 +26,162 @@ const options = {
}
};

options = {
// OPTIONAL: defaults to true
verbose: true,
/**
* OPTIONAL: console.error is called by default if verbose: true.
*
* Your own custom logging function called with helpful warning/error
* messages from the internal validators. Only used if verbose: true.
*
* @param {string} message The human readable warning/error message
* @param {object} error Sometimes an error or options object is passed
*/
customLogger: function (message, error) {
console.log('customLogger', message, error);
},
splasher: {
/**
* The websocket port the splash screen window listens on. When your main app window
* loads, it will use the same port to signal the splash screen to close itself.
*/
port: 4443,
/**
* Time in ms to wait after the new window is launched before auto-closing
* splash screen if a signal is not recieved to close sooner. Send -1 to
* only close if signaled via the websocket port.
*/
closeSplashAfter: 3000
},
autoUpdate: {
/**
* NW.js Splasher Auto Update will make a network request to the versionUrl,
* then pass the response into the confirmNewVersion and downloadPath
* callback functions you provide.
*/
// This is an example, you can put whatever URL you want here
versionUrl: 'https://api.github.com/repos/scout-app/scout-app/releases',
/**
* Check if the latest remote version is newer than the latest local version.
* If a new version is available, return the new version number to begin the
* download/extract. If no new version exists, then return false and the latest
* local version will be opened in a new window and the splash screen closed.
*
* @param {string} response The network respone from versionUrl
* @param {string} latestLocal The latest downloaded version, or undefined if not present
* @return {string} The new version number (continue to download zip), or false (open current version)
*/
confirmNewVersion: function (response, latestLocal) {
console.log('confirmNewVersion');
const latestRemote = response.data[0].tag_name.replace('v', '');
const updateAvailable = semver.gt(latestRemote, latestLocal);
if (updateAvailable) {
return latestRemote;
}
return false;
},
/**
* Called after hitting the versionUrl with the response.

Check warning on line 85 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

JSDoc description does not satisfy the regex pattern
* Must return a url to a ZIP file to be downloaded/extracted
*
* @param {string} response The response body from the network request
* @return {string} A url to a ZIP file to be downloaded
*/
downloadPath: function (response) {
console.log('downloadPath');
//This is just an example, you can put any logic you want here

Check failure on line 93 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space or tab after '//' in comment
response = JSON.parse(response);
return response.latest.downloadUrl;
},
// If the download or extract fails, we will retry n times before stopping
downloadRetries: 3,
extractRetries: 3,
/**
* Called when an update occurs during download/extract.
*
* @param {object} update Object containing percents
* @param {number} update.downloadProgress The download progress percent
* @param {number} update.extractProgress The extract progress percent
*/
onUpdate: function ({ downloadProgress, extractProgress }) {
console.log('onUpdate');
//This is just an example, you can put any logic you want here

Check failure on line 109 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space or tab after '//' in comment
if (downloadProgress) {
console.log('Download progress: ' + downloadProgress + '%');
}
if (extractProgress) {
console.log('Unzipping: ' + downloadProgress + '%');
}
},
/**
* Optional function. You can run any code to validate
* that the downloaded zip matches expecations.
* If it does return true. If you return false, then
* nwSplasherAutoUpdate will retry or stop running.
*
* @param {string} pathToZip File path to the downloaded zip file
* @return {Boolean} true = continue, false = retry/stop

Check warning on line 124 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Invalid JSDoc @return type "Boolean"; prefer: "boolean"
*/
validateZip: function (pathToZip) {

Check failure on line 126 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'pathToZip' is defined but never used
console.log('validateZip');
//This is just an example, you can put any logic you want here

Check failure on line 128 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space or tab after '//' in comment
return true;
},
/**
* Optional function. You can run any code to validate
* that the files extracted from the zip match your
* expecations. If they do return true. If you return false,
* then nwSplasherAutoUpdate will retry or stop running.
*
* @param {string} pathToExtract File path to the downloaded zip file
* @return {Boolean} true = continue, false = retry/stop

Check warning on line 138 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Invalid JSDoc @return type "Boolean"; prefer: "boolean"
*/
validateExtract: function (pathToExtract) {

Check failure on line 140 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'pathToExtract' is defined but never used
console.log('validateExtract');
//This is just an example, you can put any logic you want here

Check failure on line 142 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space or tab after '//' in comment
return true;
},
/**
* When download or extract fails, but we haven't
* exhausted all retries yet, this is called.
*
* @param {string} message Human readable warning message
*/
onRetry: function (message) {
//This is just an example, you can put any logic you want here

Check failure on line 152 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space or tab after '//' in comment
console.log('onRetry', message);
},
/**
* Called when an error is encountered that ended execution
* prematurely. Such as failing to download or extract after
* all retries were exhausted.
*
* @param {string} errorMessage Human readable error message
* @param {object} error Detailed error information if available
*/
onError: function (errorMessage, error) {
//This is just an example, you can put any logic you want here

Check failure on line 164 in manual-testing.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space or tab after '//' in comment
console.log('onError', errorMessage, error);
},
/**
* Called just prior to opening the new window
* and closing the splash screen.
*/
onComplete: function () {
console.log('onComplete');
}
},
newWindow: {
// The file from the extracted zip to load in the new window
entry: 'index.html',
// Any NW.js window subfield options: docs.nwjs.io/en/latest/References/Manifest%20Format/#window-subfields
window: {
min_width: 400,
min_height: 250
}
}
};


library.downloadLatestAppAndOpenWindowInBackground(options);
3 changes: 1 addition & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

/**
* @file Defines shared constant variables
* @author TheJaredWilcurt
* @file Defines shared constant variables
*/

const DEFAULT_CLOSE_SPLASH_AFTER = 3000;
Expand Down
29 changes: 29 additions & 0 deletions src/getLatestLocal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

/**
* @file Checks the most recent local version of the app
*/

const { OPTIONS } = require('../api-type-definitions.js');

const helpers = require('./helpers.js');

/**

Check warning on line 11 in src/getLatestLocal.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing JSDoc block description
*

Check warning on line 12 in src/getLatestLocal.js

View workflow job for this annotation

GitHub Actions / build (18.x)

JSDoc description does not satisfy the regex pattern
*
* @param {OPTIONS} options User's validated options
* @return {Promise} .
*/
async function getLatestLocal (options) {
return new Promise((resolve, reject) => {
let latestLocal = '0.0.0';
if (latestLocal) {
resolve(latestLocal);
} else {
helpers.throwError(options, 'Error getting latest local version number', error);
reject();
}
});
};

module.exports = getLatestLocal;
3 changes: 1 addition & 2 deletions src/getVersionUrl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

/**
* @file Hits a provided endpoint and returns the data containing version info
* @author TheJaredWilcurt
* @file Hits a provided endpoint and returns the data containing version info
*/

const { OPTIONS } = require('../api-type-definitions.js');
Expand Down
3 changes: 1 addition & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

/**
* @file File contains helper functions used by different files in the library.
* @author TheJaredWilcurt
* @file File contains helper functions used by different files in the library.
*/

const helpers = {
Expand Down
5 changes: 3 additions & 2 deletions src/validation.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

/**
* @file This file validates the options object passed in by the user of this library to ensure it meets the expectations of the code.
* @author TheJaredWilcurt
* @file This file validates the options object passed in by the user of
* this library to ensure it meets the expectations of the code.
* It also provides default values for options where possible.
*/

const {
Expand Down
3 changes: 1 addition & 2 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

/**
* @file Vitest setup file
* @author TheJaredWilcurt
* @file Vitest setup file
*/
3 changes: 1 addition & 2 deletions tests/src/validation.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

/**
* @file Tests the validation file's functions
* @author TheJaredWilcurt
* @file Tests the validation file's functions
*/

const helpers = require('../../src/helpers.js');
Expand Down
3 changes: 1 addition & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

/**
* @file Vite config
* @author TheJaredWilcurt
* @file Configuration file for Vite to set up unit tests.
*/

/* eslint-disable import/extensions,import/no-unresolved,import/no-anonymous-default-export,import/no-unused-modules */
Expand Down

0 comments on commit 8e5d00b

Please sign in to comment.