@arch-cadre/modules 0.0.49 → 0.0.50

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.
@@ -1,86 +1,94 @@
1
+ "use strict";
1
2
  "use server";
2
3
 
3
- const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
4
- const require_manage = require('./manage.cjs');
5
- let _arch_cadre_core_server = require("@arch-cadre/core/server");
6
- let _arch_cadre_core = require("@arch-cadre/core");
7
- let drizzle_orm = require("drizzle-orm");
8
-
9
- //#region src/server/registry.ts
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.initModules = initModules;
8
+ var _core = require("@arch-cadre/core");
9
+ var _server = require("@arch-cadre/core/server");
10
+ var _drizzleOrm = require("drizzle-orm");
11
+ var _manage = require("./manage.js");
10
12
  const globalForRegistry = globalThis;
11
13
  async function initModules(force = false) {
12
- if (globalForRegistry.__KRYO_MODULES_INITIALIZED__ && !force) return;
13
- if (force) {
14
- console.log("[Kernel:Registry] Forcing re-initialization...");
15
- _arch_cadre_core.eventBus.clearAll();
16
- }
17
- console.log("[Kernel:Registry] Synchronizing module listeners...");
18
- globalForRegistry.__KRYO_MODULES_INITIALIZED__ = true;
19
- await _arch_cadre_core.eventBus.publish("system:modules:init:start", { timestamp: Date.now() });
20
- const processedModuleIds = /* @__PURE__ */ new Set();
21
- let hasNewModules = true;
22
- let iterations = 0;
23
- const MAX_ITERATIONS = 10;
24
- while (hasNewModules && iterations < MAX_ITERATIONS) {
25
- hasNewModules = false;
26
- iterations++;
27
- const pendingModules = (await require_manage.getModules()).filter((mod) => !processedModuleIds.has(mod.id));
28
- if (pendingModules.length === 0) break;
29
- for (const mod of pendingModules) try {
30
- await _arch_cadre_core_server.db.insert(_arch_cadre_core.systemModulesTable).values({
31
- id: mod.id,
32
- enabled: mod.system ?? false,
33
- installed: mod.system ?? false,
34
- system: mod.system ?? false
35
- }).onConflictDoNothing();
36
- } catch (_e) {}
37
- const sortedPending = [...pendingModules].sort((a, b) => a.system === b.system ? 0 : a.system ? -1 : 1);
38
- for (const mod of sortedPending) {
39
- processedModuleIds.add(mod.id);
40
- hasNewModules = true;
41
- try {
42
- const enabled = await require_manage.isModuleEnabled(mod.id);
43
- if (!enabled) continue;
44
- const instance = await require_manage.getModuleInstance(mod.id);
45
- if (!instance) {
46
- console.warn(`[Kernel:Registry] No instance found for module ${mod.id}. Ensure it is registered correctly.`);
47
- continue;
48
- }
49
- let dbMod = null;
50
- try {
51
- [dbMod] = await _arch_cadre_core_server.db.select().from(_arch_cadre_core.systemModulesTable).where((0, drizzle_orm.eq)(_arch_cadre_core.systemModulesTable.id, mod.id));
52
- } catch (_e) {}
53
- if (enabled && (!dbMod || !dbMod.installed)) {
54
- console.log(`[Kernel:Registry] Installing module ${mod.id}...`);
55
- if (instance.onMigrate) {
56
- console.log(`[Kernel:Registry] Running onMigrate for ${mod.id}...`);
57
- await instance.onMigrate();
58
- }
59
- console.log(`[Kernel:Registry] Running onEnable for ${mod.id}...`);
60
- if (instance.onEnable) await instance.onEnable();
61
- try {
62
- await _arch_cadre_core_server.db.update(_arch_cadre_core.systemModulesTable).set({
63
- installed: true,
64
- lastStep: null
65
- }).where((0, drizzle_orm.eq)(_arch_cadre_core.systemModulesTable.id, mod.id));
66
- } catch (_e) {}
67
- }
68
- if (instance.init) {
69
- console.log(`[Kernel:Registry] Initializing ${mod.id}...`);
70
- await instance.init();
71
- console.log(`[Kernel:Registry] Module ${mod.id} is active.`);
72
- }
73
- } catch (error) {
74
- console.error(`[Kernel:Registry] Critical failure for ${mod.id}:`, error);
75
- }
76
- }
77
- }
78
- if (iterations >= MAX_ITERATIONS) console.warn("[Kernel:Registry] Max init iterations reached. Check for circular module registrations.");
79
- await _arch_cadre_core.eventBus.publish("system:modules:init:end", {
80
- timestamp: Date.now(),
81
- moduleCount: processedModuleIds.size
82
- });
83
- }
84
-
85
- //#endregion
86
- exports.initModules = initModules;
14
+ if (globalForRegistry.__KRYO_MODULES_INITIALIZED__ && !force) return;
15
+ if (force) {
16
+ console.log("[Kernel:Registry] Forcing re-initialization...");
17
+ _core.eventBus.clearAll();
18
+ }
19
+ console.log("[Kernel:Registry] Synchronizing module listeners...");
20
+ globalForRegistry.__KRYO_MODULES_INITIALIZED__ = true;
21
+ await _core.eventBus.publish("system:modules:init:start", {
22
+ timestamp: Date.now()
23
+ });
24
+ const processedModuleIds = /* @__PURE__ */new Set();
25
+ let hasNewModules = true;
26
+ let iterations = 0;
27
+ const MAX_ITERATIONS = 10;
28
+ while (hasNewModules && iterations < MAX_ITERATIONS) {
29
+ hasNewModules = false;
30
+ iterations++;
31
+ const currentModules = await (0, _manage.getModules)();
32
+ const pendingModules = currentModules.filter(mod => !processedModuleIds.has(mod.id));
33
+ if (pendingModules.length === 0) {
34
+ break;
35
+ }
36
+ for (const mod of pendingModules) {
37
+ try {
38
+ await _server.db.insert(_core.systemModulesTable).values({
39
+ id: mod.id,
40
+ enabled: mod.system ?? false,
41
+ installed: mod.system ?? false,
42
+ system: mod.system ?? false
43
+ }).onConflictDoNothing();
44
+ } catch (_e) {}
45
+ }
46
+ const sortedPending = [...pendingModules].sort((a, b) => a.system === b.system ? 0 : a.system ? -1 : 1);
47
+ for (const mod of sortedPending) {
48
+ processedModuleIds.add(mod.id);
49
+ hasNewModules = true;
50
+ try {
51
+ const enabled = await (0, _manage.isModuleEnabled)(mod.id);
52
+ if (!enabled) continue;
53
+ const instance = await (0, _manage.getModuleInstance)(mod.id);
54
+ if (!instance) {
55
+ console.warn(`[Kernel:Registry] No instance found for module ${mod.id}. Ensure it is registered correctly.`);
56
+ continue;
57
+ }
58
+ let dbMod = null;
59
+ try {
60
+ [dbMod] = await _server.db.select().from(_core.systemModulesTable).where((0, _drizzleOrm.eq)(_core.systemModulesTable.id, mod.id));
61
+ } catch (_e) {}
62
+ if (enabled && (!dbMod || !dbMod.installed)) {
63
+ console.log(`[Kernel:Registry] Installing module ${mod.id}...`);
64
+ if (instance.onMigrate) {
65
+ console.log(`[Kernel:Registry] Running onMigrate for ${mod.id}...`);
66
+ await instance.onMigrate();
67
+ }
68
+ console.log(`[Kernel:Registry] Running onEnable for ${mod.id}...`);
69
+ if (instance.onEnable) await instance.onEnable();
70
+ try {
71
+ await _server.db.update(_core.systemModulesTable).set({
72
+ installed: true,
73
+ lastStep: null
74
+ }).where((0, _drizzleOrm.eq)(_core.systemModulesTable.id, mod.id));
75
+ } catch (_e) {}
76
+ }
77
+ if (instance.init) {
78
+ console.log(`[Kernel:Registry] Initializing ${mod.id}...`);
79
+ await instance.init();
80
+ console.log(`[Kernel:Registry] Module ${mod.id} is active.`);
81
+ }
82
+ } catch (error) {
83
+ console.error(`[Kernel:Registry] Critical failure for ${mod.id}:`, error);
84
+ }
85
+ }
86
+ }
87
+ if (iterations >= MAX_ITERATIONS) {
88
+ console.warn("[Kernel:Registry] Max init iterations reached. Check for circular module registrations.");
89
+ }
90
+ await _core.eventBus.publish("system:modules:init:end", {
91
+ timestamp: Date.now(),
92
+ moduleCount: processedModuleIds.size
93
+ });
94
+ }
@@ -0,0 +1 @@
1
+ export declare function initModules(force?: boolean): Promise<void>;
@@ -1,85 +1,99 @@
1
1
  "use server";
