@@ -504,7 +504,7 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
504504 // Backward compatibility
505505 const oldGenerator : any = generator ;
506506 if ( ! oldGenerator . options . forwardErrorToEnvironment ) {
507- oldGenerator . on ( 'error' , ( error : any ) => this . emit ( 'error' , error ) ) ;
507+ oldGenerator . on ( 'error' , ( error : any ) => this . adapter . abort ( error ) ) ;
508508 }
509509
510510 oldGenerator . promise = oldGenerator . run ( ) ;
@@ -828,6 +828,10 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
828828 */
829829 protected async start ( options : any ) {
830830 return new Promise < void > ( ( resolve , reject ) => {
831+ if ( this . adapter . signal ?. aborted ) {
832+ return reject ( this . adapter . signal . reason ) ;
833+ }
834+
831835 Object . assign ( this . options , removePropertiesWithNullishValues ( pick ( options , [ 'skipInstall' , 'nodePackageManager' ] ) ) ) ;
832836 this . logCwd = options . logCwd ?? this . logCwd ;
833837 const conflicterOptionsProperties = [ 'force' , 'bail' , 'ignoreWhitespace' , 'dryRun' , 'skipYoResolve' ] ;
@@ -846,25 +850,21 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
846850 * would be killed if an error is thrown to environment.
847851 * Make sure to not rely on that behavior.
848852 */
849- this . on ( 'error' , async error => {
850- this . runLoop . pause ( ) ;
851- await this . adapter . onIdle ?.( ) ;
852- reject ( error ) ;
853- this . adapter . close ( ) ;
853+ let errorEmitted = false ;
854+ this . on ( 'error' , error => {
855+ errorEmitted = true ;
856+ this . adapter . abort ( error ) ;
854857 } ) ;
855858
856859 this . once ( 'end' , async ( ) => {
857860 await this . adapter . onIdle ?.( ) ;
858861 resolve ( ) ;
859- this . adapter . close ( ) ;
860862 } ) ;
861863
862864 /*
863865 * For backward compatibility
864866 */
865- this . on ( 'generator:reject' , error => {
866- this . emit ( 'error' , error ) ;
867- } ) ;
867+ this . on ( 'generator:reject' , error => this . adapter . abort ( error ) ) ;
868868
869869 /*
870870 * For backward compatibility
@@ -873,9 +873,7 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
873873 this . emit ( 'end' ) ;
874874 } ) ;
875875
876- this . runLoop . on ( 'error' , ( error : any ) => {
877- this . emit ( 'error' , error ) ;
878- } ) ;
876+ this . runLoop . on ( 'error' , ( error : any ) => this . adapter . abort ( error ) ) ;
879877
880878 this . runLoop . on ( 'paused' , ( ) => {
881879 this . emit ( 'paused' ) ;
@@ -886,6 +884,16 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
886884 this . emit ( 'end' ) ;
887885 } ) ;
888886
887+ this . adapter . signal ?. addEventListener ( 'abort' , async ( ) => {
888+ const reason = this . adapter . signal . reason ;
889+ if ( ! errorEmitted ) {
890+ this . emit ( 'error' , reason ) ;
891+ }
892+ this . runLoop . pause ( ) ;
893+ await this . adapter . onIdle ?.( ) ;
894+ reject ( reason ) ;
895+ } ) ;
896+
889897 this . emit ( 'run' ) ;
890898 this . runLoop . start ( ) ;
891899 } ) ;
0 commit comments