Skip to content

Commit

Permalink
Add support to tasksMatchingPriority feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Mar 13, 2021
1 parent ee061ab commit 93e01ea
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 13 deletions.
32 changes: 30 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ class Generator extends Base {
* @property {Storage} config - `.yo-rc` config file manager
* @property {Object} fs - An instance of {@link https://github.com/SBoudrias/mem-fs-editor Mem-fs-editor}
* @property {Function} log - Output content through Interface Adapter
* @param {Object} features - Provide Generator features information
* @property {String} uniqueBy - The Generator instance unique identifier.
* 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 {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
* Environment built-in task will not be executed
*
* @example
* const Generator = require('yeoman-generator');
Expand Down Expand Up @@ -885,6 +894,26 @@ class Generator extends Base {
this.queueTaskGroup(item, taskOptions);
}

/**
* @private
* Get task names.
*
* @return {string[]}
*/
getTaskNames() {
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(this));
let validMethods = methods.filter(methodIsValid);

if (this.features.tasksMatchingPriority) {
const queueNames = Object.keys(this._queues);
validMethods = validMethods.filter((method) =>
queueNames.includes(method)
);
}

return validMethods;
}

/**
* @private
* Schedule every generator's methods on a run queue.
Expand All @@ -895,8 +924,7 @@ class Generator extends Base {
this._running = true;
this._taskStatus = {cancelled: false, timestamp: new Date()};

const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(this));
const validMethods = methods.filter(methodIsValid);
const validMethods = this.getTaskNames();
if (validMethods.length === 0 && this._prompts.length === 0) {
throw new Error(
'This Generator is empty. Add at least one method for it to run.'
Expand Down
87 changes: 76 additions & 11 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1788,26 +1788,91 @@ describe('Base', () => {

describe('#getFeatures', () => {
it('should return namespace as uniqueBy when unique is true', function () {
const gen = new Base([], {unique: true, namespace: 'foo', env: this.env});
const gen = new Base(
[],
{namespace: 'foo', env: this.env},
{unique: true}
);
assert.equal(gen.getFeatures().uniqueBy, 'foo');
});

it("should return namespace as uniqueBy when unique is 'namespace'", function () {
const gen = new Base([], {
unique: 'namespace',
namespace: 'foo',
env: this.env
});
const gen = new Base(
[],
{
namespace: 'foo',
env: this.env
},
{
unique: 'namespace'
}
);
assert.equal(gen.getFeatures().uniqueBy, 'foo');
});

it("should return namespace with first argument as uniqueBy when unique is 'namespace'", function () {
const gen = new Base(['bar'], {
unique: 'argument',
namespace: 'foo',
env: this.env
});
const gen = new Base(
['bar'],
{
namespace: 'foo',
env: this.env
},
{
unique: 'argument'
}
);
assert.equal(gen.getFeatures().uniqueBy, 'foo#bar');
});
});

describe('getTaskNames', () => {
class TestGen extends Base {
constructor(args, options, features) {
super(
args,
{
...options,
customPriorities: [
{
name: 'customPriority',
before: 'prompting'
}
]
},
features
);
}

anyMethod() {}
default() {}
customPriority() {}
}
it('should return any public member when tasksMatchingPriority is undefined', function () {
const gen = new TestGen(
[],
{
namespace: 'foo',
env: this.env
},
{}
);
assert.deepStrictEqual(gen.getTaskNames(), [
'anyMethod',
'default',
'customPriority'
]);
});

it('should return any public member when tasksMatchingPriority is true', function () {
const gen = new TestGen(
[],
{
namespace: 'foo',
env: this.env
},
{tasksMatchingPriority: true}
);
assert.deepStrictEqual(gen.getTaskNames(), ['default', 'customPriority']);
});
});
});

0 comments on commit 93e01ea

Please sign in to comment.