Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to provide a default value for plugin options #3808

Merged
merged 2 commits into from Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/classes/PluginManager.js
Expand Up @@ -241,6 +241,7 @@ class PluginManager {
const command = this.getCommand(commandsArray, allowEntryPoints);

this.convertShortcutsIntoOptions(command);
this.assignDefaultOptions(command);
this.validateOptions(command);

const events = this.getEvents(command);
Expand Down Expand Up @@ -352,6 +353,13 @@ class PluginManager {
});
}

assignDefaultOptions(command) {
_.forEach(command.options, (value, key) => {
if (value.default && (!this.cliOptions[key] || this.cliOptions[key] === true)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be boolean-lost here, but why do we need this.cliOptions[key] === true condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol #guilty 😂 ... @pmuens would be great if you could shed some light here ☺️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@rpunkfu rpunkfu Jul 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


;pp

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these_accusations === false && these_accusations !== true

I have no idea how it got there :D I did a lot of cut pasting when I did that refactor.....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂 thanks for the response @dougmoscrop.

So I would say that we can get rid of the double check since they really both check the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 Thanks @dougmoscrop --> ⛹️

So as the author of this weird piece of code I'd say that we can drop 😂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this is going further than I thought 😄 ... it needs further investigation and testing 😊

this.cliOptions[key] = value.default;
}
});
}
}

module.exports = PluginManager;
39 changes: 39 additions & 0 deletions lib/classes/PluginManager.test.js
Expand Up @@ -888,6 +888,45 @@ describe('PluginManager', () => {
});
});

describe('#assignDefaultOptions()', () => {
it('should assign default values to empty options', () => {
pluginManager.commands = {
foo: {
options: {
bar: {
required: true,
default: 'foo',
},
},
},
};

const foo = pluginManager.commands.foo;
pluginManager.assignDefaultOptions(foo);

expect(pluginManager.cliOptions.bar).to.equal(foo.options.bar.default);
});

it('should not assign default values to non-empty options', () => {
pluginManager.commands = {
foo: {
options: {
bar: {
required: true,
default: 'foo',
},
},
},
};

const foo = pluginManager.commands.foo;
pluginManager.setCliOptions({ bar: 100 });
pluginManager.assignDefaultOptions(foo);

expect(pluginManager.cliOptions.bar).to.equal(100);
});
});

describe('#validateOptions()', () => {
it('should throw an error if a required option is not set', () => {
pluginManager.commands = {
Expand Down