@@ -125,7 +125,10 @@ function validateConfig(configFile) {
125
125
126
126
try {
127
127
validateConfigBrowsers ( storybookConfig . browsers ) ;
128
- storybookConfig . resolutions = validateConfigResolutions ( storybookConfig . resolutions ) ;
128
+ resolutions = storybookConfig . resolutions || storybookConfig . viewports
129
+ storybookConfig . resolutions = validateConfigResolutions ( resolutions ) ;
130
+ storybookConfig . viewports = storybookConfig . resolutions ;
131
+ validateCustomViewPorts ( storybookConfig . customViewports )
129
132
} catch ( error ) {
130
133
console . log ( `[smartui] Error: Invalid config, ${ error . message } ` ) ;
131
134
process . exit ( constants . ERROR_CATCHALL ) ;
@@ -136,7 +139,7 @@ function validateConfig(configFile) {
136
139
storybookConfig . waitForTimeout = 0 ;
137
140
} else if ( storybookConfig . waitForTimeout <= 0 || storybookConfig . waitForTimeout > 30000 ) {
138
141
console . log ( '[smartui] Warning: Invalid config, value of waitForTimeout must be > 0 and <= 30000' ) ;
139
- console . log ( 'If you do not wish to include waitForTimeout parameter, remove it from the config file.' ) ;
142
+ console . log ( '[smartui] If you do not wish to include waitForTimeout parameter, remove it from the config file.' ) ;
140
143
storybookConfig . waitForTimeout = 0 ;
141
144
}
142
145
@@ -158,25 +161,31 @@ function validateConfigBrowsers(browsers) {
158
161
159
162
function validateConfigResolutions ( resolutions ) {
160
163
if ( ! Array . isArray ( resolutions ) ) {
161
- throw new ValidationError ( 'invalid resolutions .' ) ;
164
+ throw new ValidationError ( 'Invalid viewports config. Please add atleast one viewport .' ) ;
162
165
}
163
166
if ( resolutions . length == 0 ) {
164
- throw new ValidationError ( 'empty resolutions list in config.' ) ;
167
+ throw new ValidationError ( 'Empty viewports list in config.' ) ;
165
168
}
166
169
if ( resolutions . length > 5 ) {
167
170
throw new ValidationError ( `max resolutions: ${ MAX_RESOLUTIONS } ` ) ;
168
171
}
169
172
let res = [ ] ;
170
173
resolutions . forEach ( element => {
171
174
if ( ! Array . isArray ( element ) || element . length == 0 || element . length > 2 ) {
172
- throw new ValidationError ( 'invalid resolutions .' ) ;
175
+ throw new ValidationError ( 'Invalid elements in viewports config .' ) ;
173
176
}
174
177
let width = element [ 0 ] ;
175
178
let height = element [ 1 ] ;
176
- if ( typeof width != 'number' || width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH ) {
179
+ if ( typeof width != 'number' ) {
180
+ width = Number ( width ) ;
181
+ }
182
+ if ( typeof height != 'number' ) {
183
+ height = Number ( height ) ;
184
+ }
185
+ if ( width && width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH ) {
177
186
throw new ValidationError ( `width must be > ${ MIN_RESOLUTION_WIDTH } , < ${ MAX_RESOLUTION_WIDTH } ` ) ;
178
187
}
179
- if ( height && ( typeof height != 'number' || height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH ) ) {
188
+ if ( height & ( height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH ) ) {
180
189
throw new ValidationError ( `height must be > ${ MIN_RESOLUTION_HEIGHT } , < ${ MAX_RESOLUTION_HEIGHT } ` ) ;
181
190
}
182
191
res . push ( [ width , height || 0 ] ) ;
@@ -185,6 +194,42 @@ function validateConfigResolutions(resolutions) {
185
194
return res
186
195
}
187
196
197
+
198
+ function validateCustomViewPorts ( customViewports ) {
199
+ if ( ! Array . isArray ( customViewports ) ) {
200
+ return
201
+ }
202
+ if ( customViewports && customViewports . length == 0 ) {
203
+ return
204
+ }
205
+ customViewports . forEach ( element => {
206
+ if ( ! Array . isArray ( element . stories ) || element . stories == 0 ) {
207
+ throw new ValidationError ( 'Missing `stories` in customViewports config. please check the config file' ) ;
208
+ }
209
+ if ( ! element . styles || ! element . styles ?. width ) {
210
+ throw new ValidationError ( 'Missing `styles` in customViewports key. Please check the config file' ) ;
211
+ }
212
+
213
+ let width = element . styles . width ;
214
+ let height = element . styles . height ;
215
+ if ( width && typeof width != 'number' ) {
216
+ width = Number ( width ) ;
217
+ }
218
+ if ( height && typeof height != 'number' ) {
219
+ height = Number ( height ) ;
220
+ }
221
+ if ( width && width < MIN_RESOLUTION_WIDTH || width > MAX_RESOLUTION_WIDTH ) {
222
+ throw new ValidationError ( `customViewports.styles width must be > ${ MIN_RESOLUTION_WIDTH } , < ${ MAX_RESOLUTION_WIDTH } ` ) ;
223
+ }
224
+ if ( height & ( height < MIN_RESOLUTION_WIDTH || height > MAX_RESOLUTION_WIDTH ) ) {
225
+ throw new ValidationError ( `customViewports.styles height must be > ${ MIN_RESOLUTION_HEIGHT } , < ${ MAX_RESOLUTION_HEIGHT } ` ) ;
226
+ }
227
+ element . styles . width = width ;
228
+ element . styles . height = height ;
229
+ } ) ;
230
+ return
231
+ }
232
+
188
233
module . exports = {
189
234
ValidationError,
190
235
validateProjectToken,
@@ -193,5 +238,6 @@ module.exports = {
193
238
validateLatestBuild,
194
239
validateConfig,
195
240
validateConfigBrowsers,
196
- validateConfigResolutions
241
+ validateConfigResolutions,
242
+ validateCustomViewPorts
197
243
} ;
0 commit comments