@@ -17,29 +17,30 @@ class ValidationError extends Error {
1717 }
1818}
1919
20- function validateProjectToken ( options ) {
21- console . log ( "options" , options ) ;
22- if ( process . env . PROJECT_TOKEN ) {
20+ function validateProjectToken ( options , logger ) {
21+ logger . debug ( "options" , options ) ;
22+ if ( process . env . PROJECT_TOKEN ) {
2323 return axios . get ( constants [ options . env ] . AUTH_URL , {
2424 headers : {
2525 projectToken : process . env . PROJECT_TOKEN
26- } } )
26+ }
27+ } )
2728 . then ( function ( response ) {
28- console . log ( '[smartui] Project Token Validated' ) ;
29+ logger . info ( '[smartui] Project Token Validated' ) ;
2930 } )
3031 . catch ( function ( error ) {
3132 if ( error . response ) {
32- console . log ( '[smartui] Error: Invalid Project Token' ) ;
33+ logger . error ( '[smartui] Error: Invalid Project Token' ) ;
3334 } else if ( error . request ) {
34- console . log ( '[smartui] Project Token not validated. Error: ' , error . message ) ;
35+ logger . error ( '[smartui] Project Token not validated. Error: ' , error . message ) ;
3536 } else {
36- console . log ( '[smartui] Project Token not validated. Error: ' , error . message ) ;
37+ logger . error ( '[smartui] Project Token not validated. Error: ' , error . message ) ;
3738 }
3839 process . exit ( constants . ERROR_CATCHALL ) ;
39- } ) ;
40+ } ) ;
4041 }
41- else {
42- console . log ( '[smartui] Error: please set PROJECT_TOKEN key, refer to https://smartui.lambdatest.com' ) ;
42+ else {
43+ logger . error ( '[smartui] Error: please set PROJECT_TOKEN key, refer to https://smartui.lambdatest.com' ) ;
4344 process . exit ( constants . ERROR_CATCHALL ) ;
4445 }
4546} ;
@@ -94,7 +95,8 @@ async function validateLatestBuild(options) {
9495 params : {
9596 branch : commit . branch ,
9697 commitId : commit . shortHash
97- } } )
98+ }
99+ } )
98100 . then ( function ( response ) {
99101 if ( response . data . status === 'Failure' ) {
100102 console . log ( `[smartui] Build with commit '${ commit . shortHash } ' on branch '${ commit . branch } ' already exists.` ) ;
@@ -198,12 +200,56 @@ function parse(file) {
198200 return JSON . parse ( data ) ;
199201}
200202
201- // Verify Screenshot config
202- function validateScreenshotConfig ( configFile ) {
203- // Verify config file exists
203+ // Verify Screenshot config
204+ function validateScreenshotConfig ( configFile , logger ) {
205+ // Check for JSON extension
206+ if ( ! isJSONFile ( configFile ) ) {
207+ logger . error ( 'capture command only supports json file' ) ;
208+ process . exit ( constants . ERROR_CATCHALL ) ;
209+ }
210+
211+ // Check if file exists
212+ if ( configFile ) {
213+ try {
214+ fs . accessSync ( configFile , fs . constants . F_OK ) ;
215+ } catch ( error ) {
216+ logger . error ( 'Error: File does not exist ' + configFile ) ;
217+ process . exit ( constants . ERROR_CATCHALL ) ;
218+ }
219+
220+ }
221+
222+
223+ let screenshots = { } ;
224+ // Check JSON Parse Error
225+ if ( configFile ) {
226+ try {
227+ screenshots = parse ( configFile )
228+ } catch ( error ) {
229+ logger . error ( 'Error: Invalid json file' ) ;
230+ process . exit ( constants . ERROR_CATCHALL ) ;
231+ }
232+ }
233+
234+ logger . debug ( screenshots )
235+
236+ //Check for URLs should not be empty
237+ for ( const screenshot of screenshots ) {
238+ if ( ! screenshot . url || screenshot . url == '' ) {
239+ logger . error ( 'Error: Missing required URL for screenshot' ) ;
240+ process . exit ( constants . ERROR_CATCHALL ) ;
241+ }
242+ //Check for URLs should valid (like abcd in URL)
243+ try {
244+ new URL ( screenshot . url ) ;
245+ } catch ( error ) {
246+ logger . error ( 'Error: Invalid screenshot URL: ' + screenshot . url ) ;
247+ process . exit ( constants . ERROR_CATCHALL ) ;
248+ }
249+ }
204250}
205251
206- module . exports = {
252+ module . exports = {
207253 ValidationError,
208254 validateProjectToken,
209255 validateStorybookUrl,
0 commit comments