From 396aaaf6e9c8adc31d468762736bbfe3d41a5872 Mon Sep 17 00:00:00 2001 From: BlipRanger <1860540+BlipRanger@users.noreply.github.com> Date: Wed, 12 Jul 2023 22:26:01 -0400 Subject: [PATCH] Add update button and check for external extenisons --- public/script.js | 13 +++++++------ public/scripts/extensions.js | 23 ++++++++++++----------- server.js | 34 +++++++++++++++++++++------------- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/public/script.js b/public/script.js index ce84993a5..7335df735 100644 --- a/public/script.js +++ b/public/script.js @@ -8496,10 +8496,10 @@ $(document).ready(function () { $('#third_party_extension_button').on('click', async () => { const html = `

Enter the Git URL of the extension to import

-
-

Disclaimer: Please be aware that using external extensions can have unintended side effects and may pose security risks. Always make sure you trust the source before importing an extension. We are not responsible for any damage caused by third-party extensions.

-
-

Example: https://github.com/author/extension-name

` +
+

Disclaimer: Please be aware that using external extensions can have unintended side effects and may pose security risks. Always make sure you trust the source before importing an extension. We are not responsible for any damage caused by third-party extensions.

+
+

Example: https://github.com/author/extension-name

` const input = await callPopup(html, 'input'); if (!input) { @@ -8523,12 +8523,13 @@ $(document).ready(function () { } const response = await request.json(); - await loadExtensionSettings(settings); - eventSource.emit(event_types.EXTENSION_SETTINGS_LOADED); toastr.success(`Extension "${response.display_name}" by ${response.author} (version ${response.version}) has been imported successfully!`, 'Extension import successful'); console.debug(`Extension "${response.display_name}" has been imported successfully at ${response.extensionPath}`); + await loadExtensionSettings(settings); + eventSource.emit(event_types.EXTENSION_SETTINGS_LOADED); }); + const $dropzone = $(document.body); $dropzone.on('dragover', (event) => { diff --git a/public/scripts/extensions.js b/public/scripts/extensions.js index dc8dfbe89..c551cc971 100644 --- a/public/scripts/extensions.js +++ b/public/scripts/extensions.js @@ -378,12 +378,13 @@ function addExtensionScript(name, manifest) { - -function showExtensionsDetails() { +async function showExtensionsDetails() { let htmlDefault = '

Default Extensions:

'; let htmlExternal = '

External Extensions:

'; - Object.entries(manifests).sort((a, b) => a[1].loading_order - b[1].loading_order).forEach(extension => async () =>{ + const extensions = Object.entries(manifests).sort((a, b) => a[1].loading_order - b[1].loading_order); + + for (const extension of extensions) { const name = extension[0]; const manifest = extension[1]; const isActive = activeExtensions.has(name); @@ -394,23 +395,23 @@ function showExtensionsDetails() { const checkboxClass = isDisabled ? "checkbox_disabled" : ""; const displayName = manifest.display_name; - // if external, get the version from a requst to get_extension_verion let displayVersion = manifest.version ? ` v${manifest.version}` : ""; - if(isExternal) { + let isUpToDate = true; + if (isExternal) { let data = await getExtensionVersion(name.replace('third-party', '')); let branch = data.currentBranchName; let commitHash = data.currentCommitHash; - displayVersion = ` v${branch}-${commitHash}`; + isUpToDate = data.isUpToDate; + displayVersion = ` (${branch}-${commitHash.substring(0, 7)})`; } - let toggleElement = ``; if (isActive || isDisabled) { toggleElement = ``; } - let updateButton = isExternal ? `` : ''; - let extensionHtml = `

${DOMPurify.sanitize(displayName)}${displayVersion} ${toggleElement}${updateButton}

`; + let updateButton = isExternal && !isUpToDate ? `` : ''; + let extensionHtml = `

${updateButton} ${DOMPurify.sanitize(displayName)}${displayVersion} ${toggleElement}

`; if (isActive) { if (Array.isArray(manifest.optional)) { @@ -435,8 +436,8 @@ function showExtensionsDetails() { } else { htmlDefault += extensionHtml; } - }); - + } + // Do something with htmlDefault and htmlExternal here let html = '

Modules provided by your Extensions API:

'; html += modules.length ? `

${DOMPurify.sanitize(modules.join(', '))}

` : '

Not connected to the API!

'; html += htmlDefault + htmlExternal; diff --git a/server.js b/server.js index f857bf9d1..1e501e8f9 100644 --- a/server.js +++ b/server.js @@ -4390,6 +4390,17 @@ async function getManifest(extensionPath) { return manifest; } +async function checkIfRepoIsUpToDate(extensionPath) { + const currentBranch = await git.cwd(extensionPath).branch(); + const currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); + const log = await git.cwd(extensionPath).log({ + from: currentCommitHash, + to: `origin/${currentBranch.current}`, + }); + + return log.total === 0; +} + /** * HTTP POST handler function to clone a git repository from a provided URL, read the extension manifest, * and return extension information and path. @@ -4449,19 +4460,14 @@ app.post('/update_extension', jsonParser, async (request, response) => { return response.status(404).send(`Directory does not exist at ${extensionPath}`); } - const currentBranch = await git.cwd(extensionPath).branch(); - const currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); - const isUpToDate = await git.cwd(extensionPath).log({ - from: currentCommitHash, - to: `origin/${currentBranch.current}`, - }); + const isUpToDate = await checkIfRepoIsUpToDate(extensionPath); - if (isUpToDate.total === 0) { - await git.cwd(extensionPath).pull('origin', currentBranch.current); - console.log(`Extension has been updated at ${extensionPath}`); - } else { - console.log(`Extension is up to date at ${extensionPath}`); - } + if (!isUpToDate) { + await git.cwd(extensionPath).pull('origin', currentBranch.current); + console.log(`Extension has been updated at ${extensionPath}`); + } else { + console.log(`Extension is up to date at ${extensionPath}`); + } const fullCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); const shortCommitHash = fullCommitHash.slice(0, 7); @@ -4495,7 +4501,9 @@ app.post('/get_extension_version', jsonParser, async (request, response) => { const currentBranchName = currentBranch.current; const currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); console.log(currentBranch, currentCommitHash); - return response.send({ currentBranchName, currentCommitHash }); + const isUpToDate = await checkIfRepoIsUpToDate(extensionPath); + + return response.send({ currentBranchName, currentCommitHash, isUpToDate }); } catch (error) { console.log('Getting extension version failed', error);