1
0
Fork 0

wip(core): More tweaks to plugin discovery

This commit is contained in:
Joost De Cock 2022-09-28 16:47:45 +02:00
parent 3af542c592
commit 6ce5937fb9
2 changed files with 33 additions and 21 deletions

View file

@ -501,40 +501,48 @@ function getPluginName(plugin) {
* @return {Pattern} this - The Pattern instance * @return {Pattern} this - The Pattern instance
*/ */
Pattern.prototype.__addPartPlugins = function (part) { Pattern.prototype.__addPartPlugins = function (part) {
if (!part.plugins) return this
if (!this.config.plugins) this.config.plugins = {} if (!this.config.plugins) this.config.plugins = {}
const plugins = { ...this.config.plugins } const plugins = { ...this.config.plugins }
if (!part.plugins) return this
// Side-step immutability of the part object to ensure plugins is an array // Side-step immutability of the part object to ensure plugins is an array
let partPlugins = part.plugins let partPlugins = part.plugins
if (!Array.isArray(partPlugins)) partPlugins = [partPlugins] if (!Array.isArray(partPlugins)) partPlugins = [partPlugins]
for (const plugin of partPlugins) plugins[getPluginName(plugin)] = plugin // Go through list of part plugins
for (let plugin of partPlugins) { for (let plugin of partPlugins) {
const name = getPluginName(plugin) const name = getPluginName(plugin)
this.store.log.debug(
plugin.plugin
? `🔌 Resolved __${name}__ conditional plugin in \`${part.name}\``
: `🔌 Resolved __${name}__ plugin in \`${part.name}\``
)
// Handle [plugin, data] scenario // Handle [plugin, data] scenario
if (Array.isArray(plugin)) { if (Array.isArray(plugin)) {
const pluginObj = { ...plugin[0], data: plugin[1] } const pluginObj = { ...plugin[0], data: plugin[1] }
plugin = pluginObj plugin = pluginObj
} }
if (plugin.plugin) if (!plugins[name]) {
this.store.log.debug(`🔌 Resolved __${name}__ conditional plugin in \`${part.name}\``) // New plugin, so we load it
else this.store.log.debug(`🔌 Resolved __${name}__ plugin in \`${part.name}\``)
// Do not overwrite an existing plugin with a conditional plugin unless it is also conditional
if (plugin.plugin && plugin.condition) {
if (!plugins[name]) {
plugins[name] = plugin
this.store.log.info(`Plugin \`${name}\` was conditionally added.`)
} else if (plugins[name]?.condition) {
plugins[name + '_'] = plugin
this.store.log.info(
`Plugin \`${name}\` was conditionally added again. Renaming to ${name}_.`
)
} else
this.store.log.info(
`Plugin \`${name}\` was requested conditionally, but is already added explicitly. Not loading.`
)
} else {
plugins[name] = plugin plugins[name] = plugin
this.store.log.info(`Plugin \`${name}\` was added.`) this.store.log.info(
plugin.condition
? `New plugin conditionally added: \`${name}\``
: `New plugin added: \`${name}\``
)
} else {
// Existing plugin, takes some more work
if (plugin.plugin && plugin.condition) {
// Multiple instances of the same plugin with different conditions
// will all be added, so we need to change the name.
if (plugins[name]?.condition) {
plugins[name + '_'] = plugin
this.store.log.info(
`Plugin \`${name}\` was conditionally added again. Renaming to ${name}_.`
)
} else
this.store.log.info(
`Plugin \`${name}\` was requested conditionally, but is already added explicitly. Not loading.`
)
}
} }
} }

View file

@ -702,6 +702,9 @@ describe('Pattern', () => {
it('Should check whether created parts get the pattern context', () => { it('Should check whether created parts get the pattern context', () => {
let partContext let partContext
const plugin = {
name: 'example',
}
const part = { const part = {
name: 'test', name: 'test',
draft: ({ Point, paths, Path, part, context }) => { draft: ({ Point, paths, Path, part, context }) => {
@ -710,6 +713,7 @@ describe('Pattern', () => {
return part return part
}, },
plugins: [plugin],
} }
const Pattern = new Design({ parts: [part], data: { name: 'test', version: '1' } }) const Pattern = new Design({ parts: [part], data: { name: 'test', version: '1' } })
const pattern = new Pattern() const pattern = new Pattern()