🔧 Refactor tokens-lib initialization

This commit is contained in:
Andrés Moya 2026-06-04 12:49:50 +02:00
parent 8f116c63d0
commit ca32b9df28
4 changed files with 25 additions and 28 deletions

View File

@ -993,9 +993,9 @@
(defmethod process-change :set-token (defmethod process-change :set-token
[data {:keys [set-id token-id attrs]}] [data {:keys [set-id token-id attrs]}]
(update data :tokens-lib (let [data (ctf/ensure-tokens-lib data)]
(fn [lib] (update data :tokens-lib
(let [lib' (ctob/ensure-tokens-lib lib)] (fn [lib']
(cond (cond
(not attrs) (not attrs)
(ctob/delete-token lib' set-id token-id) (ctob/delete-token lib' set-id token-id)
@ -1010,9 +1010,9 @@
(defmethod process-change :set-token-set (defmethod process-change :set-token-set
[data {:keys [id attrs]}] [data {:keys [id attrs]}]
(update data :tokens-lib (let [data (ctf/ensure-tokens-lib data)]
(fn [lib] (update data :tokens-lib
(let [lib' (ctob/ensure-tokens-lib lib)] (fn [lib']
(cond (cond
(not attrs) (not attrs)
(ctob/delete-set lib' id) (ctob/delete-set lib' id)
@ -1025,9 +1025,9 @@
(defmethod process-change :set-token-theme (defmethod process-change :set-token-theme
[data {:keys [id attrs]}] [data {:keys [id attrs]}]
(update data :tokens-lib (let [data (ctf/ensure-tokens-lib data)]
(fn [lib] (update data :tokens-lib
(let [lib' (ctob/ensure-tokens-lib lib)] (fn [lib']
(cond (cond
(not attrs) (not attrs)
(ctob/delete-theme lib' id) (ctob/delete-theme lib' id)
@ -1043,27 +1043,23 @@
(defmethod process-change :set-active-token-themes (defmethod process-change :set-active-token-themes
[data {:keys [theme-paths]}] [data {:keys [theme-paths]}]
(update data :tokens-lib #(-> % (ctob/ensure-tokens-lib) (-> (ctf/ensure-tokens-lib data)
(ctob/set-active-themes theme-paths)))) (update :tokens-lib ctob/set-active-themes theme-paths)))
(defmethod process-change :rename-token-set-group (defmethod process-change :rename-token-set-group
[data {:keys [set-group-path set-group-fname]}] [data {:keys [set-group-path set-group-fname]}]
(update data :tokens-lib (fn [lib] (-> (ctf/ensure-tokens-lib data)
(-> lib (update :tokens-lib ctob/rename-set-group set-group-path set-group-fname)))
(ctob/ensure-tokens-lib)
(ctob/rename-set-group set-group-path set-group-fname)))))
(defmethod process-change :move-token-set (defmethod process-change :move-token-set
[data {:keys [from-path to-path before-path before-group] :as changes}] [data {:keys [from-path to-path before-path before-group] :as changes}]
(update data :tokens-lib #(-> % (-> (ctf/ensure-tokens-lib data)
(ctob/ensure-tokens-lib) (update :tokens-lib ctob/move-set from-path to-path before-path before-group)))
(ctob/move-set from-path to-path before-path before-group))))
(defmethod process-change :move-token-set-group (defmethod process-change :move-token-set-group
[data {:keys [from-path to-path before-path before-group]}] [data {:keys [from-path to-path before-path before-group]}]
(update data :tokens-lib #(-> % (-> (ctf/ensure-tokens-lib data)
(ctob/ensure-tokens-lib) (update :tokens-lib ctob/move-set-group from-path to-path before-path before-group)))
(ctob/move-set-group from-path to-path before-path before-group))))
;; === Design Tokens configuration ;; === Design Tokens configuration

View File

@ -23,7 +23,7 @@
(defn add-tokens-lib (defn add-tokens-lib
[file] [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 (defn update-tokens-lib
[file f] [file f]

View File

@ -28,7 +28,7 @@
[app.common.types.shape :as cts] [app.common.types.shape :as cts]
[app.common.types.shape-tree :as ctst] [app.common.types.shape-tree :as ctst]
[app.common.types.text :as txt] [app.common.types.text :as txt]
[app.common.types.tokens-lib :refer [schema:tokens-lib]] [app.common.types.tokens-lib :as ctob]
[app.common.types.typographies-list :as ctyl] [app.common.types.typographies-list :as ctyl]
[app.common.types.typography :as cty] [app.common.types.typography :as cty]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
@ -86,7 +86,7 @@
[:components {:optional true} schema:components] [:components {:optional true} schema:components]
[:typographies {:optional true} schema:typographies] [:typographies {:optional true} schema:typographies]
[:plugin-data {:optional true} schema:plugin-data] [:plugin-data {:optional true} schema:plugin-data]
[:tokens-lib {:optional true} schema:tokens-lib]]) [:tokens-lib {:optional true} ctob/schema:tokens-lib]])
(def schema:file (def schema:file
"A schema for validate a file data structure; data is optional "A schema for validate a file data structure; data is optional
@ -196,6 +196,11 @@
(check-file file))) (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 ;; Helpers
(defn file-data (defn file-data

View File

@ -1452,10 +1452,6 @@ Will return a value that matches this schema:
(check-tokens-lib-map) (check-tokens-lib-map)
(map->tokens-lib))) (map->tokens-lib)))
(defn ensure-tokens-lib
[tokens-lib]
(or tokens-lib (make-tokens-lib)))
(def schema:tokens-lib (def schema:tokens-lib
(sm/type-schema (sm/type-schema
{:type ::tokens-lib {:type ::tokens-lib