Skip to content

Commit 3dbc299

Browse files
committed
Pass pluginHelper into config functions
1 parent 6b3040f commit 3dbc299

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
var CoreObject = require('core-object');
22
var chalk = require('chalk');
3+
var merge = require('lodash.merge');
4+
5+
function _pluginHelperDefaults() {
6+
return {
7+
readConfigDefault: function(property) {
8+
var configuredValue = this.defaultConfig[property];
9+
if (typeof configuredValue === 'function') {
10+
return configuredValue.call(this, this.context);
11+
}
12+
return configuredValue;
13+
}.bind(this)
14+
};
15+
}
316

417
var DeployPluginBase = CoreObject.extend({
518
context: null,
@@ -43,7 +56,8 @@ var DeployPluginBase = CoreObject.extend({
4356
readConfig: function(property){
4457
var configuredValue = this.pluginConfig[property];
4558
if (typeof configuredValue === 'function') {
46-
return configuredValue.call(this.pluginConfig, this.context);
59+
var helper = merge(this.pluginHelper(this.context), _pluginHelperDefaults.call(this));
60+
return configuredValue.call(this.pluginConfig, this.context, helper);
4761
}
4862
return configuredValue;
4963
},
@@ -74,6 +88,10 @@ var DeployPluginBase = CoreObject.extend({
7488

7589
this.logRaw(chalkColor('- ' + message));
7690
}
91+
},
92+
93+
pluginHelper: function(/*context*/) {
94+
return {};
7795
}
7896
});
7997

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"dependencies": {
2525
"chalk": "^1.0.0",
26-
"core-object": "0.0.2"
26+
"core-object": "0.0.2",
27+
"lodash.merge": "^4.6.0"
2728
}
2829
}

tests/unit/index-nodetest.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,68 @@ describe('base plugin', function() {
7070
plugin.log('foo', {verbose: true});
7171
assert.deepEqual(verboseUi.messages, ['\u001b[34m| \u001b[39m']);
7272
});
73-
7473
});
7574

75+
describe('plugin helper', function() {
76+
it('provides access to the oringal default values', function() {
77+
var Plugin = Subject.extend({
78+
requiredConfig: ['bar'],
79+
defaultConfig: {
80+
foo: function(context) {
81+
return context.foo;
82+
}
83+
}
84+
});
85+
86+
var plugin = new Plugin({
87+
name: 'blah'
88+
});
89+
90+
var context = {
91+
foo: 'foo',
92+
config: {
93+
blah: {
94+
foo: function(context, pluginHelper) {
95+
return pluginHelper.readConfigDefault('foo') + 'foo';
96+
},
97+
bar: function(context, pluginHelper) {
98+
return pluginHelper.readConfigDefault('bar') + 'bar';
99+
}
100+
}
101+
}
102+
};
103+
104+
plugin.beforeHook(context);
105+
assert.equal(plugin.readConfig('foo'), 'foofoo');
106+
assert.equal(plugin.readConfig('bar'), 'undefinedbar');
107+
});
108+
109+
it('allows the implementer to add things to the pluginHelper', function() {
110+
var Plugin = Subject.extend({
111+
defaultConfig: { foo: 'foo' }
112+
});
113+
114+
var plugin = new Plugin({
115+
name: 'blah',
116+
117+
pluginHelper: function(context) {
118+
return { bar: context.foo };
119+
}
120+
});
121+
122+
var context = {
123+
foo: 'bar',
124+
config: {
125+
blah: {
126+
foo: function(context, pluginHelper) {
127+
return pluginHelper.readConfigDefault('foo') + pluginHelper.bar;
128+
}
129+
}
130+
}
131+
};
132+
133+
plugin.beforeHook(context);
134+
assert.equal(plugin.readConfig('foo'), 'foobar');
135+
});
136+
});
76137
});

0 commit comments

Comments
 (0)