Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin.needs to assert dependencies #89

Merged
merged 4 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ function Api (plugins, defaultConfig) {
}

const name = plugin.name

if (plugin.needs) {
for (const needed of plugin.needs) {
const found = create.plugins.some((p) => p.name === needed)
if (!found) {
throw new Error(`plugin "${name ?? '?'}" needs plugin "${needed}" but not found`)
}
}
}

if (plugin.manifest) {
create.manifest = u.merge.manifest(
create.manifest,
Expand Down
33 changes: 33 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,39 @@ tape('plugin cannot be named global', function (t) {
t.end()
})

tape('plugin needs another plugin', function (t) {
// core, not a plugin.
var Create = Api([{
manifest: {},
init: function (api) {
return {}
}
}])

t.throws(() => {
Create.use({
name: 'x',
needs: ['y'],
init: function () { }
})
}, 'throws on missing plugin')

Create.use({
name: 'foo',
init: function () { }
})

t.doesNotThrow(() => {
Create.use({
name: 'bar',
needs: ['foo'],
init: function () { }
})
}, 'does not throw on existing plugin')

t.end()
})

tape('compound (array) plugins', function (t) {
// core, not a plugin.
var Create = Api([{
Expand Down
Loading