Skip to content

Commit a575aae

Browse files
committed
Merge pull request #15 from sunesimonsen/feature/supportPluginVersionField
use: Support a plugin version field.
2 parents 7b63195 + 9159ab4 commit a575aae

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

lib/MagicPen.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,18 @@ MagicPen.prototype.use = function (plugin) {
472472
});
473473

474474
if (existingPlugin) {
475-
if (existingPlugin === plugin) {
475+
if (existingPlugin === plugin || (typeof plugin.version !== 'undefined' && plugin.version === existingPlugin.version)) {
476476
// No-op
477477
return this;
478478
} else {
479-
throw new Error("Another instance of the plugin '" + plugin.name + "' is already installed. " +
480-
"Please check your node_modules folder for unmet peerDependencies.");
479+
throw new Error("Another instance of the plugin '" + plugin.name + "' " +
480+
"is already installed" +
481+
(typeof existingPlugin.version !== 'undefined' ?
482+
' (version ' + existingPlugin.version +
483+
(typeof plugin.version !== 'undefined' ?
484+
', trying to install ' + plugin.version : '') +
485+
')' : '') +
486+
". Please check your node_modules folder for unmet peerDependencies.");
481487
}
482488
}
483489

@@ -487,6 +493,7 @@ MagicPen.prototype.use = function (plugin) {
487493
throw new Error('Plugins must be functions or adhere to the following interface\n' +
488494
'{\n' +
489495
' name: <an optional plugin name>,\n' +
496+
' version: <an optional semver version string>,\n' +
490497
' dependencies: <an optional list of dependencies>,\n' +
491498
' installInto: <a function that will update the given magicpen instance>\n' +
492499
'}');

test/magicpen.spec.js

+52
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ describe('magicpen', function () {
102102
}, 'to throw', 'Plugins must be functions or adhere to the following interface\n' +
103103
'{\n' +
104104
' name: <an optional plugin name>,\n' +
105+
' version: <an optional semver version string>,\n' +
105106
' dependencies: <an optional list of dependencies>,\n' +
106107
' installInto: <a function that will update the given magicpen instance>\n' +
107108
'}');
@@ -214,6 +215,57 @@ describe('magicpen', function () {
214215
pen.use(plugin);
215216
expect(callCount, 'to be', 1);
216217
});
218+
219+
it('installing two different plugins that are identically named and have the same version (but not ===) will only install the first one', function () {
220+
var callCount1 = 0;
221+
var plugin1 = {
222+
name: 'plugin',
223+
version: '1.2.3',
224+
installInto: function () {
225+
callCount1 += 1;
226+
}
227+
};
228+
var callCount2 = 0;
229+
var plugin2 = {
230+
name: 'plugin',
231+
version: '1.2.3',
232+
installInto: function () {
233+
callCount2 += 1;
234+
}
235+
};
236+
pen.use(plugin1).use(plugin2);
237+
expect(callCount1, 'to be', 1);
238+
expect(callCount2, 'to be', 0);
239+
});
240+
241+
it('should throw an error when installing two different plugins that are identically named and have different versions', function () {
242+
pen.use({
243+
name: 'plugin',
244+
version: '1.2.3',
245+
installInto: function () {}
246+
});
247+
expect(function () {
248+
pen.use({
249+
name: 'plugin',
250+
version: '1.5.6',
251+
installInto: function () {}
252+
});
253+
}, 'to throw', "Another instance of the plugin 'plugin' is already installed (version 1.2.3, trying to install 1.5.6). Please check your node_modules folder for unmet peerDependencies.");
254+
});
255+
256+
it('should throw an error when two identically named plugins where the first one has a version number', function () {
257+
pen.use({
258+
name: 'plugin',
259+
version: '1.2.3',
260+
installInto: function () {}
261+
});
262+
expect(function () {
263+
pen.use({
264+
name: 'plugin',
265+
installInto: function () {}
266+
});
267+
}, 'to throw', "Another instance of the plugin 'plugin' is already installed (version 1.2.3). Please check your node_modules folder for unmet peerDependencies.");
268+
});
217269
});
218270

219271
describe('block', function () {

0 commit comments

Comments
 (0)