@@ -125,7 +125,10 @@ function validateConfig(configFile) {
125125
126126 try {
127127 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 )
129132 } catch ( error ) {
130133 console . log ( `[smartui] Error: Invalid config, ${ error . message } ` ) ;
131134 process . exit ( constants . ERROR_CATCHALL ) ;
@@ -136,7 +139,7 @@ function validateConfig(configFile) {
136139 storybookConfig . waitForTimeout = 0 ;
137140 } else if ( storybookConfig . waitForTimeout <= 0 || storybookConfig . waitForTimeout > 30000 ) {
138141 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.' ) ;
140143 storybookConfig . waitForTimeout = 0 ;
141144 }
142145
@@ -158,25 +161,31 @@ function validateConfigBrowsers(browsers) {
158161
159162function validateConfigResolutions ( resolutions ) {
160163 if ( ! Array . isArray ( resolutions ) ) {
161- throw new ValidationError ( 'invalid resolutions .' ) ;
164+ throw new ValidationError ( 'Invalid viewports config. Please add atleast one viewport .' ) ;
162165 }
163166 if ( resolutions . length == 0 ) {
164- throw new ValidationError ( 'empty resolutions list in config.' ) ;
167+ throw new ValidationError ( 'Empty viewports list in config.' ) ;
165168 }
166169 if ( resolutions . length > 5 ) {
167170 throw new ValidationError ( `max resolutions: ${ MAX_RESOLUTIONS } ` ) ;
168171 }
169172 let res = [ ] ;
170173 resolutions . forEach ( element => {
171174 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 .' ) ;
173176 }
174177 let width = element [ 0 ] ;
175178 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 ) {
177186 throw new ValidationError ( `width must be > ${ MIN_RESOLUTION_WIDTH } , < ${ MAX_RESOLUTION_WIDTH } ` ) ;
178187 }
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 ) ) {
180189 throw new ValidationError ( `height must be > ${ MIN_RESOLUTION_HEIGHT } , < ${ MAX_RESOLUTION_HEIGHT } ` ) ;
181190 }
182191 res . push ( [ width , height || 0 ] ) ;
@@ -185,6 +194,42 @@ function validateConfigResolutions(resolutions) {
185194 return res
186195}
187196
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+
188233module . exports = {
189234 ValidationError,
190235 validateProjectToken,
@@ -193,5 +238,6 @@ module.exports = {
193238 validateLatestBuild,
194239 validateConfig,
195240 validateConfigBrowsers,
196- validateConfigResolutions
241+ validateConfigResolutions,
242+ validateCustomViewPorts
197243} ;
0 commit comments