@webiny/plugins 0.0.0-mt-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/Plugin.d.ts +6 -0
- package/Plugin.js +28 -0
- package/PluginsContainer.d.ts +14 -0
- package/PluginsContainer.js +113 -0
- package/README.md +74 -0
- package/index.d.ts +4 -0
- package/index.js +25 -0
- package/package.json +36 -0
- package/types.d.ts +8 -0
- package/types.js +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Webiny
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Plugin.d.ts
ADDED
package/Plugin.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.Plugin = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
class Plugin {
|
|
13
|
+
constructor() {
|
|
14
|
+
(0, _defineProperty2.default)(this, "name", void 0);
|
|
15
|
+
|
|
16
|
+
if (!this.constructor.type) {
|
|
17
|
+
throw Error(`Missing "type" definition in "${this.constructor.name}"!`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get type() {
|
|
22
|
+
return this.constructor.type;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exports.Plugin = Plugin;
|
|
28
|
+
(0, _defineProperty2.default)(Plugin, "type", void 0);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Plugin } from "./types";
|
|
2
|
+
export declare class PluginsContainer {
|
|
3
|
+
private plugins;
|
|
4
|
+
private _byTypeCache;
|
|
5
|
+
constructor(...args: any[]);
|
|
6
|
+
byName<T extends Plugin>(name: T["name"]): T;
|
|
7
|
+
byType<T extends Plugin>(type: T["type"]): T[];
|
|
8
|
+
atLeastOneByType<T extends Plugin>(type: T["type"]): T[];
|
|
9
|
+
oneByType<T extends Plugin>(type: T["type"]): T;
|
|
10
|
+
all<T extends Plugin>(): T[];
|
|
11
|
+
register(...args: any): void;
|
|
12
|
+
unregister(name: string): void;
|
|
13
|
+
private findByType;
|
|
14
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.PluginsContainer = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _uniqid = _interopRequireDefault(require("uniqid"));
|
|
13
|
+
|
|
14
|
+
const isOptionsObject = item => item && !Array.isArray(item) && !item.type && !item.name;
|
|
15
|
+
|
|
16
|
+
const normalizeArgs = args => {
|
|
17
|
+
let options = {}; // Check if last item in the plugins array is actually an options object.
|
|
18
|
+
|
|
19
|
+
if (isOptionsObject(args[args.length - 1])) {
|
|
20
|
+
[options] = args.splice(-1, 1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return [args, options];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const assign = (plugins, options, target) => {
|
|
27
|
+
for (let i = 0; i < plugins.length; i++) {
|
|
28
|
+
const plugin = plugins[i];
|
|
29
|
+
|
|
30
|
+
if (Array.isArray(plugin)) {
|
|
31
|
+
assign(plugin, options, target);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let name = plugin._name || plugin.name;
|
|
36
|
+
|
|
37
|
+
if (!name) {
|
|
38
|
+
plugin.name = name = (0, _uniqid.default)(plugin.type + "-");
|
|
39
|
+
} // If skip existing was set to true, and a plugin with the same name was already registered, skip registration.
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
if (!options.skipExisting || !target[name]) {
|
|
43
|
+
target[name] = plugin;
|
|
44
|
+
plugin.init && plugin.init();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
class PluginsContainer {
|
|
50
|
+
constructor(...args) {
|
|
51
|
+
(0, _defineProperty2.default)(this, "plugins", {});
|
|
52
|
+
(0, _defineProperty2.default)(this, "_byTypeCache", {});
|
|
53
|
+
this.register(...args);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
byName(name) {
|
|
57
|
+
return this.plugins[name];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
byType(type) {
|
|
61
|
+
if (this._byTypeCache[type]) {
|
|
62
|
+
return Array.from(this._byTypeCache[type]);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const plugins = this.findByType(type);
|
|
66
|
+
this._byTypeCache[type] = plugins;
|
|
67
|
+
return Array.from(plugins);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
atLeastOneByType(type) {
|
|
71
|
+
const list = this.byType(type);
|
|
72
|
+
|
|
73
|
+
if (list.length === 0) {
|
|
74
|
+
throw new Error(`There are no plugins by type "${type}".`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return list;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
oneByType(type) {
|
|
81
|
+
const list = this.atLeastOneByType(type);
|
|
82
|
+
|
|
83
|
+
if (list.length > 1) {
|
|
84
|
+
throw new Error(`There is a requirement for plugin of type "${type}" to be only one registered.`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return list[0];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
all() {
|
|
91
|
+
return Object.values(this.plugins);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
register(...args) {
|
|
95
|
+
// reset the cache when adding new plugins
|
|
96
|
+
this._byTypeCache = {};
|
|
97
|
+
const [plugins, options] = normalizeArgs(args);
|
|
98
|
+
assign(plugins, options, this.plugins);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
unregister(name) {
|
|
102
|
+
// reset the cache when removing a plugin
|
|
103
|
+
this._byTypeCache = {};
|
|
104
|
+
delete this.plugins[name];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
findByType(type) {
|
|
108
|
+
return Object.values(this.plugins).filter(pl => pl.type === type);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
exports.PluginsContainer = PluginsContainer;
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# @webiny/plugins
|
|
2
|
+
[](https://www.npmjs.com/package/@webiny/plugins)
|
|
3
|
+
[](https://www.npmjs.com/package/@webiny/plugins)
|
|
4
|
+
[](https://github.com/prettier/prettier)
|
|
5
|
+
[](http://makeapullrequest.com)
|
|
6
|
+
|
|
7
|
+
A simple registry that stores all plugins in a shared object.
|
|
8
|
+
The only requirement for a plugin is to have a `name` and a `type` properties.
|
|
9
|
+
The rest is entirely up to you.
|
|
10
|
+
|
|
11
|
+
There is nothing spectacular going on under the hood, just a simple
|
|
12
|
+
object for storing references and a few utility functions.
|
|
13
|
+
|
|
14
|
+
For more information, please visit [the official docs](https://docs.webiny.com/docs/developer-tutorials/plugins-crash-course).
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
```
|
|
18
|
+
npm install --save @webiny/plugins
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or if you prefer yarn:
|
|
22
|
+
```
|
|
23
|
+
yarn add @webiny/plugins
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Adding a plugin
|
|
29
|
+
```
|
|
30
|
+
import { plugins } from "@webiny/plugins";
|
|
31
|
+
|
|
32
|
+
// Add a plugin
|
|
33
|
+
plugins.register({
|
|
34
|
+
name: "my-plugin",
|
|
35
|
+
type: "say-hi",
|
|
36
|
+
salute: () => "Hi!"
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
plugins.register({
|
|
40
|
+
name: "my-second-plugin",
|
|
41
|
+
type: "say-hi",
|
|
42
|
+
salute: () => "Yo!"
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Getting plugins by type
|
|
47
|
+
```
|
|
48
|
+
// anywhere in your app
|
|
49
|
+
import { plugins } from "@webiny/plugins";
|
|
50
|
+
|
|
51
|
+
const pluginList = plugins.byType("say-hi");
|
|
52
|
+
pluginList.forEach(plugin => {
|
|
53
|
+
// Call "salute" function
|
|
54
|
+
plugin.salute();
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Getting a single plugin by name
|
|
59
|
+
```
|
|
60
|
+
// anywhere in your app
|
|
61
|
+
import { plugins } from "@webiny/plugins";
|
|
62
|
+
|
|
63
|
+
const plugin = plugins.byName("my-plugin");
|
|
64
|
+
// Call "salute" function
|
|
65
|
+
plugin.salute();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Removing a plugin
|
|
69
|
+
```
|
|
70
|
+
// anywhere in your app
|
|
71
|
+
import { plugins } from "@webiny/plugins";
|
|
72
|
+
|
|
73
|
+
plugins.unregister("my-plugin");
|
|
74
|
+
```
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "Plugin", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _Plugin.Plugin;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "PluginsContainer", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _PluginsContainer.PluginsContainer;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
exports.plugins = void 0;
|
|
19
|
+
|
|
20
|
+
var _PluginsContainer = require("./PluginsContainer");
|
|
21
|
+
|
|
22
|
+
var _Plugin = require("./Plugin");
|
|
23
|
+
|
|
24
|
+
const plugins = new _PluginsContainer.PluginsContainer();
|
|
25
|
+
exports.plugins = plugins;
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webiny/plugins",
|
|
3
|
+
"version": "0.0.0-mt-1",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/webiny/webiny-js.git"
|
|
8
|
+
},
|
|
9
|
+
"description": "A simple registry that stores all plugins in a shared object.",
|
|
10
|
+
"contributors": [
|
|
11
|
+
"Pavel Denisjuk <pavel@webiny.com>",
|
|
12
|
+
"Adrian Smijulj <adrian@webiny.com>"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@babel/runtime": "7.15.4",
|
|
17
|
+
"uniqid": "5.4.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@babel/cli": "^7.5.5",
|
|
21
|
+
"@babel/core": "^7.5.5",
|
|
22
|
+
"@webiny/cli": "^0.0.0-mt-1",
|
|
23
|
+
"@webiny/project-utils": "^0.0.0-mt-1",
|
|
24
|
+
"rimraf": "^3.0.2",
|
|
25
|
+
"typescript": "^4.1.3"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"directory": "dist"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "yarn webiny run build",
|
|
33
|
+
"watch": "yarn webiny run watch"
|
|
34
|
+
},
|
|
35
|
+
"gitHead": "37736d8456a6ecb342a6c3645060bd9a3f2d4bb0"
|
|
36
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { PluginsContainer } from "./PluginsContainer";
|
|
2
|
+
export declare type Plugin<T = Record<string, any>> = {
|
|
3
|
+
type: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
init?: () => void;
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
} & T;
|
|
8
|
+
export declare type PluginCollection = (Plugin | PluginCollection)[];
|
package/types.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "PluginsContainer", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _PluginsContainer.PluginsContainer;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
var _PluginsContainer = require("./PluginsContainer");
|