Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/examples/happy/depend-on-dependencies-hub-bis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
depend: ["DependOnDependenciesHub"],

create: () => ({
dependOn: "hub",
}),
}
7 changes: 7 additions & 0 deletions src/examples/happy/depend-on-dependencies-hub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
depend: ["DependOnPlain"],

create: () => ({
dependOn: "hub",
}),
}
42 changes: 29 additions & 13 deletions src/pluginus.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import fs from "fs"
import path from "path"
import {
all,
pipe,
sortBy,
pick,
findWith,
reduce,
distinct,
remove,
Expand All @@ -18,7 +20,7 @@ import {
toLower,
is,
isEmpty,
has,
any,
} from "@asd14/m"

const capitalizeFirstLetter = string =>
Expand Down Expand Up @@ -168,6 +170,30 @@ const resolve = unresolvedPlugins => {
)(unresolvedPlugins)
}

// Sort based on dependency topology, using TSort
// https://en.wikipedia.org/wiki/Topological_sorting
const tSort = (names, input, start = [], depth = 0) => {
const processed = reduce((accum, item) => {
const plugin = findWith({ name: item }, {})(input)

const allDependenciesIncluded = pipe(
read("depend", []),
all(dependency => any(dependency, pick("name")(accum)))
)(plugin)

if (allDependenciesIncluded) {
accum.push(plugin)
}

return accum
}, start)(names)

const nextNames = names.filter(n => !processed.includes(n)),
goAgain = nextNames.length !== 0 && depth <= names.length

return goAgain ? tSort(nextNames, input, processed, depth + 1) : processed
}

export const pluginus = ({ source, nameFn = defaultNameFn }) =>
pipeP(
// Sanitize
Expand All @@ -179,17 +205,7 @@ export const pluginus = ({ source, nameFn = defaultNameFn }) =>
plugins => Promise.all(plugins),
prepare(nameFn),

// Sort based on dependency. Plugins without dependencies first
sortBy((a, b) => {
const aHasB = has(b.name, a.depend)
const bHasA = has(a.name, b.depend)

if (!aHasB && !bHasA) {
return a.depend.length > b.depend.length ? 1 : -1
}

return bHasA ? -1 : 1
}),
plugins => tSort(pick("name", plugins), plugins),

//
resolve
Expand Down
10 changes: 9 additions & 1 deletion src/pluginus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ test("Happy", async t => {

t.deepEquals(
Object.keys(plugins).sort(),
["DependOnPlain", "ExplicitName", "Object", "Plain", "PromisePlugin"],
[
"DependOnDependenciesHub",
"DependOnDependenciesHubBis",
"DependOnPlain",
"ExplicitName",
"Object",
"Plain",
"PromisePlugin",
],
'given [pluginus called with invalid values in "source" param] should [sanitize and load only valid]'
)

Expand Down