@@ -1025,14 +1025,7 @@ class Environment extends Base {
1025
1025
} ;
1026
1026
1027
1027
if ( schedule ) {
1028
- this . runLoop . add ( 'environment:run' , async ( done , stop ) => {
1029
- try {
1030
- await runGenerator ( ) ;
1031
- done ( ) ;
1032
- } catch ( error ) {
1033
- stop ( error ) ;
1034
- }
1035
- } ) ;
1028
+ this . queueTask ( 'environment:run' , ( ) => runGenerator ( ) ) ;
1036
1029
} else {
1037
1030
await runGenerator ( ) ;
1038
1031
}
@@ -1352,38 +1345,26 @@ class Environment extends Base {
1352
1345
queueConflicter ( ) {
1353
1346
const queueCommit = ( ) => {
1354
1347
debug ( 'Queueing conflicts task' ) ;
1355
- this . runLoop . add (
1348
+ this . queueTask (
1356
1349
'environment:conflicts' ,
1357
- ( done , stop ) => {
1350
+ async ( ) => {
1358
1351
let customCommitTask = this . findGeneratorCustomCommitTask ( ) ;
1359
1352
if ( customCommitTask !== undefined && customCommitTask ) {
1360
1353
if ( typeof customCommitTask !== 'function' ) {
1361
- done ( ) ;
1354
+ // There is a custom commit task or just disabled
1362
1355
return ;
1363
1356
}
1364
1357
} else {
1358
+ // Using default commit task
1365
1359
customCommitTask = this . commitSharedFs . bind ( this ) ;
1366
1360
}
1367
1361
1362
+ await customCommitTask ( ) ;
1363
+
1368
1364
if ( this . enableConflicterIgnore ) {
1369
1365
debug ( 'Adding queueCommit event listener' ) ;
1370
1366
this . sharedFs . once ( 'change' , queueCommit ) ;
1371
1367
}
1372
-
1373
- const result = customCommitTask ( ) ;
1374
- if ( ! result || ! result . then ) {
1375
- done ( ) ;
1376
- return ;
1377
- }
1378
-
1379
- return result . then ( ( ) => {
1380
- if ( ! this . enableConflicterIgnore ) {
1381
- debug ( 'Adding queueCommit event listener' ) ;
1382
- this . sharedFs . once ( 'change' , queueCommit ) ;
1383
- }
1384
-
1385
- done ( ) ;
1386
- } , stop ) ;
1387
1368
} ,
1388
1369
{
1389
1370
once : 'write memory fs to disk' ,
@@ -1398,7 +1379,35 @@ class Environment extends Base {
1398
1379
* Queue environment's package manager install task.
1399
1380
*/
1400
1381
queuePackageManagerInstall ( ) {
1401
- this . runLoop . add ( 'install' , ( done , stop ) => this . packageManagerInstallTask ( ) . then ( done , stop ) , { once : 'package manager install' } ) ;
1382
+ this . queueTask ( 'install' , ( ) => this . packageManagerInstallTask ( ) , { once : 'package manager install' } ) ;
1383
+ }
1384
+
1385
+ /**
1386
+ * Queue tasks
1387
+ * @param {string } priority
1388
+ * @param {(...args: any[]) => void | Promise<void> } task
1389
+ * @param {{ once?: string, startQueue?: boolean } } [options]
1390
+ */
1391
+ queueTask ( priority , task , options ) {
1392
+ return new Promise ( ( resolve , reject ) => {
1393
+ this . runLoop . add (
1394
+ priority ,
1395
+ async ( done , stop ) => {
1396
+ try {
1397
+ const result = await task ( ) ;
1398
+ done ( result ) ;
1399
+ resolve ( result ) ;
1400
+ } catch ( error ) {
1401
+ stop ( error ) ;
1402
+ reject ( error ) ;
1403
+ }
1404
+ } ,
1405
+ {
1406
+ once : options . once ,
1407
+ run : options . startQueue ?? false ,
1408
+ } ,
1409
+ ) ;
1410
+ } ) ;
1402
1411
}
1403
1412
}
1404
1413
Object . assign ( Environment . prototype , resolver ) ;
0 commit comments