Skip to content

Commit

Permalink
Implement taskPrefix feature.
Browse files Browse the repository at this point in the history
Allows tasks to have a prefix, while methods can be treated as normal
api.
  • Loading branch information
mshima committed May 12, 2021
1 parent 5f3b2fb commit 72f8dce
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/index.js
Expand Up @@ -27,7 +27,7 @@ const Base = mixins.reduce((a, b) => b(a), EventEmitter);

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

/**
Expand Down Expand Up @@ -123,6 +123,7 @@ class Generator extends Base {
* The Environment will ignore duplicated identifiers.
* @property {String} unique - uniqueBy calculation method (undefined/argument/namespace)
* @property {boolean} tasksMatchingPriority - Only queue methods that matches a priority.
* @property {String} taskPrefix - Tasks methods starts with prefix. Allows api methods (non tasks) without prefix.
* @property {boolean|Function} customInstallTask - Provides a custom install task. Environment >= 3.2.0
* Environment built-in task will not be executed
* @property {boolean|Function} customCommitTask - Provides a custom commit task. Environment >= 3.2.0
Expand Down Expand Up @@ -875,9 +876,11 @@ class Generator extends Base {
* @param {TaskOptions} [taskOptions]: options.
*/
queueOwnTask(name, taskOptions = {}) {
const {taskPrefix} = this.features;
const propertyName = taskPrefix ? `${taskPrefix}${name}` : name;
const property = Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(this),
name
propertyName
);
const item = property.value ? property.value : property.get.call(this);

Expand Down Expand Up @@ -914,6 +917,15 @@ class Generator extends Base {
getTaskNames() {
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(this));
let validMethods = methods.filter(methodIsValid);
const {taskPrefix} = this.features;

if (taskPrefix) {
validMethods = validMethods
.filter((method) => method.startsWith(taskPrefix))
.map((method) => method.slice(taskPrefix.length));
} else {
validMethods = validMethods.filter((method) => method.charAt(0) !== '#');
}

if (this.features.tasksMatchingPriority) {
const queueNames = Object.keys(this._queues);
Expand Down
33 changes: 33 additions & 0 deletions test/base.js
Expand Up @@ -550,6 +550,39 @@ describe('Base', () => {
});
});

describe('#run() with', () => {
beforeEach(function () {
this.TestGenerator = class extends Base {};
_.extend(this.TestGenerator.prototype, {
_private: sinon.spy(),
'#composed': sinon.spy(),
composed: sinon.spy(),
'#initializing': sinon.spy(),
initializing: sinon.spy()
});

this.testGen = new this.TestGenerator(
[],
{
resolved: 'generator-ember/all/index.js',
namespace: 'dummy',
env: this.env,
'skip-install': true
},
{taskPrefix: '#'}
);
});

it('should run hashtag prefixed method', async function () {
await this.testGen.run();
assert(this.TestGenerator.prototype['#composed'].calledOnce);
assert(this.TestGenerator.prototype.composed.notCalled);
assert(this.TestGenerator.prototype['#initializing'].calledOnce);
assert(this.TestGenerator.prototype.initializing.notCalled);
assert(this.TestGenerator.prototype._private.notCalled);
});
});

describe('#argument()', () => {
it('add a new argument to the generator instance', function () {
assert.equal(this.dummy._arguments.length, 0);
Expand Down

0 comments on commit 72f8dce

Please sign in to comment.