@shevky/core 0.0.2 → 0.0.4

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.
@@ -0,0 +1,103 @@
1
+ import { log as _log, plugin as _plugin } from "@shevky/base";
2
+ import { PluginRegistry } from "../registries/pluginRegistry.js";
3
+ import { ContentRegistry } from "../registries/contentRegistry.js";
4
+ import { Project } from "../lib/project.js";
5
+ import { MetaEngine } from "./metaEngine.js";
6
+
7
+ /** @typedef {import("../types/index.d.ts").PluginExecutionContext} PluginExecutionContext */
8
+
9
+ export class PluginEngine {
10
+ /**
11
+ * @type {Project}
12
+ */
13
+ #_project = new Project(process.cwd());
14
+
15
+ /**
16
+ * @type {PluginRegistry}
17
+ */
18
+ #_pluginRegistry;
19
+
20
+ /**
21
+ * @type {ContentRegistry}
22
+ */
23
+ #_contentRegistry;
24
+
25
+ /**
26
+ * @type {MetaEngine}
27
+ */
28
+ #_metaEngine;
29
+
30
+ /**
31
+ * @param {PluginRegistry} pluginRegistry
32
+ * @param {ContentRegistry} contentRegistry
33
+ * @param {MetaEngine} metaEngine
34
+ */
35
+ constructor(pluginRegistry, contentRegistry, metaEngine) {
36
+ this.#_pluginRegistry = pluginRegistry;
37
+ this.#_contentRegistry = contentRegistry;
38
+ this.#_metaEngine = metaEngine;
39
+ }
40
+
41
+ /**
42
+ * @param {string} hook
43
+ * @returns {Promise<void>}
44
+ */
45
+ async execute(hook) {
46
+ for (const [name, plugin] of this.#_pluginRegistry.plugins) {
47
+ /** @type {Record<string, Function | undefined>} */
48
+ const hooks = plugin.hooks ?? {};
49
+ if (!Object.keys(hooks).length) {
50
+ _log.warn(`The '${name}' plugin is invalid. Does not contains hook.`);
51
+ continue;
52
+ }
53
+
54
+ const handler = hooks[hook];
55
+ if (!handler) {
56
+ continue;
57
+ }
58
+
59
+ try {
60
+ const ctx = this.#_createContext(hook);
61
+
62
+ _log.debug(
63
+ `The '${name}' plugin has been triggered with '${hook}' hook.`,
64
+ );
65
+
66
+ await handler(ctx);
67
+ } catch (error) {
68
+ _log.err(
69
+ `The '${name}' plugin has been failed with '${hook}' hook. Error: `,
70
+ error,
71
+ );
72
+ }
73
+ }
74
+ }
75
+
76
+ /**
77
+ * @param {string} hook
78
+ * @returns {PluginExecutionContext}
79
+ */
80
+ #_createContext(hook) {
81
+ const baseContext = _plugin.createBaseContext();
82
+ return {
83
+ ...baseContext,
84
+ paths: this.#_project.toObject(),
85
+
86
+ // content:load
87
+ ...(hook === _plugin.hooks.CONTENT_LOAD
88
+ ? {
89
+ contentFiles: this.#_contentRegistry.files,
90
+ addContent: (input) => this.#_contentRegistry.addContent(input),
91
+ }
92
+ : {}),
93
+ ...(hook === _plugin.hooks.CONTENT_READY && this.#_metaEngine
94
+ ? {
95
+ contentFiles: this.#_contentRegistry.files,
96
+ pages: this.#_contentRegistry.buildCategoryTagCollections(),
97
+ footerPolicies: this.#_contentRegistry.buildFooterPolicies(),
98
+ contentIndex: this.#_contentRegistry.buildContentIndex(),
99
+ }
100
+ : {}),
101
+ };
102
+ }
103
+ }