Skip to content

Commit 4d220a6

Browse files
committed
remove token
1 parent 7f16bcc commit 4d220a6

4 files changed

Lines changed: 102 additions & 11 deletions

File tree

scripts/check-changed-tokens.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,40 @@ const readHead = (file) => {
2525
}
2626
}
2727

28+
const getMissingTokens = (base, head) => {
29+
const missingTokens = [];
30+
31+
const isLeaf = (obj) =>
32+
Object.keys(obj).length === 2 && !!obj.value && !!obj.type;
33+
const crawlObject = (base, head, path) => {
34+
const keys = Object.keys(base);
35+
for (let i = 0; i < keys.length; i++) {
36+
const key = keys[i];
37+
if (!isLeaf(base) && !!head[key]) {
38+
crawlObject(base[key], head[key], `${path ? path + "." : ""}${key}`);
39+
}
40+
if (!isLeaf(base) && !head[key]) {
41+
missingTokens.push(`${path ? path + "." : ""}${key}`);
42+
}
43+
}
44+
};
45+
46+
crawlObject(base, head, "");
47+
return missingTokens;
48+
};
49+
2850
console.log(`${files.length} token file(s) changed:`)
2951
for (const file of files) {
3052
const base = readBase(file)
3153
const head = readHead(file)
3254
const status = base === null ? 'added' : head === null ? 'deleted' : 'modified'
3355
console.log(` ${file} (${status}) base=${base?.length ?? 0}b head=${head?.length ?? 0}b`)
56+
57+
if (status === "modified") {
58+
const missingTokens = getMissingTokens(JSON.parse(base), JSON.parse(head));
59+
console.log("missing token(s)");
60+
missingTokens.forEach((token) => console.log(token));
61+
}
3462
}
3563

3664
// Real checks go here. Exit non-zero to fail the PR.

scripts/test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const base = {
2+
flex: {
3+
flexFontFamily: {
4+
value: "{semantic.fontFamily.base}",
5+
type: "fontFamilies",
6+
},
7+
},
8+
flex2: {
9+
flex2FontFamily: {
10+
value: "{semantic.fontFamily.base}",
11+
type: "fontFamilies",
12+
},
13+
flex2FontFamily2: {
14+
value: "{problem}",
15+
type: "fontFamilies",
16+
},
17+
flex2FontFamily3: {
18+
value: "{problem}",
19+
type: "fontFamilies",
20+
},
21+
},
22+
flex3: {
23+
flexFontFamily: {
24+
value: "{semantic.fontFamily.base}",
25+
type: "fontFamilies",
26+
},
27+
},
28+
};
29+
30+
const head = {
31+
flex: {
32+
flexFontFamily: {
33+
value: "{semantic.fontFamily.base}",
34+
type: "fontFamilies",
35+
},
36+
},
37+
flex2: {
38+
flex2FontFamily: {
39+
value: "{semantic.fontFamily.base}",
40+
type: "fontFamilies",
41+
},
42+
},
43+
};
44+
45+
const getMissingTokens = (base, head) => {
46+
const missingTokens = [];
47+
48+
const isLeaf = (obj) =>
49+
Object.keys(obj).length === 2 && !!obj.value && !!obj.type;
50+
const crawlObject = (base, head, path) => {
51+
const keys = Object.keys(base);
52+
for (let i = 0; i < keys.length; i++) {
53+
const key = keys[i];
54+
if (!isLeaf(base) && !!head[key]) {
55+
crawlObject(base[key], head[key], `${path ? path + "." : ""}${key}`);
56+
}
57+
if (!isLeaf(base) && !head[key]) {
58+
missingTokens.push(`${path ? path + "." : ""}${key}`);
59+
}
60+
}
61+
};
62+
63+
crawlObject(base, head, "");
64+
return missingTokens;
65+
};
66+
67+
console.log(getMissingTokens(base, head));

src/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ const tokensDir = join(__dirname, '..', 'tokensStudio')
1111
export const themeTokens = {}
1212

1313
for (const filePath of globSync('**/*.json', { cwd: tokensDir })) {
14-
const tokens = JSON.parse(readFileSync(join(tokensDir, filePath), 'utf8'))
15-
// e.g. 'canvas/semantic/color/canvas.json' -> ['canvas', 'semantic', 'color', 'canvas']
16-
const keys = filePath.replace(/\.json$/, '').split('/')
14+
const tokens = JSON.parse(readFileSync(join(tokensDir, filePath), "utf8"));
15+
// e.g. 'canvas/semantic/color/canvas.json' -> ['canvas', 'semantic', 'color', 'canvas'] test
16+
const keys = filePath.replace(/\.json$/, "").split("/");
1717

18-
let node = themeTokens
18+
let node = themeTokens;
1919
for (const key of keys.slice(0, -1)) {
20-
node[key] ??= {}
21-
node = node[key]
20+
node[key] ??= {};
21+
node = node[key];
2222
}
23-
node[keys.at(-1)] = tokens
23+
node[keys.at(-1)] = tokens;
2424
}

tokensStudio/canvas/component/AiInformation.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
"value": "{semantic.spacing.spaceLg}",
1313
"type": "spacing"
1414
},
15-
"permissionLevelTextBottomMargin": {
16-
"value": "{semantic.spacing.spaceSm}",
17-
"type": "spacing"
18-
},
1915
"permissionLevelBottomMargin": {
2016
"value": "{semantic.spacing.spaceSm}",
2117
"type": "spacing"

0 commit comments

Comments
 (0)