@@ -281,7 +281,11 @@ function getPath() {
281281 * expect(output.trim()).toBe('sentry-cli x.y.z');
282282 *
283283 * @param {string[] } args Command line arguments passed to `sentry-cli`.
284- * @param {boolean } live We inherit stdio to display `sentry-cli` output directly.
284+ * @param {boolean | 'rejectOnError' } live can be set to:
285+ * - `true` to inherit stdio to display `sentry-cli` output directly.
286+ * - `false` to not inherit stdio and return the output as a string.
287+ * - `'rejectOnError'` to inherit stdio and reject the promise if the command
288+ * exits with a non-zero exit code.
285289 * @param {boolean } silent Disable stdout for silents build (CI/Webpack Stats, ...)
286290 * @param {string } [configFile] Relative or absolute path to the configuration file.
287291 * @param {Object } [config] More configuration to pass to the CLI
@@ -322,16 +326,27 @@ async function execute(args, live, silent, configFile, config = {}) {
322326 ] ) ;
323327 args = [ ...headers , ...args ] ;
324328 }
329+
325330 return new Promise ( ( resolve , reject ) => {
326- if ( live === true ) {
331+ if ( live === true || live === 'rejectOnError' ) {
327332 const output = silent ? 'ignore' : 'inherit' ;
328333 const pid = childProcess . spawn ( getPath ( ) , args , {
329334 env,
330335 // stdin, stdout, stderr
331336 stdio : [ 'ignore' , output , output ] ,
332337 } ) ;
333- pid . on ( 'exit' , ( ) => {
334- // @ts -expect-error - this is a TODO (v3) to fix and resolve a string here
338+ pid . on ( 'exit' , ( exitCode ) => {
339+ if ( live === 'rejectOnError' ) {
340+ if ( exitCode === 0 ) {
341+ resolve ( 'success (live mode)' ) ;
342+ }
343+ reject ( new Error ( `Command ${ args . join ( ' ' ) } failed with exit code ${ exitCode } ` ) ) ;
344+ }
345+ // According to the type definition, resolving with void is not allowed.
346+ // However, for backwards compatibility, we resolve void here to
347+ // avoid a behaviour-breaking change.
348+ // TODO (v3): Clean this up and always resolve a string (or change the type definition)
349+ // @ts -expect-error - see comment above
335350 resolve ( ) ;
336351 } ) ;
337352 } else {
0 commit comments