Skip to content

Commit

Permalink
Allow to provide a default value for plugin options
Browse files Browse the repository at this point in the history
  • Loading branch information
rpunkfu committed Jun 18, 2017
1 parent 6a9e996 commit da9d7c2
Show file tree
Hide file tree
Showing 3 changed files with 4,049 additions and 0 deletions.
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)) {
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

0 comments on commit da9d7c2

Please sign in to comment.