Skip to content

Commit c3ec3ee

Browse files
committed
docs: 변경된 부분에 대한 내용을 추가한다
1 parent 11bedd3 commit c3ec3ee

File tree

1 file changed

+156
-21
lines changed

1 file changed

+156
-21
lines changed

README.md

Lines changed: 156 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,57 +40,158 @@ pnpm add generate-design-token
4040

4141
### Generate
4242

43-
Generates a file according to a user-defined template format using the given token
43+
You can generate a file or string based on an EJS template.
4444

45-
**generate(TOKEN_GROUP, config)**
45+
#### generateContents(TOKEN_GROUP, config)
46+
47+
```typescript
48+
const contents = generate(
49+
{
50+
color: {
51+
primary: {
52+
$type: "color",
53+
$value: "#ff0000",
54+
},
55+
},
56+
},
57+
{
58+
extname: "css",
59+
template: {
60+
contents: `
61+
:root {
62+
<% tokens.forEach(function (token) { %>
63+
<% const data = transformCSSVariable(token); %>
64+
<%= data.key %>: <%= data.value %>;
65+
<%})%>
66+
}
67+
`,
68+
},
69+
ejsHelper: {
70+
transformCSSVariable: (tokenData: EJSTemplateTokenData) => {
71+
return {
72+
key: `--${tokenData.props.join("-")}`,
73+
value: tokenData.value.$value,
74+
};
75+
},
76+
},
77+
},
78+
);
79+
```
80+
81+
**Configs**
82+
83+
```typescript
84+
{
85+
/**
86+
* File extension
87+
*/
88+
extname: string;
89+
/**
90+
* EJS template and template path, one or the
91+
* other must be defined unconditionally. If you
92+
* define both, content takes precedence over
93+
*/
94+
template: {
95+
content?: string;
96+
path?: string
97+
};
98+
/**
99+
* Custom EJS template data
100+
*/
101+
ejsData?: Record<string, any>
102+
/**
103+
* EJS library options
104+
*/
105+
ejsOptions?: EjsOptions;
106+
/**
107+
* Register helper functions to be used in
108+
* EJS templates
109+
*/
110+
ejsHelper?: {
111+
[key in string]: (tokenData: {
112+
props: string[];
113+
value: TOKEN_OBJECT;
114+
meta: {
115+
[key in `$${string}`]: any
116+
};
117+
}) => any
118+
};
119+
/**
120+
* prettier library options
121+
*/
122+
prettierConfig?: PrettierConfig;
123+
}
124+
```
125+
126+
#### generateFile(TOKEN_GROUP, config)
46127

47128
```typescript
48129
generate(
49130
{
50131
color: {
51132
primary: {
52133
$type: "color",
53-
$value: "#ff0000"
134+
$value: "#ff0000",
54135
},
55136
},
56137
},
57138
{
58-
filename: FILE_NAME,
59-
path: CREATE_PATH,
60-
template: `
61-
:root {
62-
<% tokens.forEach(function (token) { %>
63-
<% const data = transformCSSVariable(token); %>
64-
<%= data.key %>: <%= data.value %>;
65-
<%})%>
66-
}
67-
`,
139+
filename: "variables.css",
140+
outputPath: OUTPUT_PATH
141+
template: {
142+
contents: `
143+
:root {
144+
<% tokens.forEach(function (token) { %>
145+
<% const data = transformCSSVariable(token); %>
146+
<%= data.key %>: <%= data.value %>;
147+
<%})%>
148+
}
149+
`
150+
},
151+
ejsHelper: {
152+
transformCSSVariable: (tokenData: EJSTemplateTokenData) => {
153+
return {
154+
key: `--${tokenData.props.join("-")}`,
155+
value: tokenData.value.$value,
156+
};
157+
}
158+
}
68159
},
69160
);
70161
```
71162

72-
**Generate configs**
163+
**Configs**
73164

74165
```typescript
75166
{
76167
/**
77-
* File name to create
168+
* filename
78169
*/
79170
filename: string;
80171
/**
81-
* Path to the file to create
172+
* output path
82173
*/
83-
path: string;
174+
outputPath: string;
84175
/**
85-
* EJS template string and path
176+
* EJS template and template path, one or the
177+
* other must be defined unconditionally. If you
178+
* define both, content takes precedence over
86179
*/
87-
template: string;
180+
template: {
181+
content?: string;
182+
path?: string
183+
};
184+
/**
185+
* Custom EJS template data
186+
*/
187+
ejsData?: Record<string, any>
88188
/**
89189
* EJS library options
90190
*/
91-
ejsOptions?: EjsOptions;
191+
ejsOptions?: EjsOptions;
92192
/**
93-
* Register helper functions to be used in EJS templates
193+
* Register helper functions to be used in
194+
* EJS templates
94195
*/
95196
ejsHelper?: {
96197
[key in string]: (tokenData: {
@@ -108,6 +209,40 @@ generate(
108209
}
109210
```
110211

212+
### Data and default helper functions available in EJS templates
213+
214+
#### Data
215+
216+
**tokens**
217+
An array consisting of information from token objects and groups.
218+
219+
```typescript
220+
{
221+
props: string[];
222+
value: TokenObj | TokenGroup;
223+
meta: Record<`$${string}`, any>;
224+
}
225+
```
226+
227+
- **props**: Array of property names where token objects and groups are located
228+
- **value**: Token objects and groups
229+
- **meta**: Meta-information except $type, $value contained in token objects and groups
230+
231+
#### Default helper functions
232+
233+
Default helper functions available in EJS templates
234+
235+
**isTokenObj**
236+
The value of the token data determines whether it is a token object or not.
237+
238+
example:
239+
240+
```ejs
241+
<% tokens.forEach(function (token) { %>
242+
<% if (!isTokenObj(token.value)) return %>
243+
<%})%>
244+
```
245+
111246
## Transform
112247

113248
Structural transformations are used to effectively use duplicate structures. When writing defined style attributes in JSON, duplication of values could be resolved with reference values, but not structurally. For example, here's an example of a case like this

0 commit comments

Comments
 (0)