From 096eee54613b00c3f9617bb1cbe129ae9b520d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Thu, 4 Jun 2026 12:49:50 +0200 Subject: [PATCH] :wrench: Refactor tokens-lib initialization --- common/src/app/common/files/changes.cljc | 38 +++++++++---------- .../src/app/common/test_helpers/tokens.cljc | 2 +- common/src/app/common/types/file.cljc | 7 +++- common/src/app/common/types/tokens_lib.cljc | 4 -- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index 013b1c50ec..47c9d4da17 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -998,9 +998,9 @@ (defmethod process-change :set-token [data {:keys [set-id token-id attrs]}] - (update data :tokens-lib - (fn [lib] - (let [lib' (ctob/ensure-tokens-lib lib)] + (let [data (ctf/ensure-tokens-lib data)] + (update data :tokens-lib + (fn [lib'] (cond (not attrs) (ctob/delete-token lib' set-id token-id) @@ -1015,9 +1015,9 @@ (defmethod process-change :set-token-set [data {:keys [id attrs]}] - (update data :tokens-lib - (fn [lib] - (let [lib' (ctob/ensure-tokens-lib lib)] + (let [data (ctf/ensure-tokens-lib data)] + (update data :tokens-lib + (fn [lib'] (cond (not attrs) (ctob/delete-set lib' id) @@ -1030,9 +1030,9 @@ (defmethod process-change :set-token-theme [data {:keys [id attrs]}] - (update data :tokens-lib - (fn [lib] - (let [lib' (ctob/ensure-tokens-lib lib)] + (let [data (ctf/ensure-tokens-lib data)] + (update data :tokens-lib + (fn [lib'] (cond (not attrs) (ctob/delete-theme lib' id) @@ -1048,27 +1048,23 @@ (defmethod process-change :set-active-token-themes [data {:keys [theme-paths]}] - (update data :tokens-lib #(-> % (ctob/ensure-tokens-lib) - (ctob/set-active-themes theme-paths)))) + (-> (ctf/ensure-tokens-lib data) + (update :tokens-lib ctob/set-active-themes theme-paths))) (defmethod process-change :rename-token-set-group [data {:keys [set-group-path set-group-fname]}] - (update data :tokens-lib (fn [lib] - (-> lib - (ctob/ensure-tokens-lib) - (ctob/rename-set-group set-group-path set-group-fname))))) + (-> (ctf/ensure-tokens-lib data) + (update :tokens-lib ctob/rename-set-group set-group-path set-group-fname))) (defmethod process-change :move-token-set [data {:keys [from-path to-path before-path before-group] :as changes}] - (update data :tokens-lib #(-> % - (ctob/ensure-tokens-lib) - (ctob/move-set from-path to-path before-path before-group)))) + (-> (ctf/ensure-tokens-lib data) + (update :tokens-lib ctob/move-set from-path to-path before-path before-group))) (defmethod process-change :move-token-set-group [data {:keys [from-path to-path before-path before-group]}] - (update data :tokens-lib #(-> % - (ctob/ensure-tokens-lib) - (ctob/move-set-group from-path to-path before-path before-group)))) + (-> (ctf/ensure-tokens-lib data) + (update :tokens-lib ctob/move-set-group from-path to-path before-path before-group))) ;; === Design Tokens configuration diff --git a/common/src/app/common/test_helpers/tokens.cljc b/common/src/app/common/test_helpers/tokens.cljc index 81697ac4b8..662cd3fd11 100644 --- a/common/src/app/common/test_helpers/tokens.cljc +++ b/common/src/app/common/test_helpers/tokens.cljc @@ -23,7 +23,7 @@ (defn add-tokens-lib [file] - (ctf/update-file-data file #(update % :tokens-lib ctob/ensure-tokens-lib))) + (ctf/update-file-data file ctf/ensure-tokens-lib)) (defn update-tokens-lib [file f] diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index bcc4ddc44d..e31e317525 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -87,7 +87,7 @@ [:components {:optional true} schema:components] [:typographies {:optional true} schema:typographies] [:plugin-data {:optional true} schema:plugin-data] - [:tokens-source {:optional true} ::sm/uuid] ;; Forward-compat: UUID of external library containing tokens-lib (full support in follow-up PR) + [:tokens-source {:optional true} ::sm/uuid] [:tokens-lib {:optional true} ctob/schema:tokens-lib] [:tokens-status {:optional true} ctos/schema:tokens-status]]) @@ -209,6 +209,11 @@ (check-file file))) +(defn ensure-tokens-lib + "Ensure file-data has a :tokens-lib key, creating one if necessary." + [file-data] + (update file-data :tokens-lib #(or % (ctob/make-tokens-lib)))) + ;; Helpers (defn file-data diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index 1fe5b87658..860416b148 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -1452,10 +1452,6 @@ Will return a value that matches this schema: (check-tokens-lib-map) (map->tokens-lib))) -(defn ensure-tokens-lib - [tokens-lib] - (or tokens-lib (make-tokens-lib))) - (def schema:tokens-lib (sm/type-schema {:type ::tokens-lib