Skip to content

Commit

Permalink
chore: fix config save
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Nov 27, 2024
1 parent 1f61f21 commit 22b7912
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
12 changes: 5 additions & 7 deletions static/scripts/config-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ export class ConfigParser {
return YAML.parse(`${this.newConfigYml}`);
}

async updateConfig(org: string, octokit: Octokit, option: "add" | "remove", path = CONFIG_FULL_PATH, repo = CONFIG_ORG_REPO) {
this.repoConfig = this.newConfigYml;
this.saveConfig();
async updateConfig(org: string, octokit: Octokit, path = CONFIG_FULL_PATH, repo = CONFIG_ORG_REPO) {
return this.createOrUpdateFileContents(org, repo, path, octokit);
}

Expand Down Expand Up @@ -151,8 +149,7 @@ export class ConfigParser {
}

addPlugin(plugin: Plugin) {
const config = this.loadConfig();
const parsedConfig = this.parseConfig(config);
const parsedConfig = this.parseConfig(this.repoConfig);
parsedConfig.plugins ??= [];

const existingPlugin = parsedConfig.plugins.find((p) => p.uses[0].plugin === plugin.uses[0].plugin);
Expand All @@ -163,19 +160,20 @@ export class ConfigParser {
}

this.newConfigYml = YAML.stringify(parsedConfig);
this.repoConfig = this.newConfigYml;
this.saveConfig();
}

removePlugin(plugin: Plugin) {
const config = this.loadConfig();
const parsedConfig = this.parseConfig(config);
const parsedConfig = this.parseConfig(this.repoConfig);
if (!parsedConfig.plugins) {
toastNotification("No plugins found in config", { type: "error" });
return;
}

parsedConfig.plugins = parsedConfig.plugins.filter((p: Plugin) => p.uses[0].plugin !== plugin.uses[0].plugin);
this.newConfigYml = YAML.stringify(parsedConfig);
this.repoConfig = this.newConfigYml;
this.saveConfig();
}

Expand Down
7 changes: 3 additions & 4 deletions static/scripts/rendering/plugin-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { STRINGS } from "../../utils/strings";
import { ManifestRenderer } from "../render-manifest";
import { renderConfigEditor } from "./config-editor";
import { controlButtons } from "./control-buttons";
import { closeAllSelect, updateGuiTitle } from "./utils";
import { closeAllSelect, normalizePluginName, updateGuiTitle } from "./utils";

/**
* Renders a dropdown of plugins taken from the marketplace with an installed indicator.
Expand Down Expand Up @@ -62,9 +62,8 @@ export function renderPluginSelector(renderer: ManifestRenderer): void {
if (!cleanManifestCache[url]?.name) {
return;
}

const [, repo] = url.replace("https://raw.githubusercontent.com/", "").split("/");
const reg = new RegExp(`${repo}`, "gi");
const normalizedName = normalizePluginName(cleanManifestCache[url].name);
const reg = new RegExp(normalizedName, "i");
const installedPlugin: Plugin | undefined = installedPlugins.find((plugin) => plugin.uses[0].plugin.match(reg));
const defaultForInstalled: ManifestPreDecode | null = cleanManifestCache[url];
const optionText = defaultForInstalled.name;
Expand Down
5 changes: 1 addition & 4 deletions static/scripts/rendering/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,5 @@ export function removeTrackedEventListener(target: EventTarget, type: string, li
}

export function getTrackedEventListeners(target: EventTarget, type: string): EventListener[] {
if (eventListenersMap.has(target)) {
return eventListenersMap.get(target)?.get(type) || [];
}
return [];
return eventListenersMap.get(target)?.get(type) || [];
}
4 changes: 2 additions & 2 deletions static/scripts/rendering/write-add-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function handleAddPlugin(renderer: ManifestRenderer, plugin: Plugin, pluginManif
}

try {
await renderer.configParser.updateConfig(org, octokit, "add");
await renderer.configParser.updateConfig(org, octokit);
} catch (error) {
console.error("Error pushing config to GitHub:", error);
toastNotification("An error occurred while pushing the configuration to GitHub.", {
Expand Down Expand Up @@ -129,7 +129,7 @@ function handleRemovePlugin(renderer: ManifestRenderer, plugin: Plugin, pluginMa
}

try {
await renderer.configParser.updateConfig(org, octokit, "remove");
await renderer.configParser.updateConfig(org, octokit);
} catch (error) {
console.error("Error pushing config to GitHub:", error);
toastNotification("An error occurred while pushing the configuration to GitHub.", {
Expand Down

0 comments on commit 22b7912

Please sign in to comment.