2
-
3
- import { getModuleInstance, getModules, isModuleEnabled } from "./manage.mjs";
4
- import { db } from "@arch-cadre/core/server";
5
2
  import { eventBus, systemModulesTable } from "@arch-cadre/core";
3
+ import { db } from "@arch-cadre/core/server";
6
4
  import { eq } from "drizzle-orm";
7
-
8
- //#region src/server/registry.ts
5
+ import { getModuleInstance, getModules, isModuleEnabled } from "./manage.js";
9
6
  const globalForRegistry = globalThis;
10
- async function initModules(force = false) {
11
- if (globalForRegistry.__KRYO_MODULES_INITIALIZED__ && !force) return;
12
- if (force) {
13
- console.log("[Kernel:Registry] Forcing re-initialization...");
14
- eventBus.clearAll();
15
- }
16
- console.log("[Kernel:Registry] Synchronizing module listeners...");
17
- globalForRegistry.__KRYO_MODULES_INITIALIZED__ = true;
18
- await eventBus.publish("system:modules:init:start", { timestamp: Date.now() });
19
- const processedModuleIds = /* @__PURE__ */ new Set();
20
- let hasNewModules = true;
21
- let iterations = 0;
22
- const MAX_ITERATIONS = 10;
23
- while (hasNewModules && iterations < MAX_ITERATIONS) {
24
- hasNewModules = false;
25
- iterations++;
26
- const pendingModules = (await getModules()).filter((mod) => !processedModuleIds.has(mod.id));
27
- if (pendingModules.length === 0) break;
28
- for (const mod of pendingModules) try {
29
- await db.insert(systemModulesTable).values({
30
- id: mod.id,
31
- enabled: mod.system ?? false,
32
- installed: mod.system ?? false,
33
- system: mod.system ?? false
34
- }).onConflictDoNothing();
35
- } catch (_e) {}
36
- const sortedPending = [...pendingModules].sort((a, b) => a.system === b.system ? 0 : a.system ? -1 : 1);
37
- for (const mod of sortedPending) {
38
- processedModuleIds.add(mod.id);
39
- hasNewModules = true;
40
- try {
41
- const enabled = await isModuleEnabled(mod.id);
42
- if (!enabled) continue;
43
- const instance = await getModuleInstance(mod.id);
44
- if (!instance) {
45
- console.warn(`[Kernel:Registry] No instance found for module ${mod.id}. Ensure it is registered correctly.`);
46
- continue;
47
- }
48
- let dbMod = null;
49
- try {
50
- [dbMod] = await db.select().from(systemModulesTable).where(eq(systemModulesTable.id, mod.id));
51
- } catch (_e) {}
52
- if (enabled && (!dbMod || !dbMod.installed)) {
53
- console.log(`[Kernel:Registry] Installing module ${mod.id}...`);
54
- if (instance.onMigrate) {
55
- console.log(`[Kernel:Registry] Running onMigrate for ${mod.id}...`);
56
- await instance.onMigrate();
57
- }
58
- console.log(`[Kernel:Registry] Running onEnable for ${mod.id}...`);
59
- if (instance.onEnable) await instance.onEnable();
60
- try {
61
- await db.update(systemModulesTable).set({
62
- installed: true,
63
- lastStep: null
64
- }).where(eq(systemModulesTable.id, mod.id));
65
- } catch (_e) {}
66
- }
67
- if (instance.init) {
68
- console.log(`[Kernel:Registry] Initializing ${mod.id}...`);
69
- await instance.init();
70
- console.log(`[Kernel:Registry] Module ${mod.id} is active.`);
71
- }
72
- } catch (error) {
73
- console.error(`[Kernel:Registry] Critical failure for ${mod.id}:`, error);
74
- }
75
- }
76
- }
77
- if (iterations >= MAX_ITERATIONS) console.warn("[Kernel:Registry] Max init iterations reached. Check for circular module registrations.");
78
- await eventBus.publish("system:modules:init:end", {
79
- timestamp: Date.now(),
80
- moduleCount: processedModuleIds.size
81
- });
7
+ export async function initModules(force = false) {
8
+ if (globalForRegistry.__KRYO_MODULES_INITIALIZED__ && !force) return;
9
+ if (force) {
10
+ console.log("[Kernel:Registry] Forcing re-initialization...");
11
+ eventBus.clearAll();
12
+ }
13
+ console.log("[Kernel:Registry] Synchronizing module listeners...");
14
+ globalForRegistry.__KRYO_MODULES_INITIALIZED__ = true;
15
+ await eventBus.publish("system:modules:init:start", {
16
+ timestamp: Date.now()
17
+ });
18
+ const processedModuleIds = /* @__PURE__ */ new Set();
19
+ let hasNewModules = true;
20
+ let iterations = 0;
21
+ const MAX_ITERATIONS = 10;
22
+ while (hasNewModules && iterations < MAX_ITERATIONS) {
23
+ hasNewModules = false;
24
+ iterations++;
25
+ const currentModules = await getModules();
26
+ const pendingModules = currentModules.filter(
27
+ (mod) => !processedModuleIds.has(mod.id)
28
+ );
29
+ if (pendingModules.length === 0) {
30
+ break;
31
+ }
32
+ for (const mod of pendingModules) {
33
+ try {
34
+ await db.insert(systemModulesTable).values({
35
+ id: mod.id,
36
+ enabled: mod.system ?? false,
37
+ installed: mod.system ?? false,
38
+ system: mod.system ?? false
39
+ }).onConflictDoNothing();
40
+ } catch (_e) {
41
+ }
42
+ }
43
+ const sortedPending = [...pendingModules].sort(
44
+ (a, b) => a.system === b.system ? 0 : a.system ? -1 : 1
45
+ );
46
+ for (const mod of sortedPending) {
47
+ processedModuleIds.add(mod.id);
48
+ hasNewModules = true;
49
+ try {
50
+ const enabled = await isModuleEnabled(mod.id);
51
+ if (!enabled) continue;
52
+ const instance = await getModuleInstance(mod.id);
53
+ if (!instance) {
54
+ console.warn(
55
+ `[Kernel:Registry] No instance found for module ${mod.id}. Ensure it is registered correctly.`
56
+ );
57
+ continue;
58
+ }
59
+ let dbMod = null;
60
+ try {
61
+ [dbMod] = await db.select().from(systemModulesTable).where(eq(systemModulesTable.id, mod.id));
62
+ } catch (_e) {
63
+ }
64
+ if (enabled && (!dbMod || !dbMod.installed)) {
65
+ console.log(`[Kernel:Registry] Installing module ${mod.id}...`);
66
+ if (instance.onMigrate) {
67
+ console.log(`[Kernel:Registry] Running onMigrate for ${mod.id}...`);
68
+ await instance.onMigrate();
69
+ }
70
+ console.log(`[Kernel:Registry] Running onEnable for ${mod.id}...`);
71
+ if (instance.onEnable) await instance.onEnable();
72
+ try {
73
+ await db.update(systemModulesTable).set({ installed: true, lastStep: null }).where(eq(systemModulesTable.id, mod.id));
74
+ } catch (_e) {
75
+ }
76
+ }
77
+ if (instance.init) {
78
+ console.log(`[Kernel:Registry] Initializing ${mod.id}...`);
79
+ await instance.init();
80
+ console.log(`[Kernel:Registry] Module ${mod.id} is active.`);
81
+ }
82
+ } catch (error) {
83
+ console.error(
84
+ `[Kernel:Registry] Critical failure for ${mod.id}:`,
85
+ error
86
+ );
87
+ }
88
+ }
89
+ }
90
+ if (iterations >= MAX_ITERATIONS) {
91
+ console.warn(
92
+ "[Kernel:Registry] Max init iterations reached. Check for circular module registrations."
93
+ );
94
+ }
95
+ await eventBus.publish("system:modules:init:end", {
96
+ timestamp: Date.now(),
97
+ moduleCount: processedModuleIds.size
98
+ });
82
99
  }
83
-
84
- //#endregion
85
- export { initModules };