-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-dictionary.js
99 lines (90 loc) · 2.2 KB
/
build-dictionary.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
const path = require('path')
const fs = require('fs')
const baseDir = 'figmagic'
const tokenDir = 'dictionary'
const StyleDictionary = require('style-dictionary').extend({
parsers: [
{
pattern: /\.json$/,
parse: ({ contents, filePath }) => {
try {
const object = JSON.parse(contents)
const output = parseTokens(object, filePath)
return output
} catch (error) {
return error
}
},
},
],
source: [`./${baseDir}/${tokenDir}/**/*.json`],
platforms: {
js: {
transforms: [
'attribute/cti',
'name/cti/constant',
'time/seconds',
'content/icon',
'color/rgb',
'size/rem',
],
buildPath: './src/styles/',
files: [
{
destination: 'dictionary.js',
format: 'javascript/es6',
},
],
},
css: {
transforms: [
'attribute/cti',
'name/cti/kebab',
'time/seconds',
'content/icon',
'color/rgb',
'size/rem',
],
buildPath: './src/styles/',
files: [
{
destination: 'dictionary.css',
format: 'css/variables',
},
],
},
},
})
function loadConfig() {
const files = ['.sdconfigrc', 'sdconfig.json']
const validFiles = files.filter((file) => fs.existsSync(file))
if (validFiles.length > 0) {
try {
const rawdata = fs.readFileSync(validFiles[0])
const configData = JSON.parse(rawdata)
return configData
} catch (error) {
console.log(error)
}
}
}
function parseTokens(object, filePath) {
const tokenFile = path.basename(filePath).replace('.json', '')
const configData = loadConfig() || {}
const token = (token) => token.input === tokenFile
const tokenName =
Object.keys(configData).indexOf('tokenNames') > -1
? configData.tokenNames.find(token)
? configData.tokenNames.find(token).output || tokenFile
: tokenFile
: tokenFile
const output = {}
output[tokenName] = { ...object }
for (const key in output[tokenName]) {
output[tokenName][key] = {
value: output[tokenName][key],
}
}
return output
}
StyleDictionary.buildAllPlatforms()