-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
182 lines (144 loc) · 4.12 KB
/
build.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import fs from 'fs';
import path from 'path';
import StyleDictionary from 'style-dictionary';
import camelcase from 'camelcase';
const dirname = path.dirname('');
const copyFontFace = () => {
const sourceFile = path.join(dirname, 'assets/fonts/', 'fonts.css');
const outputFile = path.join(dirname, 'dist', 'fonts.css');
fs.copyFile(sourceFile, outputFile, function (err) {
if (err) {
throw err;
} else {
console.log('🎉 Success! fonts copied\n');
}
});
};
const handleGroups = (datas) => {
if (!datas) {
return;
}
const groups = datas.map((token) => token.group);
const filtredGroups = groups.filter((group) => group !== undefined);
const uniqueGroups = [...new Set(filtredGroups)];
return uniqueGroups;
};
console.log('\nBuild started...');
// REGISTER THE CUSTOM FORMAT
StyleDictionary.registerFormat({
name: 'json',
formatter: function ({ dictionary }) {
const groups = handleGroups(dictionary.allTokens);
const formatTokens = (tokens, group) => {
const formatedTokens = tokens.map((token) => {
const result = {};
result[token.name] = token.value;
return result;
});
return { [group]: formatedTokens };
};
const datas = groups.map((group) => {
const data = dictionary.allTokens.filter((token) => {
return token.group === group;
});
return formatTokens(data, group);
});
return JSON.stringify(datas, null, 2);
},
});
StyleDictionary.registerFormat({
name: 'custom/json',
formatter: function ({ dictionary }) {
const result = {};
dictionary.allTokens.map((token) => {
const tokenName = camelcase(token.name);
result[tokenName] = token.value;
});
return JSON.stringify(result, null, 2);
},
});
StyleDictionary.registerFormat({
name: 'custom/doc',
formatter: function ({ dictionary }) {
const groups = handleGroups(dictionary.allTokens);
const datas = groups.map((group) => {
const formatedGroup = group.charAt(0).toUpperCase() + group.slice(1);
let header = `\n/**\n * @tokens ${formatedGroup}\n * @presenter ${formatedGroup} \n */\n`;
const data = dictionary.allTokens.filter((token) => {
return token.group === group;
});
if (group === 'mediaQuery' || group === 'shadow' || group === 'zIndex') {
header = `\n/**\n * @tokens ${formatedGroup} \n */\n`;
}
return {
header,
data,
};
});
const storybookDocs = datas
.map(function (tokens) {
return (
tokens.header +
tokens.data
.map((props) => `$${props.name}: ${props.value};`)
.join('\n')
);
})
.join('\n');
return storybookDocs;
},
});
// REGISTER THE CUSTOM TRANFORMS
StyleDictionary.registerTransform({
name: 'pxToRem',
type: 'value',
matcher: function (token) {
return (
token.attributes.type === 'size' ||
token.attributes.category === 'space' ||
token.attributes.category === 'border-radius'
);
},
transformer: function (token, options) {
if (parseInt(token.original.value) === 0) {
return token.original.value;
}
let base;
if (options.options?.base10) {
base = 10;
} else {
base = 16;
}
return `${parseInt(token.value) / base}rem`;
},
});
StyleDictionary.registerTransform({
name: 'mqPx',
type: 'value',
matcher: function (token) {
return token.attributes.category === 'breakpoints';
},
transformer: function (token) {
return `${token.value}px`;
},
});
// REGISTER THE CUSTOM TRANFORM GROUPS
StyleDictionary.registerTransformGroup({
name: 'custom/scss',
transforms: StyleDictionary.transformGroup['scss'].concat([
'pxToRem',
'mqPx',
]),
});
StyleDictionary.registerTransformGroup({
name: 'custom/css',
transforms: StyleDictionary.transformGroup['css'].concat(['pxToRem', 'mqPx']),
});
// APPLY THE CONFIGURATION
const StyleDictionaryExtended = StyleDictionary.extend(
`${dirname}/config.json`
);
// FINALLY, BUILD ALL THE PLATFORMS
StyleDictionaryExtended.buildAllPlatforms();
console.log('\n🚀 Build completed!\n');
copyFontFace();