Add update button and check for external extenisons

This commit is contained in:
BlipRanger
2023-07-12 22:26:01 -04:00
parent 27e3485127
commit 396aaaf6e9
3 changed files with 40 additions and 30 deletions

View File

@@ -8496,10 +8496,10 @@ $(document).ready(function () {
$('#third_party_extension_button').on('click', async () => {
const html = `<h3>Enter the Git URL of the extension to import</h3>
<br>
<p><b>Disclaimer:</b> 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.</p>
<br>
<p>Example: <tt> https://github.com/author/extension-name </tt></p>`
<br>
<p><b>Disclaimer:</b> 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.</p>
<br>
<p>Example: <tt> https://github.com/author/extension-name </tt></p>`
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) => {

View File

@@ -378,12 +378,13 @@ function addExtensionScript(name, manifest) {
function showExtensionsDetails() {
async function showExtensionsDetails() {
let htmlDefault = '<h3>Default Extensions:</h3>';
let htmlExternal = '<h3>External Extensions:</h3>';
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 = `<input type="checkbox" title="Cannot enable extension" data-name="${name}" class="extension_missing ${checkboxClass}" disabled>`;
if (isActive || isDisabled) {
toggleElement = `<input type="checkbox" title="Click to toggle" data-name="${name}" class="${isActive ? 'toggle_disable' : 'toggle_enable'} ${checkboxClass}" ${isActive ? 'checked' : ''}>`;
}
let updateButton = isExternal ? `<button class="btn_update" data-name= ${name.replace('third-party', '')} + ">Update</button>` : '';
let extensionHtml = `<hr><h4><span class="${titleClass}">${DOMPurify.sanitize(displayName)}${displayVersion}</span> <span style="float:right;">${toggleElement}<span>${updateButton}</h4>`;
let updateButton = isExternal && !isUpToDate ? `<button class="btn_update" data-name="${name.replace('third-party', '')}">Update</button>` : '';
let extensionHtml = `<hr><h4><span class="update-button">${updateButton}</span> <span class="${titleClass}">${DOMPurify.sanitize(displayName)}${displayVersion}</span> <span style="float:right;">${toggleElement}</span></h4>`;
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 = '<h3>Modules provided by your Extensions API:</h3>';
html += modules.length ? `<p>${DOMPurify.sanitize(modules.join(', '))}</p>` : '<p class="failure">Not connected to the API!</p>';
html += htmlDefault + htmlExternal;

View File

@@ -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);