🔧 Refactor tokens-lib initialization

This commit is contained in:
Andrés Moya 2026-06-04 12:49:50 +02:00
parent 70727c56b1
commit 096eee5461
4 changed files with 24 additions and 27 deletions

View File

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

View File

@ -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]

View File

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

View File

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