Skip to content

Commit df63be6

Browse files
authored
feat: adjusts for adapter's signal (#655)
1 parent 9a4b654 commit df63be6

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"doc_path": "./yeoman-environment-doc"
5858
},
5959
"dependencies": {
60-
"@yeoman/adapter": "^3.0.0",
60+
"@yeoman/adapter": "^3.1.0",
6161
"@yeoman/conflicter": "^3.0.0",
6262
"@yeoman/namespace": "^1.0.1",
6363
"@yeoman/transform": "^2.1.0",

src/environment-base.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)