Skip to content

Commit

Permalink
Convert TS to JSON (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anboias authored Mar 26, 2024
1 parent 85d7c5a commit 4bcc34d
Show file tree
Hide file tree
Showing 14 changed files with 1,684 additions and 2,254 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
dist
converted
28 changes: 20 additions & 8 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
source: ["tokens/converted-tokens.json"],
source: ["tokens/converted-tokens-w3c.json"],
platforms: {
css: {
transformGroup: "css",
Expand All @@ -14,24 +14,36 @@ module.exports = {
js: {
transformGroup: "js",
buildPath: "converted/js/",
transforms: ["name/cti/camel", "size/px"],
files: [
{
destination: "tokens.ts",
format: "javascript/es6",
destination: "tokens.json",
format: "json",
},
],
},
},
parsers: [
{
pattern: "tokens/converted-tokens.json",
parse: ({ contents, filePath }) => {
pattern: "tokens/converted-tokens-w3c.json",
parse: ({ contents }) => {
const dsContent = JSON.parse(contents).ds;

// Whenever we reach a $value and $type on the same level, take only the $value and assigned it to the key.
// This is to make the structure more flat
const flatten = (obj) => {
const result = {};
Object.keys(obj).forEach((key) => {
if (obj[key].$value && obj[key].$type) {
result[key] = obj[key].$value;
} else {
result[key] = flatten(obj[key]);
}
});
return result;
};

return {
ds: dsContent,
filePath,
ds: flatten(dsContent),
};
},
},
Expand Down
Loading

0 comments on commit 4bcc34d

Please sign in to comment.