From 6aa797f51b50ea4c84ed7c9fb96f952515880690 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 23 Oct 2025 11:16:36 +0200 Subject: [PATCH] :sparkles: Normalize token theme serialization to JSON --- common/src/app/common/types/tokens_lib.cljc | 55 ++++++++++--------- .../common_tests/types/tokens_lib_test.cljc | 9 +-- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index d6f273defe..3ad11f90a7 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -1690,10 +1690,10 @@ Will return a value that matches this schema: (uuid/next)) :name (get theme "name") :group (get theme "group") - :is-source (get theme "is-source") + :is-source (or (get theme "isSource") + ;; NOTE: backward compatibility + (get theme "is-source")) :external-id (get theme "id") - :modified-at (some-> (get theme "modified-at") - (ct/inst)) :sets (into #{} (comp (map key) xf-normalize-set-name @@ -1822,15 +1822,15 @@ Will return a value that matches this schema: (filter #(and (instance? TokenTheme %) (not (hidden-theme? %)))) (map (fn [token-theme] - (let [theme-map (->> token-theme - (into {}) - walk/stringify-keys)] - (-> theme-map - (set/rename-keys {"sets" "selectedTokenSets" - "external-id" "id"}) - (update "selectedTokenSets" (fn [sets] - (->> (for [s sets] [s "enabled"]) - (into {}))))))))) + ;; NOTE: this probaly can be implemented as type method + (d/without-nils + {"id" (:external-id token-theme) + "name" (:name token-theme) + "group" (:group token-theme) + "description" (:description token-theme) + "isSource" (:is-source token-theme) + "selectedTokenSets" (reduce #(assoc %1 %2 "enabled") {} (:sets token-theme))})))) + themes (->> (get-theme-tree tokens-lib) (tree-seq d/ordered-map? vals) @@ -1840,29 +1840,34 @@ Will return a value that matches this schema: active-themes (-> (get-active-theme-paths tokens-lib) (disj hidden-theme-path))] - {:themes themes - :active-themes active-themes})) + [themes active-themes])) (defn export-dtcg-multi-file "Convert a TokensLib into a plain clojure map, suitable to be encoded as a multi json files each encoded in DTCG format." [tokens-lib] - (let [{:keys [themes active-themes]} (dtcg-export-themes tokens-lib) - sets (->> (get-sets tokens-lib) - (map (fn [token-set] - (let [name (get-name token-set) - tokens (get-tokens- token-set)] - [(str name ".json") (tokens-tree tokens :update-token-fn token->dtcg-token)]))) - (into {}))] + (let [[themes active-themes] + (dtcg-export-themes tokens-lib) + + sets + (->> (get-sets tokens-lib) + (map (fn [token-set] + (let [name (get-name token-set) + tokens (get-tokens- token-set)] + [(str name ".json") (tokens-tree tokens :update-token-fn token->dtcg-token)]))) + (into {}))] + (-> sets (assoc "$themes.json" themes) - (assoc "$metadata.json" {"tokenSetOrder" (get-set-names tokens-lib) - "activeThemes" active-themes - "activeSets" (get-active-themes-set-names tokens-lib)})))) + (assoc "$metadata.json" + {"tokenSetOrder" (get-set-names tokens-lib) + "activeThemes" active-themes + "activeSets" (get-active-themes-set-names tokens-lib)})))) (defn export-dtcg-json "Convert a TokensLib into a plain clojure map, suitable to be encoded as a multi sets json string in DTCG format." [tokens-lib] - (let [{:keys [themes active-themes]} (dtcg-export-themes tokens-lib) + (let [[themes active-themes] + (dtcg-export-themes tokens-lib) name-set-tuples (->> (get-set-tree tokens-lib) diff --git a/common/test/common_tests/types/tokens_lib_test.cljc b/common/test/common_tests/types/tokens_lib_test.cljc index 7ff924e59d..d34888cf91 100644 --- a/common/test/common_tests/types/tokens_lib_test.cljc +++ b/common/test/common_tests/types/tokens_lib_test.cljc @@ -1440,8 +1440,7 @@ result (ctob/export-dtcg-json tokens-lib) expected {"$themes" [{"description" "" "group" "group-1" - "is-source" false - "modified-at" now + "isSource" false "id" "test-id-00" "name" "theme-1" "selectedTokenSets" {"core" "enabled"}}] @@ -1562,8 +1561,7 @@ result (ctob/export-dtcg-json tokens-lib) expected {"$themes" [{"description" "" "group" "group-1" - "is-source" false - "modified-at" now + "isSource" false "id" "test-id-01" "name" "theme-1" "selectedTokenSets" {"core" "enabled"}}] @@ -1616,8 +1614,7 @@ result (ctob/export-dtcg-multi-file tokens-lib) expected {"$themes.json" [{"description" "" "group" "group-1" - "is-source" false - "modified-at" now + "isSource" false "id" "test-id-01" "name" "theme-1" "selectedTokenSets" {"some/set" "enabled"}}]