plug-and-play
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [2.4.0](https://github.com/adaltas/node-plug-and-play/compare/v2.3.0...v2.4.0) (2022-02-16) | ||
### Features | ||
* esm support ([f74caa6](https://github.com/adaltas/node-plug-and-play/commit/f74caa66390df024eb2d3277ae278db16f81c2c8)) | ||
## [2.3.0](https://github.com/adaltas/node-plug-and-play/compare/v2.2.0...v2.3.0) (2021-03-23) | ||
@@ -7,0 +14,0 @@ |
@@ -1,9 +0,6 @@ | ||
// Generated by CoffeeScript 2.5.1 | ||
var PlugableError; | ||
PlugableError = class PlugableError extends Error { | ||
const PlugableError = class PlugableError extends Error { | ||
constructor(code, message, ...contexts) { | ||
var context, i, key, len, value; | ||
if (Array.isArray(message)) { | ||
message = message.filter(function(line) { | ||
message = message.filter(function (line) { | ||
return !!line; | ||
@@ -32,7 +29,5 @@ }).join(' '); | ||
} | ||
}; | ||
module.exports = function() { | ||
export default (function () { | ||
return new PlugableError(...arguments); | ||
}; | ||
}); |
252
lib/index.js
@@ -1,13 +0,8 @@ | ||
// Generated by CoffeeScript 2.5.1 | ||
var array_flatten, error, errors, is_object, is_object_literal, merge, normalize_hook, toposort; | ||
({is_object_literal, is_object, merge} = require('mixme')); | ||
import {is_object_literal, is_object, merge} from 'mixme'; | ||
import toposort from 'toposort'; | ||
import error from './error.js'; | ||
import {array_flatten} from './utils.js'; | ||
toposort = require('toposort'); | ||
error = require('./error'); | ||
({array_flatten} = require('./utils')); | ||
normalize_hook = function(name, hook) { | ||
const normalize_hook = function(name, hook) { | ||
if (!Array.isArray(hook)) { | ||
@@ -26,7 +21,5 @@ hook = [hook]; | ||
if (typeof hook.after === 'string') { | ||
// hook.after ?= [] | ||
hook.after = [hook.after]; | ||
} | ||
if (typeof hook.before === 'string') { | ||
// hook.before ?= [] | ||
hook.before = [hook.before]; | ||
@@ -38,11 +31,18 @@ } | ||
module.exports = function({args, chain, parent, plugins = []} = {}) { | ||
var i, len, obj, plugin, store; | ||
const errors = { | ||
PLUGINS_HOOK_AFTER_INVALID: function({name, plugin, after}) { | ||
throw error('PLUGINS_HOOK_AFTER_INVALID', [`the hook ${JSON.stringify(name)}`, plugin ? `in plugin ${JSON.stringify(plugin)}` : void 0, 'references an after dependency', `in plugin ${JSON.stringify(after)} which does not exists`]); | ||
}, | ||
PLUGINS_HOOK_BEFORE_INVALID: function({name, plugin, before}) { | ||
throw error('PLUGINS_HOOK_BEFORE_INVALID', [`the hook ${JSON.stringify(name)}`, plugin ? `in plugin ${JSON.stringify(plugin)}` : void 0, 'references a before dependency', `in plugin ${JSON.stringify(before)} which does not exists`]); | ||
} | ||
}; | ||
const plugandplay = function({args, chain, parent, plugins = []} = {}) { | ||
// Internal plugin store | ||
store = []; | ||
const store = []; | ||
// Public API definition | ||
obj = { | ||
const registry = { | ||
// Register new plugins | ||
register: function(plugin) { | ||
var hook, name, ref; | ||
if (typeof plugin === 'function') { | ||
@@ -57,6 +57,4 @@ plugin = plugin.apply(null, args); | ||
} | ||
ref = plugin.hooks; | ||
for (name in ref) { | ||
hook = ref[name]; | ||
plugin.hooks[name] = normalize_hook(name, hook); | ||
for (let name in plugin.hooks) { | ||
plugin.hooks[name] = normalize_hook(name, plugin.hooks[name]); | ||
} | ||
@@ -67,35 +65,15 @@ store.push(plugin); | ||
get: function({name, hooks = [], sort = true}) { | ||
var after, before, edges, edges_after, edges_before, hook, i, index, len, plugin; | ||
hooks = [ | ||
...normalize_hook(name, | ||
hooks), | ||
...array_flatten((function() { | ||
var i, | ||
len, | ||
results; | ||
results = []; | ||
for (i = 0, len = store.length; i < len; i++) { | ||
plugin = store[i]; | ||
if (!plugin.hooks[name]) { | ||
continue; | ||
} | ||
results.push((function() { | ||
var j, | ||
len1, | ||
ref, | ||
results1; | ||
ref = plugin.hooks[name]; | ||
results1 = []; | ||
for (j = 0, len1 = ref.length; j < len1; j++) { | ||
hook = ref[j]; | ||
results1.push(merge({ | ||
plugin: plugin.name | ||
}, | ||
hook)); | ||
} | ||
return results1; | ||
})()); | ||
} | ||
return results; | ||
})()), | ||
...normalize_hook(name, hooks), | ||
...array_flatten( | ||
store.map(function(plugin){ | ||
if(!plugin.hooks[name]) return; | ||
return plugin.hooks[name].map(function(hook){ | ||
return merge({ | ||
plugin: plugin.name | ||
}, hook); | ||
}); | ||
}) | ||
.filter(function(hook){return hook !== undefined;}) | ||
), | ||
...(parent ? parent.get({ | ||
@@ -110,66 +88,36 @@ name: name, | ||
// Topological sort | ||
index = {}; | ||
for (i = 0, len = hooks.length; i < len; i++) { | ||
hook = hooks[i]; | ||
const index = {}; | ||
for(const hook of hooks){ | ||
index[hook.plugin] = hook; | ||
} | ||
edges_after = (function() { | ||
var j, len1, results; | ||
results = []; | ||
for (j = 0, len1 = hooks.length; j < len1; j++) { | ||
hook = hooks[j]; | ||
if (!hook.after) { | ||
continue; | ||
} | ||
results.push((function() { | ||
var k, len2, ref, results1; | ||
ref = hook.after; | ||
results1 = []; | ||
for (k = 0, len2 = ref.length; k < len2; k++) { | ||
after = ref[k]; | ||
// This check assume the plugin has the same hooks which is not always the case | ||
if (!index[after]) { | ||
throw errors.PLUGINS_HOOK_AFTER_INVALID({ | ||
name: name, | ||
plugin: hook.plugin, | ||
after: after | ||
}); | ||
} | ||
results1.push([index[after], hook]); | ||
const edges_after = hooks | ||
.map(function(hook){ | ||
if(!hook.after) return; | ||
return hook.after.map(function(after){ | ||
// This check assume the plugin has the same hooks which is not always the case | ||
if(!index[after]){ | ||
throw errors.PLUGINS_HOOK_AFTER_INVALID({ | ||
name: name, | ||
plugin: hook.plugin, | ||
after: after | ||
}); | ||
} | ||
return results1; | ||
})()); | ||
} | ||
return results; | ||
})(); | ||
edges_before = (function() { | ||
var j, len1, results; | ||
results = []; | ||
for (j = 0, len1 = hooks.length; j < len1; j++) { | ||
hook = hooks[j]; | ||
if (!hook.before) { | ||
continue; | ||
} | ||
results.push((function() { | ||
var k, len2, ref, results1; | ||
ref = hook.before; | ||
results1 = []; | ||
for (k = 0, len2 = ref.length; k < len2; k++) { | ||
before = ref[k]; | ||
if (!index[before]) { | ||
throw errors.PLUGINS_HOOK_BEFORE_INVALID({ | ||
name: name, | ||
plugin: hook.plugin, | ||
before: before | ||
}); | ||
} | ||
results1.push([hook, index[before]]); | ||
return [index[after], hook]; | ||
}); | ||
}).filter(function(hook){return hook !== undefined;}); | ||
const edges_before = hooks | ||
.map(function(hook){ | ||
if(!hook.before) return; | ||
return hook.before.map(function(before){ | ||
if(!index[before]){ | ||
throw errors.PLUGINS_HOOK_BEFORE_INVALID({ | ||
name: name, | ||
plugin: hook.plugin, | ||
before: before | ||
}); | ||
} | ||
return results1; | ||
})()); | ||
} | ||
return results; | ||
})(); | ||
edges = [...edges_after, ...edges_before]; | ||
edges = array_flatten(edges, 0); | ||
return [hook, index[before]]; | ||
}); | ||
}).filter(function(hook){return hook !== undefined;}); | ||
const edges = array_flatten([...edges_after, ...edges_before], 0); | ||
return toposort.array(hooks, edges); | ||
@@ -179,3 +127,2 @@ }, | ||
call: async function({args = [], handler, hooks = [], name}) { | ||
var hook, i, len; | ||
if (arguments.length !== 1) { | ||
@@ -193,18 +140,17 @@ throw error('PLUGINS_INVALID_ARGUMENTS_NUMBER', ['function `call` expect 1 object argument,', `got ${arguments.length} arguments.`]); | ||
}); | ||
// Call the hooks | ||
for (i = 0, len = hooks.length; i < len; i++) { | ||
hook = hooks[i]; | ||
// Call the hooks | ||
for(const hook of hooks){ | ||
switch (hook.handler.length) { | ||
case 0: | ||
case 1: | ||
await hook.handler.call(this, args); | ||
break; | ||
case 2: | ||
handler = (await hook.handler.call(this, args, handler)); | ||
if (handler === null) { | ||
return null; | ||
} | ||
break; | ||
default: | ||
throw error('PLUGINS_INVALID_HOOK_HANDLER', ['hook handlers must have 0 to 2 arguments', `got ${hook.handler.length}`]); | ||
case 0: | ||
case 1: | ||
await hook.handler.call(this, args); | ||
break; | ||
case 2: | ||
handler = (await hook.handler.call(this, args, handler)); | ||
if (handler === null) { | ||
return null; | ||
} | ||
break; | ||
default: | ||
throw error('PLUGINS_INVALID_HOOK_HANDLER', ['hook handlers must have 0 to 2 arguments', `got ${hook.handler.length}`]); | ||
} | ||
@@ -219,3 +165,2 @@ } | ||
call_sync: function({args = [], handler, hooks = [], name}) { | ||
var hook, i, len; | ||
if (arguments.length !== 1) { | ||
@@ -233,18 +178,17 @@ throw error('PLUGINS_INVALID_ARGUMENTS_NUMBER', ['function `call` expect 1 object argument,', `got ${arguments.length} arguments.`]); | ||
}); | ||
// Call the hooks | ||
for (i = 0, len = hooks.length; i < len; i++) { | ||
hook = hooks[i]; | ||
// Call the hooks | ||
for(const hook of hooks) { | ||
switch (hook.handler.length) { | ||
case 0: | ||
case 1: | ||
hook.handler.call(this, args); | ||
break; | ||
case 2: | ||
handler = hook.handler.call(this, args, handler); | ||
if (handler === null) { | ||
return null; | ||
} | ||
break; | ||
default: | ||
throw error('PLUGINS_INVALID_HOOK_HANDLER', ['hook handlers must have 0 to 2 arguments', `got ${hook.handler.length}`]); | ||
case 0: | ||
case 1: | ||
hook.handler.call(this, args); | ||
break; | ||
case 2: | ||
handler = hook.handler.call(this, args, handler); | ||
if (handler === null) { | ||
return null; | ||
} | ||
break; | ||
default: | ||
throw error('PLUGINS_INVALID_HOOK_HANDLER', ['hook handlers must have 0 to 2 arguments', `got ${hook.handler.length}`]); | ||
} | ||
@@ -258,18 +202,10 @@ } | ||
}; | ||
// Register initial plugins | ||
for (i = 0, len = plugins.length; i < len; i++) { | ||
plugin = plugins[i]; | ||
obj.register(plugin); | ||
// Register initial plugins | ||
for(const plugin of plugins){ | ||
registry.register(plugin); | ||
} | ||
// return the object | ||
return obj; | ||
return registry; | ||
}; | ||
errors = { | ||
PLUGINS_HOOK_AFTER_INVALID: function({name, plugin, after}) { | ||
throw error('PLUGINS_HOOK_AFTER_INVALID', [`the hook ${JSON.stringify(name)}`, plugin ? `in plugin ${JSON.stringify(plugin)}` : void 0, 'references an after dependency', `in plugin ${JSON.stringify(after)} which does not exists`]); | ||
}, | ||
PLUGINS_HOOK_BEFORE_INVALID: function({name, plugin, before}) { | ||
throw error('PLUGINS_HOOK_BEFORE_INVALID', [`the hook ${JSON.stringify(name)}`, plugin ? `in plugin ${JSON.stringify(plugin)}` : void 0, 'references a before dependency', `in plugin ${JSON.stringify(before)} which does not exists`]); | ||
} | ||
}; | ||
export {plugandplay}; |
@@ -1,19 +0,20 @@ | ||
// Generated by CoffeeScript 2.5.1 | ||
module.exports = { | ||
array_flatten: function(arr, depth = -1) { | ||
var i, j, ref, ret; | ||
ret = []; | ||
for (i = j = 0, ref = arr.length; (0 <= ref ? j < ref : j > ref); i = 0 <= ref ? ++j : --j) { | ||
if (Array.isArray(arr[i])) { | ||
if (depth === 0) { | ||
ret.push(...arr[i]); | ||
} else { | ||
ret.push(...module.exports.array_flatten(arr[i], depth - 1)); | ||
} | ||
} else { | ||
ret.push(arr[i]); | ||
export const array_flatten = function (items, depth = -1) { | ||
const result = []; | ||
for (const item of items) { | ||
if (Array.isArray(item)) { | ||
if (depth === 0) { | ||
result.push(...item); | ||
} | ||
else { | ||
result.push(...array_flatten(item, depth - 1)); | ||
} | ||
} | ||
return ret; | ||
else { | ||
result.push(item); | ||
} | ||
} | ||
return result; | ||
}; | ||
export default { | ||
array_flatten | ||
}; |
{ | ||
"name": "plug-and-play", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"author": "David Worms <david@adaltas.com>", | ||
@@ -28,3 +28,3 @@ "bugs": { | ||
"dependencies": { | ||
"mixme": "^0.3.5", | ||
"mixme": "^0.5.4", | ||
"toposort": "^2.0.2" | ||
@@ -34,13 +34,23 @@ }, | ||
"devDependencies": { | ||
"@commitlint/cli": "^11.0.0", | ||
"@commitlint/config-conventional": "^11.0.0", | ||
"coffeescript": "^2.5.1", | ||
"eslint": "^7.6.0", | ||
"eslint-plugin-coffee": "^0.1.12", | ||
"husky": "^5.0.7", | ||
"mocha": "8.1.1", | ||
"pinst": "^2.1.2", | ||
"@commitlint/cli": "^16.2.1", | ||
"@commitlint/config-conventional": "^16.2.1", | ||
"@rollup/plugin-eslint": "^8.0.1", | ||
"@rollup/plugin-node-resolve": "^13.1.3", | ||
"coffeescript": "^2.6.1", | ||
"eslint": "^8.9.0", | ||
"eslint-plugin-coffee": "^0.1.15", | ||
"eslint-plugin-mocha": "^10.0.3", | ||
"husky": "^7.0.4", | ||
"mocha": "9.2.0", | ||
"pinst": "^2.1.6", | ||
"rollup": "^2.67.2", | ||
"should": "~13.2.3", | ||
"standard-version": "^9.1.0" | ||
"standard-version": "^9.3.2" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/esm/index.js", | ||
"require": "./dist/cjs/index.cjs" | ||
} | ||
}, | ||
"homepage": "https://github.com/adaltas/node-plug-and-play#readme", | ||
@@ -60,11 +70,11 @@ "keywords": [ | ||
"mocha": { | ||
"throw-deprecation": true, | ||
"inline-diffs": true, | ||
"loader": "./test/loaders/coffee.js", | ||
"recursive": true, | ||
"reporter": "spec", | ||
"require": [ | ||
"should", | ||
"coffeescript/register" | ||
"should" | ||
], | ||
"inline-diffs": true, | ||
"timeout": 40000, | ||
"reporter": "spec", | ||
"recursive": true | ||
"throw-deprecation": true, | ||
"timeout": 40000 | ||
}, | ||
@@ -76,9 +86,8 @@ "repository": { | ||
"scripts": { | ||
"build": "npx rollup -c", | ||
"_postinstall": "husky install", | ||
"prepublishOnly": "pinst --disable", | ||
"postpublish": "pinst --enable", | ||
"build": "coffee -b -o lib src", | ||
"lint": "eslint 'src/**/*.coffee'", | ||
"lint:fix": "eslint --fix 'src/**/*.coffee'", | ||
"pretest": "npm run build", | ||
"lint": "eslint 'lib/**/*.js' && eslint -c .eslintrc.test.json 'test/**/*.coffee'", | ||
"lint:fix": "eslint --fix 'lib/**/*.js' && eslint --fix -c .eslintrc.test.json 'test/**/*.coffee'", | ||
"test": "mocha 'test/**/*.coffee'", | ||
@@ -89,3 +98,4 @@ "release": "standard-version", | ||
"release:major": "standard-version --release-as major" | ||
} | ||
}, | ||
"type": "module" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62228
33
840
Yes
14
1
+ Addedmixme@0.5.10(transitive)
- Removedmixme@0.3.5(transitive)
Updatedmixme@^0.5.4