@webiny/plugins 0.0.0-mt-3 → 0.0.0-unstable.1145e7667f

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/Plugin.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export declare abstract class Plugin {
2
2
  static readonly type: string;
3
- name: string;
3
+ name?: string;
4
4
  constructor();
5
5
  get type(): string;
6
6
  }
package/Plugin.js CHANGED
@@ -1,28 +1,21 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.Plugin = void 0;
9
-
10
8
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
9
  class Plugin {
13
10
  constructor() {
14
11
  (0, _defineProperty2.default)(this, "name", void 0);
15
-
16
12
  if (!this.constructor.type) {
17
13
  throw Error(`Missing "type" definition in "${this.constructor.name}"!`);
18
14
  }
19
15
  }
20
-
21
16
  get type() {
22
17
  return this.constructor.type;
23
18
  }
24
-
25
19
  }
26
-
27
20
  exports.Plugin = Plugin;
28
21
  (0, _defineProperty2.default)(Plugin, "type", void 0);
package/Plugin.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Plugin","constructor","type","Error","name"],"sources":["Plugin.ts"],"sourcesContent":["export abstract class Plugin {\n public static readonly type: string;\n public name?: string;\n\n constructor() {\n if (!(this.constructor as typeof Plugin).type) {\n throw Error(`Missing \"type\" definition in \"${this.constructor.name}\"!`);\n }\n }\n\n get type() {\n return (this.constructor as typeof Plugin).type;\n }\n}\n"],"mappings":";;;;;;;;AAAO,MAAeA,MAAM,CAAC;EAIzBC,WAAW,GAAG;IAAA;IACV,IAAI,CAAE,IAAI,CAACA,WAAW,CAAmBC,IAAI,EAAE;MAC3C,MAAMC,KAAK,CAAE,iCAAgC,IAAI,CAACF,WAAW,CAACG,IAAK,IAAG,CAAC;IAC3E;EACJ;EAEA,IAAIF,IAAI,GAAG;IACP,OAAQ,IAAI,CAACD,WAAW,CAAmBC,IAAI;EACnD;AACJ;AAAC;AAAA,8BAbqBF,MAAM"}
@@ -1,12 +1,14 @@
1
- import { Plugin } from "./types";
1
+ import { Plugin, PluginCollection } from "./types";
2
2
  export declare class PluginsContainer {
3
3
  private plugins;
4
4
  private _byTypeCache;
5
- constructor(...args: any[]);
6
- byName<T extends Plugin>(name: T["name"]): T;
5
+ constructor(...args: PluginCollection);
6
+ byName<T extends Plugin>(name: T["name"]): T | null;
7
7
  byType<T extends Plugin>(type: T["type"]): T[];
8
8
  atLeastOneByType<T extends Plugin>(type: T["type"]): T[];
9
9
  oneByType<T extends Plugin>(type: T["type"]): T;
10
+ merge(input: PluginsContainer | PluginCollection): void;
11
+ mergeByType(container: PluginsContainer, type: string): void;
10
12
  all<T extends Plugin>(): T[];
11
13
  register(...args: any): void;
12
14
  unregister(name: string): void;
@@ -1,113 +1,103 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.PluginsContainer = void 0;
9
-
10
8
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
9
  var _uniqid = _interopRequireDefault(require("uniqid"));
13
-
14
10
  const isOptionsObject = item => item && !Array.isArray(item) && !item.type && !item.name;
15
-
16
11
  const normalizeArgs = args => {
17
- let options = {}; // Check if last item in the plugins array is actually an options object.
12
+ let options = {};
18
13
 
14
+ // Check if last item in the plugins array is actually an options object.
19
15
  if (isOptionsObject(args[args.length - 1])) {
20
16
  [options] = args.splice(-1, 1);
21
17
  }
22
-
23
18
  return [args, options];
24
19
  };
25
-
26
20
  const assign = (plugins, options, target) => {
27
- for (let i = 0; i < plugins.length; i++) {
28
- const plugin = plugins[i];
29
-
21
+ for (const plugin of plugins) {
30
22
  if (Array.isArray(plugin)) {
31
23
  assign(plugin, options, target);
32
24
  continue;
33
25
  }
34
-
35
26
  let name = plugin._name || plugin.name;
36
-
37
27
  if (!name) {
38
28
  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
-
29
+ }
41
30
 
31
+ // If skip existing was set to true, and a plugin with the same name was already registered, skip registration.
42
32
  if (!options.skipExisting || !target[name]) {
43
33
  target[name] = plugin;
44
34
  plugin.init && plugin.init();
45
35
  }
46
36
  }
47
37
  };
48
-
49
38
  class PluginsContainer {
50
39
  constructor(...args) {
51
40
  (0, _defineProperty2.default)(this, "plugins", {});
52
41
  (0, _defineProperty2.default)(this, "_byTypeCache", {});
53
42
  this.register(...args);
54
43
  }
55
-
56
44
  byName(name) {
45
+ if (!name) {
46
+ return null;
47
+ }
48
+ /**
49
+ * We can safely cast name as string, we know it is so.
50
+ */
57
51
  return this.plugins[name];
58
52
  }
59
-
60
53
  byType(type) {
61
54
  if (this._byTypeCache[type]) {
62
55
  return Array.from(this._byTypeCache[type]);
63
56
  }
64
-
65
57
  const plugins = this.findByType(type);
66
58
  this._byTypeCache[type] = plugins;
67
59
  return Array.from(plugins);
68
60
  }
69
-
70
61
  atLeastOneByType(type) {
71
62
  const list = this.byType(type);
72
-
73
63
  if (list.length === 0) {
74
64
  throw new Error(`There are no plugins by type "${type}".`);
75
65
  }
76
-
77
66
  return list;
78
67
  }
79
-
80
68
  oneByType(type) {
81
69
  const list = this.atLeastOneByType(type);
82
-
83
70
  if (list.length > 1) {
84
71
  throw new Error(`There is a requirement for plugin of type "${type}" to be only one registered.`);
85
72
  }
86
-
87
73
  return list[0];
88
74
  }
89
-
75
+ merge(input) {
76
+ if (input instanceof PluginsContainer) {
77
+ this.register(...input.all());
78
+ return;
79
+ }
80
+ this.register(input);
81
+ }
82
+ mergeByType(container, type) {
83
+ this.register(...container.byType(type));
84
+ }
90
85
  all() {
91
86
  return Object.values(this.plugins);
92
87
  }
93
-
94
88
  register(...args) {
95
89
  // reset the cache when adding new plugins
96
90
  this._byTypeCache = {};
97
91
  const [plugins, options] = normalizeArgs(args);
98
92
  assign(plugins, options, this.plugins);
99
93
  }
100
-
101
94
  unregister(name) {
102
95
  // reset the cache when removing a plugin
103
96
  this._byTypeCache = {};
104
97
  delete this.plugins[name];
105
98
  }
106
-
107
99
  findByType(type) {
108
100
  return Object.values(this.plugins).filter(pl => pl.type === type);
109
101
  }
110
-
111
102
  }
112
-
113
103
  exports.PluginsContainer = PluginsContainer;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["isOptionsObject","item","Array","isArray","type","name","normalizeArgs","args","options","length","splice","assign","plugins","target","plugin","_name","uniqid","skipExisting","init","PluginsContainer","constructor","register","byName","byType","_byTypeCache","from","findByType","atLeastOneByType","list","Error","oneByType","merge","input","all","mergeByType","container","Object","values","unregister","filter","pl"],"sources":["PluginsContainer.ts"],"sourcesContent":["import { Plugin, PluginCollection } from \"./types\";\nimport uniqid from \"uniqid\";\n\nconst isOptionsObject = (item?: any) => item && !Array.isArray(item) && !item.type && !item.name;\nconst normalizeArgs = (args: any[]): [Plugin[], any] => {\n let options = {};\n\n // Check if last item in the plugins array is actually an options object.\n if (isOptionsObject(args[args.length - 1])) {\n [options] = args.splice(-1, 1);\n }\n\n return [args, options];\n};\n\nconst assign = (\n plugins: Plugin[] | Plugin[][],\n options: any,\n target: Record<string, any>\n): void => {\n for (const plugin of plugins) {\n if (Array.isArray(plugin)) {\n assign(plugin, options, target);\n continue;\n }\n\n let name = plugin._name || plugin.name;\n if (!name) {\n plugin.name = name = uniqid(plugin.type + \"-\");\n }\n\n // If skip existing was set to true, and a plugin with the same name was already registered, skip registration.\n if (!options.skipExisting || !target[name]) {\n target[name] = plugin;\n plugin.init && plugin.init();\n }\n }\n};\n\nexport class PluginsContainer {\n private plugins: Record<string, Plugin> = {};\n private _byTypeCache: Record<string, Plugin[]> = {};\n\n constructor(...args: PluginCollection) {\n this.register(...args);\n }\n\n public byName<T extends Plugin>(name: T[\"name\"]): T | null {\n if (!name) {\n return null;\n }\n /**\n * We can safely cast name as string, we know it is so.\n */\n return this.plugins[name as string] as T;\n }\n\n public byType<T extends Plugin>(type: T[\"type\"]): T[] {\n if (this._byTypeCache[type]) {\n return Array.from(this._byTypeCache[type]) as T[];\n }\n const plugins = this.findByType<T>(type);\n this._byTypeCache[type] = plugins;\n return Array.from(plugins);\n }\n\n public atLeastOneByType<T extends Plugin>(type: T[\"type\"]): T[] {\n const list = this.byType<T>(type);\n if (list.length === 0) {\n throw new Error(`There are no plugins by type \"${type}\".`);\n }\n return list;\n }\n\n public oneByType<T extends Plugin>(type: T[\"type\"]): T {\n const list = this.atLeastOneByType<T>(type);\n if (list.length > 1) {\n throw new Error(\n `There is a requirement for plugin of type \"${type}\" to be only one registered.`\n );\n }\n return list[0];\n }\n\n public merge(input: PluginsContainer | PluginCollection): void {\n if (input instanceof PluginsContainer) {\n this.register(...input.all());\n return;\n }\n this.register(input);\n }\n\n public mergeByType(container: PluginsContainer, type: string): void {\n this.register(...container.byType(type));\n }\n\n public all<T extends Plugin>(): T[] {\n return Object.values(this.plugins) as T[];\n }\n\n public register(...args: any): void {\n // reset the cache when adding new plugins\n this._byTypeCache = {};\n const [plugins, options] = normalizeArgs(args);\n assign(plugins, options, this.plugins);\n }\n\n public unregister(name: string): void {\n // reset the cache when removing a plugin\n this._byTypeCache = {};\n delete this.plugins[name];\n }\n\n private findByType<T extends Plugin>(type: T[\"type\"]): T[] {\n return Object.values(this.plugins).filter((pl): pl is T => pl.type === type) as T[];\n }\n}\n"],"mappings":";;;;;;;;AACA;AAEA,MAAMA,eAAe,GAAIC,IAAU,IAAKA,IAAI,IAAI,CAACC,KAAK,CAACC,OAAO,CAACF,IAAI,CAAC,IAAI,CAACA,IAAI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/B,IAAY,EAAQ;IAChE,IAAI,CAACiB,QAAQ,CAAC,GAAGc,SAAS,CAACZ,MAAM,CAACnB,IAAI,CAAC,CAAC;EAC5C;EAEO6B,GAAG,GAA0B;IAChC,OAAOG,MAAM,CAACC,MAAM,CAAC,IAAI,CAACzB,OAAO,CAAC;EACtC;EAEOS,QAAQ,CAAC,GAAGd,IAAS,EAAQ;IAChC;IACA,IAAI,CAACiB,YAAY,GAAG,CAAC,CAAC;IACtB,MAAM,CAACZ,OAAO,EAAEJ,OAAO,CAAC,GAAGF,aAAa,CAACC,IAAI,CAAC;IAC9CI,MAAM,CAACC,OAAO,EAAEJ,OAAO,EAAE,IAAI,CAACI,OAAO,CAAC;EAC1C;EAEO0B,UAAU,CAACjC,IAAY,EAAQ;IAClC;IACA,IAAI,CAACmB,YAAY,GAAG,CAAC,CAAC;IACtB,OAAO,IAAI,CAACZ,OAAO,CAACP,IAAI,CAAC;EAC7B;EAEQqB,UAAU,CAAmBtB,IAAe,EAAO;IACvD,OAAOgC,MAAM,CAACC,MAAM,CAAC,IAAI,CAACzB,OAAO,CAAC,CAAC2B,MAAM,CAAEC,EAAE,IAAcA,EAAE,CAACpC,IAAI,KAAKA,IAAI,CAAC;EAChF;AACJ;AAAC"}
package/index.js CHANGED
@@ -16,10 +16,7 @@ Object.defineProperty(exports, "PluginsContainer", {
16
16
  }
17
17
  });
