11const fs = require ( 'fs' ) ;
22const path = require ( 'path' ) ;
3- const { env } = require ( 'process' ) ;
3+ const { env, exit } = require ( 'process' ) ;
44
55const updateArgument = '--update-sentry-capacitor' ;
66
@@ -29,22 +29,17 @@ function SkipPostInstall() {
2929 */
3030function GetRequiredSiblingVersion ( ) {
3131 if ( env . npm_package_dependencies__sentry_browser ) {
32- // Yarn.
32+ // Yarn V1 .
3333 return env . npm_package_dependencies__sentry_browser ;
3434 }
3535
3636 let capacitorPackagePath = '' ;
37- if ( env . npm_package_json ) {
38- // NPM.
39- capacitorPackagePath = env . npm_package_json ;
40- }
41- else if ( __dirname ) {
37+ if ( __dirname ) {
4238 capacitorPackagePath = path . join ( __dirname , '..' , 'package.json' ) ;
4339 }
4440 else {
4541 return undefined ;
4642 }
47-
4843 const capacitorPackageJson = fs . readFileSync ( capacitorPackagePath , 'utf8' ) ;
4944
5045 const version = capacitorPackageJson . match ( jsonFilter ) ;
@@ -59,7 +54,7 @@ function GetRequiredSiblingVersion() {
5954 * This function will throw if the paramater contains a sibling with different version to the one used
6055 * by the SDK or if no version were specified by the user.
6156 */
62- function ValidateSentryPackageParameters ( packages ) {
57+ function ValidateSentryPackageParameters ( packages , siblingVersion ) {
6358 let errorMessages = [ ] ;
6459 var packageFilter = / .* ( c a p a c i t o r | c l i | w i z a r d | t y p e s c r i p t ) / ;
6560 for ( const argPackage of packages ) {
@@ -75,18 +70,27 @@ function ValidateSentryPackageParameters(packages) {
7570 }
7671
7772 if ( errorMessages . length > 0 ) {
78- throw errorMessages . join ( "\n" ) ;
73+ console . error ( `⚠️ ${ errorMessages . join ( "\n" ) } ` ) ;
74+ exit ( 1 ) ;
7975 }
8076}
8177
8278/**
8379 * @return {String } The path where package.json is located.
8480 */
8581function GetPackageJsonRootPath ( ) {
82+
83+ // Avaliable when using NPM.
8684 if ( env . INIT_CWD ) {
8785 // Avaliable when using NPM.
8886 return env . INIT_CWD + '/' ;
8987 }
88+
89+ // Unix only.
90+ if ( env . PWD ) {
91+ return env . PWD + '/' ;
92+ }
93+
9094 let packagePath = __dirname + '/../../' ;
9195 while ( ! fs . existsSync ( path . resolve ( packagePath , 'package.json' ) ) ) {
9296 packagePath += '../' ;
@@ -99,8 +103,8 @@ function GetPackageJsonRootPath() {
99103 * @return {String } The path where package.json is located.
100104 */
101105function FormatPackageInstallCommand ( sentryPackages ) {
102- // Yarn
103- if ( env . npm_config_argv ) {
106+ // Yarn V1 || Yarn V3/V4.
107+ if ( env . npm_config_argv || env . npm_config_user_agent ?. startsWith ( 'yarn' ) ) {
104108 return "yarn add --exact " + sentryPackages + " " + updateArgument ;
105109 }
106110 else {
@@ -110,7 +114,6 @@ function FormatPackageInstallCommand(sentryPackages) {
110114}
111115
112116function CheckSiblings ( ) {
113-
114117 if ( SkipPostInstall ( ) ) {
115118 return ;
116119 }
@@ -125,7 +128,7 @@ function CheckSiblings() {
125128 // Only available on Yarn.
126129 const npmAction = JSON . parse ( env . npm_config_argv ) ;
127130 if ( npmAction . original && npmAction . original . length > 1 ) {
128- ValidateSentryPackageParameters ( npmAction . original ) ;
131+ ValidateSentryPackageParameters ( npmAction . original , siblingVersion ) ;
129132 return ;
130133 }
131134 }
@@ -134,25 +137,28 @@ function CheckSiblings() {
134137 let rootPath = GetPackageJsonRootPath ( ) ;
135138 let incompatiblePackages = [ ] ;
136139 const packageJson = fs . readFileSync ( rootPath + 'package.json' , 'utf8' ) . split ( "\n" ) ;
137-
138140 for ( const lineData of packageJson ) {
139141 let sentryRef = lineData . match ( jsonFilter ) ;
140142 if ( sentryRef && sentryRef [ 2 ] !== siblingVersion && ! sentryRef [ 2 ] . includes ( '%3A' + siblingVersion + '#' ) ) {
141143 incompatiblePackages . push ( [ '@sentry/' + sentryRef [ 1 ] , sentryRef [ 2 ] ] ) ;
142144 }
143145 }
144146 if ( incompatiblePackages . length > 0 ) {
145- const IncompatibilityError = [ "This version of Sentry Capacitor is incompatible with the following installed packages: " ] ;
147+ const IncompatibilityError = [ "This version of Sentry Capacitor is incompatible with the following installed packages:" ] ;
146148 let packagesList = ''
147149 for ( const sentryPackage of incompatiblePackages ) {
148150 IncompatibilityError . push ( sentryPackage [ 0 ] + ' version ' + sentryPackage [ 1 ] ) ;
149151 packagesList += sentryPackage [ 0 ] + '@' + siblingVersion + ' ' ;
150152 }
151- IncompatibilityError . push ( "Please install the mentioned packages exactly with version " + siblingVersion + " and with the argument " + updateArgument ) ;
152- IncompatibilityError . push ( FormatPackageInstallCommand ( packagesList ) ) ;
153- throw IncompatibilityError . join ( "\n" ) ;
154- }
153+ IncompatibilityError . push (
154+ `Please install the mentioned packages exactly with version ${ siblingVersion } and with the argument ${ updateArgument } .
155+ Your project will build with the wrong package but you may face Runtime errors.
156+ You can use the below command to fix your package.json:` ) ;
155157
158+ console . error ( `⚠️ ${ IncompatibilityError . join ( "\n" ) } ` ) ;
159+ console . warn ( ` ${ FormatPackageInstallCommand ( packagesList ) } ` ) ;
160+ exit ( 1 ) ;
161+ }
156162}
157163
158164CheckSiblings ( ) ;
0 commit comments