@@ -351,9 +351,43 @@ const getCiInfo = () => {
351351
352352let packages = { } ;
353353
354- exports . getPackageVersion = ( package_ ) => {
354+ exports . getPackageVersion = ( package_ , bsConfig = null ) => {
355355 if ( packages [ package_ ] ) return packages [ package_ ] ;
356- return packages [ package_ ] = this . requireModule ( `${ package_ } /package.json` ) . version ;
356+ let packageVersion ;
357+ /* Try to find version from module path */
358+ try {
359+ packages [ package_ ] = this . requireModule ( `${ package_ } /package.json` ) . version ;
360+ logger . info ( `Getting ${ package_ } package version from module path = ${ packages [ package_ ] } ` ) ;
361+ packageVersion = packages [ package_ ] ;
362+ } catch ( e ) {
363+ exports . debug ( `Unable to find package ${ package_ } at module path with error ${ e } ` ) ;
364+ }
365+
366+ /* Read package version from npm_dependencies in browserstack.json file if present */
367+ if ( utils . isUndefined ( packageVersion ) && bsConfig && ( process . env . BROWSERSTACK_AUTOMATION == "true" || process . env . BROWSERSTACK_AUTOMATION == "1" ) ) {
368+ const runSettings = bsConfig . run_settings ;
369+ if ( runSettings && runSettings . npm_dependencies !== undefined &&
370+ Object . keys ( runSettings . npm_dependencies ) . length !== 0 &&
371+ typeof runSettings . npm_dependencies === 'object' ) {
372+ if ( package_ in runSettings . npm_dependencies ) {
373+ packages [ package_ ] = runSettings . npm_dependencies [ package_ ] ;
374+ logger . info ( `Getting ${ package_ } package version from browserstack.json = ${ packages [ package_ ] } ` ) ;
375+ packageVersion = packages [ package_ ] ;
376+ }
377+ }
378+ }
379+
380+ /* Read package version from project's package.json if present */
381+ const packageJSONPath = path . join ( process . cwd ( ) , 'package.json' ) ;
382+ if ( utils . isUndefined ( packageVersion ) && fs . existsSync ( packageJSONPath ) ) {
383+ const packageJSONContents = require ( packageJSONPath ) ;
384+ if ( packageJSONContents . devDependencies && ! utils . isUndefined ( packageJSONContents . devDependencies [ package_ ] ) ) packages [ package_ ] = packageJSONContents . devDependencies [ package_ ] ;
385+ if ( packageJSONContents . dependencies && ! utils . isUndefined ( packageJSONContents . dependencies [ package_ ] ) ) packages [ package_ ] = packageJSONContents . dependencies [ package_ ] ;
386+ logger . info ( `Getting ${ package_ } package version from package.json = ${ packages [ package_ ] } ` ) ;
387+ packageVersion = packages [ package_ ] ;
388+ }
389+
390+ return packageVersion ;
357391}
358392
359393exports . getAgentVersion = ( ) => {
@@ -452,9 +486,14 @@ const setBrowserstackCypressCliDependency = (bsConfig) => {
452486}
453487
454488const getCypressConfigFileContent = ( bsConfig , cypressConfigPath ) => {
455- const cypressConfigFile = require ( path . resolve ( bsConfig ? bsConfig . run_settings . cypress_config_file : cypressConfigPath ) ) ;
456- if ( bsConfig ) process . env . OBS_CRASH_REPORTING_CYPRESS_CONFIG_PATH = bsConfig . run_settings . cypress_config_file ;
457- return cypressConfigFile ;
489+ try {
490+ const cypressConfigFile = require ( path . resolve ( bsConfig ? bsConfig . run_settings . cypress_config_file : cypressConfigPath ) ) ;
491+ if ( bsConfig ) process . env . OBS_CRASH_REPORTING_CYPRESS_CONFIG_PATH = bsConfig . run_settings . cypress_config_file ;
492+ return cypressConfigFile ;
493+ } catch ( e ) {
494+ exports . debug ( `Encountered an error when trying to import Cypress Config File ${ e } ` ) ;
495+ return { } ;
496+ }
458497}
459498
460499exports . setCrashReportingConfigFromReporter = ( credentialsStr , bsConfigPath , cypressConfigPath ) => {
@@ -530,7 +569,7 @@ exports.launchTestSession = async (user_config, bsConfigPath) => {
530569 'version_control' : await getGitMetaData ( ) ,
531570 'observability_version' : {
532571 frameworkName : "Cypress" ,
533- frameworkVersion : exports . getPackageVersion ( 'cypress' ) ,
572+ frameworkVersion : exports . getPackageVersion ( 'cypress' , user_config ) ,
534573 sdkVersion : exports . getAgentVersion ( )
535574 }
536575 } ;
0 commit comments