Skip to content

Commit 8a81ebb

Browse files
Merge pull request #15 from achambers/add-plugin-helper
Add plugin helper
2 parents 4d3c11a + d47e12a commit 8a81ebb

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

index.js

Lines changed: 14 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 cloneDeep = require('lodash.clonedeep');
4+
5+
function _pluginHelper() {
6+
return {
7+
readConfigDefault: function(property) {
8+
var configuredValue = this.defaultConfig[property];
9+
if (typeof configuredValue === 'function') {
10+
return cloneDeep(configuredValue.call(this, this.context));
11+
}
12+
return cloneDeep(configuredValue);
13+
}.bind(this)
14+
};
15+
}
316

417
var DeployPluginBase = CoreObject.extend({
518
context: null,
@@ -43,7 +56,7 @@ var DeployPluginBase = CoreObject.extend({
4356
readConfig: function(property){
4457
var configuredValue = this.pluginConfig[property];
4558
if (typeof configuredValue === 'function') {
46-
return configuredValue.call(this, this.context);
59+
return configuredValue.call(this.pluginConfig, this.context, _pluginHelper.call(this));
4760
}
4861
return configuredValue;
4962
},

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.clonedeep": "^4.5.0"
2728
}
2829
}

tests/unit/index-nodetest.js

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,109 @@ 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+
defaultConfig: {
79+
distFiles: ['index.html', 'assets/logo.png'],
80+
jsonBlueprint: {
81+
link: {
82+
selector: 'link',
83+
attributes: ['rel', 'href']
84+
},
85+
},
86+
git: {
87+
sha: function() {
88+
return '1';
89+
}
90+
},
91+
revisionKey: function(context) {
92+
return context.revisionData.revisionKey;
93+
}
94+
}
95+
});
96+
97+
var plugin = new Plugin({
98+
name: 'build'
99+
});
100+
101+
var context = {
102+
revisionData: { revisionKey: '111' },
103+
config: {
104+
build: {
105+
distFiles: function(context, pluginHelper) {
106+
var arr = pluginHelper.readConfigDefault('distFiles');
107+
arr.push('index.json');
108+
return arr;
109+
},
110+
jsonBlueprint: function(context, pluginHelper) {
111+
var blueprint = pluginHelper.readConfigDefault('jsonBlueprint');
112+
blueprint.link.attributes.push('integrity');
113+
114+
return blueprint;
115+
},
116+
sha: function(context, pluginHelper) {
117+
var git = pluginHelper.readConfigDefault('git');
118+
119+
return git.sha() + '2';
120+
},
121+
revisionKey: function(context, pluginHelper) {
122+
return pluginHelper.readConfigDefault('revisionKey') + '222';
123+
}
124+
}
125+
}
126+
};
127+
128+
plugin.beforeHook(context);
129+
assert.deepEqual(plugin.readConfig('distFiles'), ['index.html', 'assets/logo.png', 'index.json']);
130+
assert.deepEqual(plugin.readConfig('jsonBlueprint').link.attributes, ['rel', 'href', 'integrity']);
131+
assert.equal(plugin.readConfig('sha'), '12');
132+
assert.equal(plugin.readConfig('revisionKey'), '111222');
133+
});
134+
135+
it('ensures default values do not get mutated', function() {
136+
var Plugin = Subject.extend({
137+
defaultConfig: {
138+
distFiles: ['index.html', 'assets/logo.png'],
139+
jsonBlueprint: {
140+
link: {
141+
selector: 'link',
142+
attributes: ['rel', 'href']
143+
}
144+
}
145+
}
146+
});
147+
148+
var plugin = new Plugin({
149+
name: 'build'
150+
});
151+
152+
var context = {
153+
config: {
154+
build: {
155+
distFiles: function(context, pluginHelper) {
156+
var arr = pluginHelper.readConfigDefault('distFiles');
157+
arr.push('index.json');
158+
return arr;
159+
},
160+
jsonBlueprint: function(context, pluginHelper) {
161+
var blueprint = pluginHelper.readConfigDefault('jsonBlueprint');
162+
blueprint.link.attributes.push('integrity');
163+
164+
return blueprint;
165+
}
166+
}
167+
}
168+
};
169+
170+
plugin.beforeHook(context);
171+
plugin.readConfig('distFiles')
172+
plugin.readConfig('jsonBlueprint')
173+
174+
assert.deepEqual(plugin.defaultConfig.distFiles, ['index.html', 'assets/logo.png']);
175+
assert.deepEqual(plugin.defaultConfig.jsonBlueprint.link.attributes, ['rel', 'href']);
176+
})
177+
});
76178
});

0 commit comments

Comments
 (0)