From c5fef125da75050bb52e9fbb97192c45c7aae1a7 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Fri, 26 Apr 2024 00:49:40 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20de-duplicate=20before=20adding?= =?UTF-8?q?=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/config/util.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/core/config/util.ts b/core/config/util.ts index 154b819f8..ad2eb1a0b 100644 --- a/core/config/util.ts +++ b/core/config/util.ts @@ -2,17 +2,30 @@ import { readFileSync, writeFileSync } from "fs"; import { ModelDescription } from ".."; import { editConfigJson, getConfigJsonPath } from "../util/paths"; -export function addModel(model: ModelDescription) { - const config = readFileSync(getConfigJsonPath(), "utf8"); - const configJson = JSON.parse(config); - configJson.models.push(model); - const newConfigString = JSON.stringify( - configJson, +function stringify(obj: any, indentation?: number): string { + return JSON.stringify( + obj, (key, value) => { return value === null ? undefined : value; }, - 2, + indentation, ); +} + +export function addModel(model: ModelDescription) { + const config = readFileSync(getConfigJsonPath(), "utf8"); + const configJson = JSON.parse(config); + + // De-duplicate + if (configJson.models?.some((m: any) => stringify(m) === stringify(model))) { + return config; + } + if (configJson.models?.some((m: any) => m?.title === model.title)) { + model.title = `${model.title} (1)`; + } + + configJson.models.push(model); + const newConfigString = stringify(configJson, 2); writeFileSync(getConfigJsonPath(), newConfigString); return newConfigString; }