From c0501eb962135d0c21c856543d4274b441679cac Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Thu, 28 Sep 2023 19:22:15 +0600 Subject: [PATCH] Support for custom versions of the IITC core has been added to the methods for creating and restoring backups --- src/backup.js | 29 +++++++++++++++++++----- test/manager.9.backup.spec.js | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/backup.js b/src/backup.js index 62a4c58..5db5b05 100644 --- a/src/backup.js +++ b/src/backup.js @@ -1,6 +1,6 @@ // @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3 -import { parseMeta } from './helpers.js'; +import { isSet, parseMeta } from './helpers.js'; import deepmerge from '@bundled-es-modules/deepmerge'; /** @@ -78,9 +78,9 @@ export const exportPluginsSettings = (all_storage) => { }; /** - * Exports external plugins from the provided storage object. + * Exports external IITC core and plugins from the provided storage object. * - * This function takes a storage object and extracts external plugins based on predefined keys. + * This function takes a storage object and extracts external IITC core and plugins based on predefined keys. * It creates a new object containing the external plugins organized by their channels and filenames, * and returns it. * @@ -91,7 +91,14 @@ export const exportExternalPlugins = (all_storage) => { const external_plugins = {}; // An array of predefined keys for external plugins - const storage_keys = ['release_plugins_user', 'beta_plugins_user', 'custom_plugins_user']; + const storage_keys = [ + 'release_iitc_core_user', + 'beta_iitc_core_user', + 'custom_iitc_core_user', + 'release_plugins_user', + 'beta_plugins_user', + 'custom_plugins_user', + ]; // Loop through all_storage and check if the keys are present in storage_keys // If present, process and add the external plugins to the external_plugins object @@ -99,7 +106,19 @@ export const exportExternalPlugins = (all_storage) => { if (storage_keys.includes(key)) { // Extract the channel name from the key by splitting at '_' const channel = key.split('_')[0]; - external_plugins[channel] = {}; + const variant = key.split('_')[1]; + + // Create a channel if it doesn't exist + if (!(channel in external_plugins)) { + external_plugins[channel] = {}; + } + + // Add a custom IITC core to the external_plugins object + if (variant === 'iitc' && isSet(all_storage[key]) && isSet(all_storage[key]['code'])) { + const plugin_filename = 'total-conversion-build.user.js'; + external_plugins[channel][plugin_filename] = all_storage[key]['code']; + continue; + } // Loop through each plugin UID in the current key's storage data for (const plugin_uid in all_storage[key]) { diff --git a/test/manager.9.backup.spec.js b/test/manager.9.backup.spec.js index d1c85f3..915c38b 100644 --- a/test/manager.9.backup.spec.js +++ b/test/manager.9.backup.spec.js @@ -9,11 +9,18 @@ describe('getBackupData and setBackupData', function () { let manager = null; const first_plugin_uid = 'Available AP statistics+https://github.com/IITC-CE/ingress-intel-total-conversion'; const external_code = '// ==UserScript==\n// @name IITC plugin\n// ==/UserScript==\nreturn false;'; + const external_iitc_code = + '// ==UserScript==\n' + + '// @name IITC: Ingress intel map total conversion\n' + + '// @namespace https://github.com/IITC-CE/ingress-intel-total-conversion\n' + + '// ==/UserScript==\n' + + 'return false;'; const initialBackupData = { external_plugins: { beta: {}, custom: {}, release: { + 'total-conversion-build.user.js': external_iitc_code, 'bookmarks1.user.js': external_code, }, }, @@ -39,6 +46,7 @@ describe('getBackupData and setBackupData', function () { 'bookmarks2.user.js': external_code, }, beta: { + 'total-conversion-build.user.js': external_iitc_code, 'bookmarks3.user.js': external_code, }, }, @@ -90,6 +98,31 @@ describe('getBackupData and setBackupData', function () { const run = await manager.managePlugin(first_plugin_uid, 'on'); expect(run).to.be.undefined; }); + it('Add custom IITC core', async function () { + const scripts = [ + { + meta: { + id: 'total-conversion-build', + namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion', + name: 'IITC: Ingress intel map total conversion', + filename: 'total-conversion-build.user.js', + }, + code: external_iitc_code, + }, + ]; + const installed = { + 'IITC: Ingress intel map total conversion+https://github.com/IITC-CE/ingress-intel-total-conversion': { + uid: 'IITC: Ingress intel map total conversion+https://github.com/IITC-CE/ingress-intel-total-conversion', + id: 'total-conversion-build', + namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion', + name: 'IITC: Ingress intel map total conversion', + code: external_iitc_code, + filename: 'total-conversion-build.user.js', + }, + }; + const run = await manager.addUserScripts(scripts); + expect(run).to.deep.equal(installed); + }); it('Add external plugin', async function () { const scripts = [ { @@ -155,6 +188,15 @@ describe('getBackupData and setBackupData', function () { VMin9999: 'backup2', }); + const externalCore = await storage.get(['beta_iitc_core_user']); + expect(externalCore['beta_iitc_core_user']).to.deep.equal({ + uid: 'IITC: Ingress intel map total conversion+https://github.com/IITC-CE/ingress-intel-total-conversion', + name: 'IITC: Ingress intel map total conversion', + namespace: 'https://github.com/IITC-CE/ingress-intel-total-conversion', + code: external_iitc_code, + filename: 'total-conversion-build.user.js', + }); + const externalPlugins = await storage.get(['release_plugins_user', 'beta_plugins_user']); expect(externalPlugins['release_plugins_user']).to.have.all.keys( 'Bookmarks for maps and portals+https://github.com/IITC-CE/ingress-intel-total-conversion',