18
18
  exports.plugins = void 0;
19
-
20
19
  var _PluginsContainer = require("./PluginsContainer");
21
-
22
20
  var _Plugin = require("./Plugin");
23
-
24
21
  const plugins = new _PluginsContainer.PluginsContainer();
25
22
  exports.plugins = plugins;
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["plugins","PluginsContainer"],"sources":["index.ts"],"sourcesContent":["import { PluginsContainer } from \"./PluginsContainer\";\nimport { Plugin } from \"./Plugin\";\n\nconst plugins = new PluginsContainer();\n\nexport { Plugin, PluginsContainer, plugins };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AACA;AAEA,MAAMA,OAAO,GAAG,IAAIC,kCAAgB,EAAE;AAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/plugins",
3
- "version": "0.0.0-mt-3",
3
+ "version": "0.0.0-unstable.1145e7667f",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,16 +13,18 @@
13
13
  ],
14
14
  "license": "MIT",
15
15
  "dependencies": {
16
- "@babel/runtime": "7.15.4",
16
+ "@babel/runtime": "7.20.13",
17
17
  "uniqid": "5.4.0"
18
18
  },
19
19
  "devDependencies": {
20
- "@babel/cli": "^7.5.5",
21
- "@babel/core": "^7.5.5",
22
- "@webiny/cli": "^0.0.0-mt-3",
23
- "@webiny/project-utils": "^0.0.0-mt-3",
20
+ "@babel/cli": "^7.19.3",
21
+ "@babel/core": "^7.19.3",
22
+ "@types/uniqid": "^5.3.2",
23
+ "@webiny/cli": "^0.0.0-unstable.1145e7667f",
24
+ "@webiny/project-utils": "^0.0.0-unstable.1145e7667f",
24
25
  "rimraf": "^3.0.2",
25
- "typescript": "^4.1.3"
26
+ "ttypescript": "^1.5.13",
27
+ "typescript": "4.7.4"
26
28
  },
27
29
  "publishConfig": {
28
30
  "access": "public",
@@ -32,5 +34,5 @@
32
34
  "build": "yarn webiny run build",
33
35
  "watch": "yarn webiny run watch"
34
36
  },
35
- "gitHead": "ebea815be2be99404591cba465cc1fe88355bd48"
37
+ "gitHead": "1145e7667ffd3d18bfea1e73d6078cd6a35c1038"
36
38
  }
package/types.js CHANGED
@@ -9,5 +9,4 @@ Object.defineProperty(exports, "PluginsContainer", {
9
9
  return _PluginsContainer.PluginsContainer;
10
10
  }
11
11
  });
12
-
13
12
  var _PluginsContainer = require("./PluginsContainer");
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["export { PluginsContainer } from \"./PluginsContainer\";\n\nexport type Plugin<T = Record<string, any>> = {\n type: string;\n name?: string;\n init?: () => void;\n [key: string]: any;\n} & T;\n\nexport type PluginCollection = (Plugin | PluginCollection)[];\n"],"mappings":";;;;;;;;;;;AAAA"}