@@ -85,16 +85,11 @@ async function createSassVariablesFile(ast) {
85
85
fs . writeFileSync ( `src/styles/_variables.scss` , fileContent ) ;
86
86
}
87
87
88
- /**
89
- * Parse the mixins file and generate mixins mdx.
90
- */
91
- async function mixins ( ) {
92
- const url = 'src/styles/_mixins.scss' ;
93
- const docsUrl = 'src/__stories__/2_style/3_mixins.stories.mdx' ;
94
- const mixinsUrl = 'src/__stories__/2_style/mixins.ts' ;
88
+ /* --- Mixins. --- */
95
89
96
- const mixinsAst = getAST ( url ) ;
97
- const docsFile = fs . readFileSync ( docsUrl ) ;
90
+ async function generateMixinsFile ( input , output , transform , replace = v => v ) {
91
+ const mixinsAst = getAST ( input ) ;
92
+ const docsFile = fs . readFileSync ( transform ) ;
98
93
99
94
const mixinLines = docsFile . toString ( ) . split ( '\n' ) ;
100
95
const indexOfEntry = mixinLines . findIndex ( l => l . includes ( 'GENERATE_ENTRY' ) ) ;
@@ -126,21 +121,27 @@ async function mixins() {
126
121
const media = isMedia ? ` {\n // Content\n}` : '' ;
127
122
return {
128
123
include : `@include ${ m . params } ${ media } ;` ,
129
- code : css
130
- . slice ( m . source . start . line - 2 , m . source . end . line )
131
- . join ( '\n' )
132
- . trim ( ) ,
124
+ code : replace (
125
+ css
126
+ . slice ( m . source . start . line - 2 , m . source . end . line - 1 )
127
+ . filter ( line => ! line . includes ( '@mixin' ) )
128
+ . map ( line =>
129
+ line . replace ( ' ' , '' ) . replace ( '@content;' , '/* Content */' ) ,
130
+ )
131
+ . join ( '\n' )
132
+ . trim ( ) ,
133
+ ) ,
133
134
animation : animation && `\n\n${ animation } ` ,
134
135
} ;
135
136
} ) ;
136
137
137
138
printFile (
138
- mixinsUrl ,
139
+ output ,
139
140
data . map ( d => d . include ) ,
140
141
) ;
141
142
142
143
fs . writeFileSync (
143
- docsUrl ,
144
+ transform ,
144
145
`${ pre }
145
146
${ data
146
147
. map (
@@ -149,20 +150,89 @@ ${data
149
150
150
151
\`\`\`scss
151
152
${ d . code } ${ d . animation }
152
- \`\`\`
153
- ` ,
153
+ \`\`\`` ,
154
154
)
155
- . join ( '\n' ) } `,
155
+ . join ( '\n' ) }
156
+ ` ,
156
157
) ;
157
158
}
158
159
160
+ /**
161
+ * Parse the mixins file and generate mixins mdx.
162
+ */
163
+ async function mixins ( ) {
164
+ const url = 'src/styles/_mixins.scss' ;
165
+ const docsUrl = 'src/__stories__/2_style/3_mixins.stories.mdx' ;
166
+ const mixinsUrl = 'src/__stories__/2_style/generated/mixins.ts' ;
167
+
168
+ generateMixinsFile ( url , mixinsUrl , docsUrl ) ;
169
+ }
170
+
171
+ /**
172
+ * Parse breakpoints.
173
+ */
174
+ async function breakpoints ( ) {
175
+ const url = 'src/styles/_breakpoints.scss' ;
176
+ const breakpointsUrl = 'src/__stories__/2_style/generated/breakpoints.ts' ;
177
+ const docsUrl = 'src/__stories__/2_style/4_breakpoints.stories.mdx' ;
178
+
179
+ const breakpointsAST = getAST ( url ) ;
180
+
181
+ const breakpointsNodes = breakpointsAST . nodes ;
182
+
183
+ const declarations = ( ( ) => {
184
+ const decl = [ ] ;
185
+
186
+ breakpointsNodes . forEach ( n => {
187
+ if ( n . type !== 'decl' ) return ;
188
+ decl . push ( n ) ;
189
+ } ) ;
190
+
191
+ return decl ;
192
+ } ) ( ) . map ( ( { prop, value } ) => ( { prop, value } ) ) ;
193
+
194
+ const mixins = breakpointsNodes . filter ( n => n . name === 'mixin' ) ;
195
+ const css = mixins [ 0 ] . source . input . css . split ( '\n' ) ;
196
+
197
+ const data = mixins . map ( m => {
198
+ const code = css
199
+ . slice ( m . source . start . line - 2 , m . source . end . line )
200
+ . join ( '\n' )
201
+ . trim ( ) ;
202
+ const isOnly = m . params . includes ( 'only' ) ;
203
+ const declaration = declarations . find ( decl => code . includes ( decl . prop ) ) ;
204
+ const numValue = Number ( declaration . value . replace ( 'px' , '' ) ) ;
205
+ return {
206
+ params : m . params ,
207
+ min : isOnly ? undefined : numValue ,
208
+ max : isOnly ? numValue - 1 : undefined ,
209
+ ...declaration ,
210
+ } ;
211
+ } ) ;
212
+
213
+ printFile ( 'src/__stories__/2_style/generated/breakpointRange.ts' , data ) ;
214
+
215
+ generateMixinsFile ( url , breakpointsUrl , docsUrl , val => {
216
+ let newVal = val ;
217
+ data . forEach ( data => {
218
+ const isMax = newVal . includes ( 'max-width' ) ;
219
+ if ( isMax && data . max ) {
220
+ newVal = newVal . replace ( data . prop , `${ data . max } px` ) ;
221
+ } else if ( ! isMax && data . min ) {
222
+ newVal = newVal . replace ( data . prop , `${ data . min } px` ) ;
223
+ }
224
+ newVal = newVal . replace ( ' - 1' , '' ) ;
225
+ } ) ;
226
+ return newVal ;
227
+ } ) ;
228
+ }
229
+
159
230
/**
160
231
* Parse variables and themes files, generate variables and colors mdx.
161
232
*/
162
233
function main ( ) {
163
- /* --- Generate mixins file. --- */
164
-
165
234
mixins ( ) ;
235
+ breakpoints ( ) ;
166
236
167
237
const ast = getAST ( 'src/styles/_theme.scss' ) ;
168
238
const nodes = ast . nodes . find ( n => n . selector === ':root' ) . nodes ;
@@ -175,32 +245,35 @@ function main() {
175
245
const color = [ ] ;
176
246
const zIndex = [ ] ;
177
247
const padding = [ ] ;
178
- const breakpoint = [ ] ;
179
248
const fontSize = [ ] ;
180
249
const fontWeight = [ ] ;
181
250
const borderRadius = [ ] ;
251
+ const boxShadow = [ ] ;
252
+ const focus = [ ] ;
182
253
const other = [ ] ;
183
254
184
255
nodes . forEach ( n => {
185
256
if ( n . type !== 'decl' ) return ;
186
257
if ( n . prop . startsWith ( '--color' ) ) return color . push ( n ) ;
187
258
if ( n . prop . startsWith ( '--z-index' ) ) return zIndex . push ( n ) ;
188
259
if ( n . prop . startsWith ( '--padding' ) ) return padding . push ( n ) ;
189
- if ( n . prop . startsWith ( '--breakpoint' ) ) return breakpoint . push ( n ) ;
190
260
if ( n . prop . startsWith ( '--font-size' ) ) return fontSize . push ( n ) ;
191
261
if ( n . prop . startsWith ( '--font-weight' ) ) return fontWeight . push ( n ) ;
192
262
if ( n . prop . startsWith ( '--border-radius' ) ) return borderRadius . push ( n ) ;
263
+ if ( n . prop . startsWith ( '--box-shadow' ) ) return boxShadow . push ( n ) ;
264
+ if ( n . prop . startsWith ( '--focus' ) ) return focus . push ( n ) ;
193
265
other . push ( n ) ;
194
266
} ) ;
195
267
196
268
return {
197
269
color,
198
270
zIndex,
199
271
padding,
200
- breakpoint,
201
272
fontSize,
202
273
fontWeight,
203
274
borderRadius,
275
+ boxShadow,
276
+ focus,
204
277
other,
205
278
} ;
206
279
} ) ( ) ;
@@ -215,6 +288,7 @@ function main() {
215
288
const gray = [ ] ;
216
289
const blueGray = [ ] ;
217
290
const contrast = [ ] ;
291
+ const message = [ ] ;
218
292
const other = [ ] ;
219
293
220
294
allColors . forEach ( c => {
@@ -223,10 +297,11 @@ function main() {
223
297
if ( c . scss . startsWith ( '$color-gray' ) ) return gray . push ( c ) ;
224
298
if ( c . scss . startsWith ( '$color-blue-gray' ) ) return blueGray . push ( c ) ;
225
299
if ( c . scss . startsWith ( '$color-contrast' ) ) return contrast . push ( c ) ;
300
+ if ( c . scss . startsWith ( '$color-message' ) ) return message . push ( c ) ;
226
301
other . push ( c ) ;
227
302
} ) ;
228
303
229
- return { theme, font, gray, blueGray, contrast, other } ;
304
+ return { theme, font, gray, blueGray, contrast, message , other } ;
230
305
} ) ( ) ;
231
306
232
307
/* --- Map variables. --- */
@@ -235,7 +310,8 @@ function main() {
235
310
const fontWeight = declarations . fontWeight . map ( mapper ) ;
236
311
const padding = declarations . padding . map ( mapper ) ;
237
312
const borderRadius = declarations . borderRadius . map ( mapper ) ;
238
- const breakpoint = declarations . breakpoint . map ( mapper ) ;
313
+ const boxShadow = declarations . boxShadow . map ( mapper ) ;
314
+ const focus = declarations . focus . map ( mapper ) ;
239
315
const zIndex = declarations . zIndex . map ( mapper ) ;
240
316
const other = declarations . other . map ( mapper ) ;
241
317
@@ -245,13 +321,17 @@ function main() {
245
321
fontWeight,
246
322
padding,
247
323
borderRadius,
248
- breakpoint,
324
+ boxShadow,
325
+ focus,
249
326
zIndex,
250
327
other,
251
328
} ;
252
329
253
330
createSassVariablesFile ( variableObject ) ;
254
- printFile ( 'src/__stories__/2_style/styleVariables.ts' , variableObject ) ;
331
+ printFile (
332
+ 'src/__stories__/2_style/generated/styleVariables.ts' ,
333
+ variableObject ,
334
+ ) ;
255
335
}
256
336
257
337
main ( ) ;
0 commit comments