-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-tokens.js
152 lines (121 loc) · 4.23 KB
/
build-tokens.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { resolve, join } = require('path');
const { writeFile } = require('fs/promises');
const { readFileSync } = require('fs');
const { readdir, rmdir, unlink } = require('fs').promises;
const set = require('lodash/set')
const merge = require('lodash/merge')
const isObject = require('lodash/isObject');
const { isArray } = require('lodash');
const StyleDictionary = require('./build-style-dictionary-tokens');
const buildPath = './builds/tokens'
const excludeExportPaths = [];
async function getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
}));
return Array.prototype.concat(...files);
}
async function removeDirectoryContents(directory) {
try {
const items = await readdir(directory, { withFileTypes: true });
for (const item of items) {
const itemPath = join(directory, item.name);
if (item.isDirectory()) {
await removeDirectoryContents(itemPath);
await rmdir(itemPath);
} else {
await unlink(itemPath);
}
}
} catch (error) {
console.error(`Error removing contents of ${directory}:`, error);
}
}
function getAllValues(obj) {
let result = {};
function traverse(obj, currentObj, path = '', parentKey = '', parentObj = {}) {
for (const key in obj) {
const newPath = path ? `${path}.${key}` : key;
if (isObject(obj[key]) && !isArray(obj[key])) {
currentObj[key] = {};
traverse(obj[key], currentObj[key], newPath, key, currentObj);
} else if (isArray(obj[key])) {
if (parentKey === '$extensions') {
if (obj.hasOwnProperty('generators')) {
if (isArray(obj.generators)) {
const rootPath = path.split('.').filter(d => d != '$extensions').join('.')
obj.generators.forEach(({ type, value }) => {
Object.keys(value).forEach((generatorKey) => {
set(
result,
`${rootPath}${generatorKey}`,
{
$type: parentObj['$type'],
$value: `{${rootPath}}`,
$extensions: {
[type]: value[generatorKey],
}
},
)
})
});
}
}
}
currentObj[key] = [];
traverse(obj[key], currentObj[key], newPath, key, currentObj);
} else {
currentObj[key] = obj[key];
}
}
}
traverse(obj, result);
return result;
}
async function createExportFiles() {
try {
const content = readFileSync(`${buildPath}/kda-design-system.tokens.json`, 'utf8');
await removeDirectoryContents(buildPath + '/export');
if (content) {
const rawData = JSON.parse(content).kda.foundation;
Object.keys(rawData)
.filter(foundationItemKey => !excludeExportPaths.includes(foundationItemKey))
.forEach(async (foundationItemKey) => {
const foundationItemExportPath = join(__dirname, `${buildPath}/export/${foundationItemKey}.tokens.json`)
await writeFile(foundationItemExportPath, JSON.stringify({
kda: {
foundation: {
[foundationItemKey]: rawData[foundationItemKey]
},
},
}), { flag: 'w', encoding: 'utf-8' })
})
}
} catch (error) {
console.error(`Error while exporting tokens.`, error)
}
}
(async function () {
const tokens = await getFiles('./tokens');
const jsons = tokens.filter(x => x.endsWith('.tokens.json')).map(jsonFile => {
const content = readFileSync(jsonFile, 'utf8');
return {
path: jsonFile,
content,
}
});
const result = jsons.reduce((data, jsonFile) => {
try {
return merge(data, getAllValues(JSON.parse(jsonFile.content)))
} catch (error) {
return data
}
}, {});
const destPath = `${buildPath}/kda-design-system.raw.tokens.json`;
const writeFilePath = join(__dirname, destPath);
await writeFile(writeFilePath, JSON.stringify(result), { flag: 'w', encoding: 'utf-8' })
StyleDictionary(destPath)
createExportFiles()
})()