Skip to content

Commit

Permalink
Add support to object on queueMethod (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored and SBoudrias committed Dec 15, 2019
1 parent 866cea1 commit 14651ca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
51 changes: 37 additions & 14 deletions lib/index.js
Expand Up @@ -20,6 +20,11 @@ const promptSuggestion = require('./util/prompt-suggestion');

const EMPTY = '@@_YEOMAN_EMPTY_MARKER_@@';

// Ensure a prototype method is a candidate run by default
const methodIsValid = function(name) {
return name.charAt(0) !== '_' && name !== 'constructor';
};

/**
* The `Generator` class provides the common API shared by all generators.
* It define options, arguments, file, prompt, log, API, etc.
Expand Down Expand Up @@ -383,9 +388,39 @@ class Generator extends EventEmitter {
});
}

/**
* Schedule methods on a run queue.
*
* @param {Function|Object} method: Method to be scheduled or object with function properties.
* @param {String} [methodName]: Name of the method to be scheduled.
* @param {String} [queueName]: Name of the queue to be scheduled on.
* @param {String} [reject]: Reject callback.
*/
queueMethod(method, methodName, queueName, reject = () => {}) {
if (typeof queueName === 'function') {
reject = queueName;
queueName = 'default';
} else {
queueName = queueName || 'default';
}

const self = this;
queueName = queueName || 'default';
if (!_.isFunction(method)) {
if (typeof methodName === 'function') {
reject = methodName;
methodName = undefined;
}

queueName = methodName || queueName;
// Run each queue items
_.each(method, (newMethod, newMethodName) => {
if (!_.isFunction(newMethod) || !methodIsValid(newMethodName)) return;

self.queueMethod(newMethod, newMethodName, queueName, reject);
});
return;
}

let namespace = '';
if (self.options && self.options.namespace) {
namespace = self.options.namespace;
Expand Down Expand Up @@ -451,11 +486,6 @@ class Generator extends EventEmitter {
resolve();
});

// Ensure a prototype method is a candidate run by default
function methodIsValid(name) {
return name.charAt(0) !== '_' && name !== 'constructor';
}

function addInQueue(name) {
const property = Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(self),
Expand All @@ -475,14 +505,7 @@ class Generator extends EventEmitter {
return;
}

// Run each queue items
_.each(item, (method, methodName) => {
if (!_.isFunction(method) || !methodIsValid(methodName)) {
return;
}

self.queueMethod(method, methodName, queueName, reject);
});
self.queueMethod(item, queueName, reject);
}

validMethods.forEach(addInQueue);
Expand Down
11 changes: 8 additions & 3 deletions test/base.js
Expand Up @@ -1181,9 +1181,14 @@ describe('Base', () => {
constructor(args, opts) {
super(args, opts);

this.queueMethod(function() {
this.queue = this.options.testQueue;
}, 'testQueue');
this.queueMethod(
{
testQueue: function() {
this.queue = this.options.testQueue;
}
},
() => {}
);
}

exec() {}
Expand Down

0 comments on commit 14651ca

Please sign in to comment.