From 076d0669823356d56fe7270718065f266b7a5ecf Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Mon, 23 Jul 2018 16:40:55 +0100 Subject: [PATCH] WIP 6: Scoped prefixes --- README.md | 2 ++ lib/plugins.js | 7 ++++++- test/all.test.js | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f8adc59..b7f1129 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,8 @@ assert(app.plugins.express); The plugins are registered as `router` and `express`, but the npm modules which were required were called 'monkey-router' and 'monkey-express'. +Scoped prefixes also work. e.g. with prefix '@monkey', 'routes' will be loaded from '@monkey/routes'. + NB `PLUGIN_PREFIX` is currently a string, but it may be changed to a `Symbol` in a future version. This will not be considered a semver major change, so always use it via `Pluggi.PLUGIN_PREFIX`. ### Options diff --git a/lib/plugins.js b/lib/plugins.js index 9e6d0f3..4dde74f 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -37,6 +37,10 @@ module.exports = class Plugins extends Plugin { * @returns {PluginsPlugin} - plugin for chaining */ addPrefix(prefix) { + if ( + (prefix[0] == '@' && prefix.slice(-1) == '/') || // jshint ignore:line + prefix.slice(-1) == '-' + ) prefix = prefix.slice(0, -1); this.prefixes.push(prefix); return this; } @@ -76,7 +80,8 @@ module.exports = class Plugins extends Plugin { resolveModule(name) { // Attempt to resolve as module with prefixes for (let prefix of this.prefixes) { - const path = `${prefix}-${name}`; + const separator = (prefix[0] == '@' && !prefix.includes('/')) ? '/' : '-'; // jshint ignore:line + const path = `${prefix}${separator}${name}`; if (tryResolve(path)) return path; } diff --git a/test/all.test.js b/test/all.test.js index 6eaa254..1d877a6 100644 --- a/test/all.test.js +++ b/test/all.test.js @@ -111,6 +111,8 @@ describe('`.plugin()`', function() { localOptions: true }); }); + + // TODO Add tests for @-scoped prefix }); describe('with arguments (name)', function() {