Skip to content

Commit c96082f

Browse files
committed
Implement registerPriorities.
1 parent 245fcf5 commit c96082f

File tree

1 file changed

+87
-76
lines changed

1 file changed

+87
-76
lines changed

lib/index.js

Lines changed: 87 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -299,98 +299,109 @@ class Generator extends EventEmitter {
299299

300300
// Add custom queues
301301
if (Array.isArray(this.options.customPriorities)) {
302-
const customPriorities = this.options.customPriorities.map(customPriority => {
303-
// Keep backward compatibility with name
304-
const newPriority = { priorityName: customPriority.name, ...customPriority };
305-
delete newPriority.name;
306-
return newPriority;
307-
});
302+
this.registerPriorities(this.options.customPriorities);
303+
}
308304

309-
// Sort customPriorities, a referenced custom queue must be added before the one that reference it.
310-
customPriorities.sort((a, b) => {
311-
if (a.priorityName === b.priorityName) {
312-
throw new Error(`Duplicate custom queue ${a.name}`);
313-
}
305+
this.compose = this.options.compose;
314306

315-
if (a.priorityName === b.before) {
316-
return -1;
317-
}
307+
// Expose utilities for dependency-less generators.
308+
this._ = _;
309+
}
318310

319-
if (b.priorityName === a.before) {
320-
return 1;
321-
}
311+
/**
312+
* Register priorities for this generator
313+
*
314+
* @param {Object[]} priorities - Priorities
315+
* @param {String} priorities.priorityName - Priority name
316+
* @param {String} priorities.before - The new priority will be queued before the `before` priority.
317+
* @param {String} [priorities.queueName] - Name to be used at grouped-queue
318+
*/
319+
registerPriorities(priorities) {
320+
const customPriorities = priorities.map(customPriority => {
321+
// Keep backward compatibility with name
322+
const newPriority = { priorityName: customPriority.name, ...customPriority };
323+
delete newPriority.name;
324+
return newPriority;
325+
});
322326

323-
return 0;
324-
});
327+
// Sort customPriorities, a referenced custom queue must be added before the one that reference it.
328+
customPriorities.sort((a, b) => {
329+
if (a.priorityName === b.priorityName) {
330+
throw new Error(`Duplicate custom queue ${a.name}`);
331+
}
325332

326-
// Add queue to runLoop
327-
customPriorities.forEach(customQueue => {
328-
customQueue.queueName =
329-
customQueue.queueName ||
330-
`${this.options.namespace}#${customQueue.priorityName}`;
331-
debug(`Registering custom queue ${customQueue.queueName}`);
332-
this._queues[customQueue.priorityName] = customQueue;
333+
if (a.priorityName === b.before) {
334+
return -1;
335+
}
333336

334-
if (this.env.runLoop.queueNames.includes(customQueue.queueName)) {
335-
return;
336-
}
337+
if (b.priorityName === a.before) {
338+
return 1;
339+
}
337340

338-
// Backwards compatibilitiy with grouped-queue < 1.0.0
339-
if (!this.env.runLoop.addSubQueue) {
340-
let SubQueue;
341-
try {
342-
SubQueue = require('grouped-queue/lib/subqueue');
343-
} catch (error) {
344-
throw new Error(
345-
"The running environment doesn't have the necessary features to run this generator. Update it and run again."
346-
);
347-
}
341+
return 0;
342+
});
348343

349-
this.env.runLoop.addSubQueue = function(name, before) {
350-
if (this.__queues__[name]) {
351-
// Sub-queue already exists
352-
return;
353-
}
344+
// Add queue to runLoop
345+
customPriorities.forEach(customQueue => {
346+
customQueue.queueName =
347+
customQueue.queueName || `${this.options.namespace}#${customQueue.priorityName}`;
348+
debug(`Registering custom queue ${customQueue.queueName}`);
349+
this._queues[customQueue.priorityName] = customQueue;
354350

355-
if (!before) {
356-
// Add at last place.
357-
this.__queues__[name] = new SubQueue();
358-
this.queueNames.push(name);
359-
return;
360-
}
351+
if (this.env.runLoop.queueNames.includes(customQueue.queueName)) {
352+
return;
353+
}
361354

362-
if (!this.__queues__[before] || _.indexOf(this.queueNames, before) === -1) {
363-
throw new Error('sub-queue ' + before + ' not found');
364-
}
355+
// Backwards compatibilitiy with grouped-queue < 1.0.0
356+
if (!this.env.runLoop.addSubQueue) {
357+
let SubQueue;
358+
try {
359+
SubQueue = require('grouped-queue/lib/subqueue');
360+
} catch (error) {
361+
throw new Error(
362+
"The running environment doesn't have the necessary features to run this generator. Update it and run again."
363+
);
364+
}
365365

366-
const current = this.__queues__;
367-
const currentNames = Object.keys(current);
368-
// Recreate the queue with new order.
369-
this.__queues__ = {};
370-
currentNames.forEach(currentName => {
371-
if (currentName === before) {
372-
this.__queues__[name] = new SubQueue();
373-
}
366+
this.env.runLoop.addSubQueue = function(name, before) {
367+
if (this.__queues__[name]) {
368+
// Sub-queue already exists
369+
return;
370+
}
374371

375-
this.__queues__[currentName] = current[currentName];
376-
});
372+
if (!before) {
373+
// Add at last place.
374+
this.__queues__[name] = new SubQueue();
375+
this.queueNames.push(name);
376+
return;
377+
}
377378

378-
// Recreate queueNames
379-
this.queueNames = Object.keys(this.__queues__);
380-
};
381-
}
379+
if (!this.__queues__[before] || _.indexOf(this.queueNames, before) === -1) {
380+
throw new Error('sub-queue ' + before + ' not found');
381+
}
382382

383-
let beforeQueue = customQueue.before
384-
? this._queues[customQueue.before].queueName
385-
: undefined;
386-
this.env.runLoop.addSubQueue(customQueue.queueName, beforeQueue);
387-
});
388-
}
383+
const current = this.__queues__;
384+
const currentNames = Object.keys(current);
385+
// Recreate the queue with new order.
386+
this.__queues__ = {};
387+
currentNames.forEach(currentName => {
388+
if (currentName === before) {
389+
this.__queues__[name] = new SubQueue();
390+
}
389391

390-
this.compose = this.options.compose;
392+
this.__queues__[currentName] = current[currentName];
393+
});
391394

392-
// Expose utilities for dependency-less generators.
393-
this._ = _;
395+
// Recreate queueNames
396+
this.queueNames = Object.keys(this.__queues__);
397+
};
398+
}
399+
400+
let beforeQueue = customQueue.before
401+
? this._queues[customQueue.before].queueName
402+
: undefined;
403+
this.env.runLoop.addSubQueue(customQueue.queueName, beforeQueue);
404+
});
394405
}
395406

396407
checkEnvironmentVersion(packageDependency, version) {

0 commit comments

Comments
 (0)