@cldmv/slothlet 2.11.0 → 3.0.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.
Files changed (189) hide show
  1. package/AGENT-USAGE.md +355 -325
  2. package/README.md +554 -238
  3. package/dist/lib/builders/api-assignment.mjs +605 -0
  4. package/dist/lib/builders/api_builder.mjs +1073 -0
  5. package/dist/lib/builders/builder.mjs +94 -0
  6. package/dist/lib/builders/modes-processor.mjs +1816 -0
  7. package/dist/lib/errors.mjs +227 -0
  8. package/dist/lib/factories/component-base.mjs +96 -0
  9. package/dist/lib/factories/context.mjs +38 -0
  10. package/dist/lib/handlers/api-cache-manager.mjs +216 -0
  11. package/dist/lib/handlers/api-manager.mjs +2364 -0
  12. package/dist/lib/handlers/context-async.mjs +184 -0
  13. package/dist/lib/handlers/context-live.mjs +184 -0
  14. package/dist/lib/handlers/hook-manager.mjs +789 -0
  15. package/dist/lib/handlers/lifecycle-token.mjs +44 -0
  16. package/dist/lib/handlers/lifecycle.mjs +131 -0
  17. package/dist/lib/handlers/materialize-manager.mjs +64 -0
  18. package/dist/lib/handlers/metadata.mjs +500 -0
  19. package/dist/lib/handlers/ownership.mjs +338 -0
  20. package/dist/lib/handlers/unified-wrapper.mjs +3031 -0
  21. package/dist/lib/helpers/class-instance-wrapper.mjs +125 -0
  22. package/dist/lib/helpers/config.mjs +343 -0
  23. package/dist/lib/helpers/eventemitter-context.mjs +365 -0
  24. package/dist/lib/helpers/hint-detector.mjs +63 -0
  25. package/dist/lib/helpers/modes-utils.mjs +53 -0
  26. package/dist/lib/helpers/resolve-from-caller.mjs +123 -117
  27. package/dist/lib/helpers/sanitize.mjs +247 -168
  28. package/dist/lib/helpers/utilities.mjs +46 -81
  29. package/dist/lib/i18n/languages/de-de.json +377 -0
  30. package/dist/lib/i18n/languages/en-gb.json +377 -0
  31. package/dist/lib/i18n/languages/en-us.json +377 -0
  32. package/dist/lib/i18n/languages/es-mx.json +377 -0
  33. package/dist/lib/i18n/languages/fr-fr.json +377 -0
  34. package/dist/lib/i18n/languages/hi-in.json +377 -0
  35. package/dist/lib/i18n/languages/ja-jp.json +377 -0
  36. package/dist/lib/i18n/languages/ko-kr.json +377 -0
  37. package/dist/lib/i18n/languages/pt-br.json +377 -0
  38. package/dist/lib/i18n/languages/ru-ru.json +377 -0
  39. package/dist/lib/i18n/languages/zh-cn.json +377 -0
  40. package/dist/lib/i18n/translations.mjs +140 -0
  41. package/dist/lib/modes/eager.mjs +75 -0
  42. package/dist/lib/modes/lazy.mjs +97 -0
  43. package/dist/lib/processors/flatten.mjs +453 -0
  44. package/dist/lib/processors/loader.mjs +355 -0
  45. package/dist/lib/processors/type-generator.mjs +291 -0
  46. package/dist/lib/processors/typescript.mjs +188 -0
  47. package/dist/lib/runtime/runtime-asynclocalstorage.mjs +80 -522
  48. package/dist/lib/runtime/runtime-livebindings.mjs +45 -390
  49. package/dist/lib/runtime/runtime.mjs +39 -159
  50. package/dist/slothlet.mjs +525 -744
  51. package/docs/API-RULES.md +338 -486
  52. package/index.cjs +4 -4
  53. package/index.mjs +82 -45
  54. package/package.json +143 -30
  55. package/types/dist/lib/builders/api-assignment.d.mts +97 -0
  56. package/types/dist/lib/builders/api-assignment.d.mts.map +1 -0
  57. package/types/dist/lib/builders/api_builder.d.mts +96 -0
  58. package/types/dist/lib/builders/api_builder.d.mts.map +1 -0
  59. package/types/dist/lib/builders/builder.d.mts +60 -0
  60. package/types/dist/lib/builders/builder.d.mts.map +1 -0
  61. package/types/dist/lib/builders/modes-processor.d.mts +32 -0
  62. package/types/dist/lib/builders/modes-processor.d.mts.map +1 -0
  63. package/types/dist/lib/errors.d.mts +118 -0
  64. package/types/dist/lib/errors.d.mts.map +1 -0
  65. package/types/dist/lib/factories/component-base.d.mts +182 -0
  66. package/types/dist/lib/factories/component-base.d.mts.map +1 -0
  67. package/types/dist/lib/factories/context.d.mts +26 -0
  68. package/types/dist/lib/factories/context.d.mts.map +1 -0
  69. package/types/dist/lib/handlers/api-cache-manager.d.mts +208 -0
  70. package/types/dist/lib/handlers/api-cache-manager.d.mts.map +1 -0
  71. package/types/dist/lib/handlers/api-manager.d.mts +392 -0
  72. package/types/dist/lib/handlers/api-manager.d.mts.map +1 -0
  73. package/types/dist/lib/handlers/context-async.d.mts +66 -0
  74. package/types/dist/lib/handlers/context-async.d.mts.map +1 -0
  75. package/types/dist/lib/handlers/context-live.d.mts +65 -0
  76. package/types/dist/lib/handlers/context-live.d.mts.map +1 -0
  77. package/types/dist/lib/handlers/hook-manager.d.mts +199 -0
  78. package/types/dist/lib/handlers/hook-manager.d.mts.map +1 -0
  79. package/types/dist/lib/handlers/lifecycle-token.d.mts +49 -0
  80. package/types/dist/lib/handlers/lifecycle-token.d.mts.map +1 -0
  81. package/types/dist/lib/handlers/lifecycle.d.mts +90 -0
  82. package/types/dist/lib/handlers/lifecycle.d.mts.map +1 -0
  83. package/types/dist/lib/handlers/materialize-manager.d.mts +75 -0
  84. package/types/dist/lib/handlers/materialize-manager.d.mts.map +1 -0
  85. package/types/dist/lib/handlers/metadata.d.mts +215 -0
  86. package/types/dist/lib/handlers/metadata.d.mts.map +1 -0
  87. package/types/dist/lib/handlers/ownership.d.mts +170 -0
  88. package/types/dist/lib/handlers/ownership.d.mts.map +1 -0
  89. package/types/dist/lib/handlers/unified-wrapper.d.mts +250 -0
  90. package/types/dist/lib/handlers/unified-wrapper.d.mts.map +1 -0
  91. package/types/dist/lib/helpers/class-instance-wrapper.d.mts +54 -0
  92. package/types/dist/lib/helpers/class-instance-wrapper.d.mts.map +1 -0
  93. package/types/dist/lib/helpers/config.d.mts +96 -0
  94. package/types/dist/lib/helpers/config.d.mts.map +1 -0
  95. package/types/dist/lib/helpers/eventemitter-context.d.mts +31 -0
  96. package/types/dist/lib/helpers/eventemitter-context.d.mts.map +1 -0
  97. package/types/dist/lib/helpers/hint-detector.d.mts +20 -0
  98. package/types/dist/lib/helpers/hint-detector.d.mts.map +1 -0
  99. package/types/dist/lib/helpers/modes-utils.d.mts +35 -0
  100. package/types/dist/lib/helpers/modes-utils.d.mts.map +1 -0
  101. package/types/dist/lib/helpers/resolve-from-caller.d.mts +29 -145
  102. package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
  103. package/types/dist/lib/helpers/sanitize.d.mts +95 -94
  104. package/types/dist/lib/helpers/sanitize.d.mts.map +1 -1
  105. package/types/dist/lib/helpers/utilities.d.mts +53 -116
  106. package/types/dist/lib/helpers/utilities.d.mts.map +1 -1
  107. package/types/dist/lib/i18n/translations.d.mts +39 -0
  108. package/types/dist/lib/i18n/translations.d.mts.map +1 -0
  109. package/types/dist/lib/modes/eager.d.mts +36 -0
  110. package/types/dist/lib/modes/eager.d.mts.map +1 -0
  111. package/types/dist/lib/modes/lazy.d.mts +49 -0
  112. package/types/dist/lib/modes/lazy.d.mts.map +1 -0
  113. package/types/dist/lib/processors/flatten.d.mts +114 -0
  114. package/types/dist/lib/processors/flatten.d.mts.map +1 -0
  115. package/types/dist/lib/processors/loader.d.mts +47 -0
  116. package/types/dist/lib/processors/loader.d.mts.map +1 -0
  117. package/types/dist/lib/processors/type-generator.d.mts +19 -0
  118. package/types/dist/lib/processors/type-generator.d.mts.map +1 -0
  119. package/types/dist/lib/processors/typescript.d.mts +55 -0
  120. package/types/dist/lib/processors/typescript.d.mts.map +1 -0
  121. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +47 -42
  122. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
  123. package/types/dist/lib/runtime/runtime-livebindings.d.mts +34 -65
  124. package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
  125. package/types/dist/lib/runtime/runtime.d.mts +39 -9
  126. package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
  127. package/types/dist/slothlet.d.mts +184 -111
  128. package/types/dist/slothlet.d.mts.map +1 -1
  129. package/types/index.d.mts +1 -3
  130. package/dist/lib/engine/README.md +0 -21
  131. package/dist/lib/engine/slothlet_child.mjs +0 -59
  132. package/dist/lib/engine/slothlet_engine.mjs +0 -372
  133. package/dist/lib/engine/slothlet_esm.mjs +0 -230
  134. package/dist/lib/engine/slothlet_helpers.mjs +0 -455
  135. package/dist/lib/engine/slothlet_worker.mjs +0 -149
  136. package/dist/lib/helpers/als-eventemitter.mjs +0 -256
  137. package/dist/lib/helpers/api_builder/add_api.mjs +0 -553
  138. package/dist/lib/helpers/api_builder/analysis.mjs +0 -532
  139. package/dist/lib/helpers/api_builder/construction.mjs +0 -495
  140. package/dist/lib/helpers/api_builder/decisions.mjs +0 -748
  141. package/dist/lib/helpers/api_builder/metadata.mjs +0 -248
  142. package/dist/lib/helpers/api_builder.mjs +0 -41
  143. package/dist/lib/helpers/auto-wrap.mjs +0 -62
  144. package/dist/lib/helpers/hooks.mjs +0 -389
  145. package/dist/lib/helpers/instance-manager.mjs +0 -111
  146. package/dist/lib/helpers/metadata-api.mjs +0 -201
  147. package/dist/lib/helpers/multidefault.mjs +0 -216
  148. package/dist/lib/modes/slothlet_eager.mjs +0 -154
  149. package/dist/lib/modes/slothlet_lazy.mjs +0 -594
  150. package/docs/API-RULES-CONDITIONS.md +0 -712
  151. package/types/dist/lib/engine/slothlet_child.d.mts +0 -2
  152. package/types/dist/lib/engine/slothlet_child.d.mts.map +0 -1
  153. package/types/dist/lib/engine/slothlet_engine.d.mts +0 -31
  154. package/types/dist/lib/engine/slothlet_engine.d.mts.map +0 -1
  155. package/types/dist/lib/engine/slothlet_esm.d.mts +0 -19
  156. package/types/dist/lib/engine/slothlet_esm.d.mts.map +0 -1
  157. package/types/dist/lib/engine/slothlet_helpers.d.mts +0 -25
  158. package/types/dist/lib/engine/slothlet_helpers.d.mts.map +0 -1
  159. package/types/dist/lib/engine/slothlet_worker.d.mts +0 -2
  160. package/types/dist/lib/engine/slothlet_worker.d.mts.map +0 -1
  161. package/types/dist/lib/helpers/als-eventemitter.d.mts +0 -56
  162. package/types/dist/lib/helpers/als-eventemitter.d.mts.map +0 -1
  163. package/types/dist/lib/helpers/api_builder/add_api.d.mts +0 -102
  164. package/types/dist/lib/helpers/api_builder/add_api.d.mts.map +0 -1
  165. package/types/dist/lib/helpers/api_builder/analysis.d.mts +0 -189
  166. package/types/dist/lib/helpers/api_builder/analysis.d.mts.map +0 -1
  167. package/types/dist/lib/helpers/api_builder/construction.d.mts +0 -107
  168. package/types/dist/lib/helpers/api_builder/construction.d.mts.map +0 -1
  169. package/types/dist/lib/helpers/api_builder/decisions.d.mts +0 -213
  170. package/types/dist/lib/helpers/api_builder/decisions.d.mts.map +0 -1
  171. package/types/dist/lib/helpers/api_builder/metadata.d.mts +0 -99
  172. package/types/dist/lib/helpers/api_builder/metadata.d.mts.map +0 -1
  173. package/types/dist/lib/helpers/api_builder.d.mts +0 -6
  174. package/types/dist/lib/helpers/api_builder.d.mts.map +0 -1
  175. package/types/dist/lib/helpers/auto-wrap.d.mts +0 -49
  176. package/types/dist/lib/helpers/auto-wrap.d.mts.map +0 -1
  177. package/types/dist/lib/helpers/hooks.d.mts +0 -342
  178. package/types/dist/lib/helpers/hooks.d.mts.map +0 -1
  179. package/types/dist/lib/helpers/instance-manager.d.mts +0 -41
  180. package/types/dist/lib/helpers/instance-manager.d.mts.map +0 -1
  181. package/types/dist/lib/helpers/metadata-api.d.mts +0 -132
  182. package/types/dist/lib/helpers/metadata-api.d.mts.map +0 -1
  183. package/types/dist/lib/helpers/multidefault.d.mts +0 -90
  184. package/types/dist/lib/helpers/multidefault.d.mts.map +0 -1
  185. package/types/dist/lib/modes/slothlet_eager.d.mts +0 -65
  186. package/types/dist/lib/modes/slothlet_eager.d.mts.map +0 -1
  187. package/types/dist/lib/modes/slothlet_lazy.d.mts +0 -31
  188. package/types/dist/lib/modes/slothlet_lazy.d.mts.map +0 -1
  189. package/types/index.d.mts.map +0 -1
@@ -1,495 +0,0 @@
1
- /*
2
- Copyright 2026 CLDMV/Shinrai
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */
16
-
17
-
18
-
19
-
20
-
21
- import fs from "node:fs/promises";
22
- import path from "node:path";
23
- import { types as utilTypes } from "node:util";
24
- import { sanitizePathName } from "@cldmv/slothlet/helpers/sanitize";
25
- import { multidefault_analyzeModules } from "@cldmv/slothlet/helpers/multidefault";
26
- import { analyzeModule, processModuleFromAnalysis } from "@cldmv/slothlet/helpers/api_builder/analysis";
27
- import { processModuleForAPI, buildCategoryDecisions } from "@cldmv/slothlet/helpers/api_builder/decisions";
28
-
29
-
30
-
31
-
32
-
33
-
34
- export function toapiPathKey(name, sanitizeConfig = {}) {
35
- return sanitizePathName(name, sanitizeConfig);
36
- }
37
-
38
-
39
- export function shouldIncludeFile(entry) {
40
-
41
- if (!entry.isFile()) return false;
42
-
43
- if (!(entry.name.endsWith(".mjs") || entry.name.endsWith(".cjs") || entry.name.endsWith(".js"))) return false;
44
-
45
- if (entry.name.startsWith(".")) return false;
46
-
47
- if (entry.name.startsWith("__slothlet_")) return false;
48
- return true;
49
- }
50
-
51
-
52
-
53
-
54
-
55
-
56
- export async function buildCategoryStructure(categoryPath, options = {}) {
57
- const { currentDepth = 0, maxDepth = Infinity, mode = "eager", subdirHandler, instance } = options;
58
-
59
- if (!instance || typeof instance._toapiPathKey !== "function") {
60
- throw new Error("buildCategoryStructure requires a valid slothlet instance");
61
- }
62
-
63
-
64
- const decisions = await buildCategoryDecisions(categoryPath, {
65
- currentDepth,
66
- maxDepth,
67
- mode,
68
- subdirHandler,
69
- instance,
70
- existingApi: options.existingApi
71
- });
72
-
73
-
74
- if (decisions.type === "single-file") {
75
- const { singleFile } = decisions;
76
- const { mod, moduleName } = singleFile;
77
-
78
-
79
- if (decisions.shouldFlatten) {
80
- switch (decisions.flattenType) {
81
- case "function-folder-match":
82
- case "default-function":
83
- try {
84
- Object.defineProperty(mod, "name", { value: decisions.preferredName, configurable: true });
85
- } catch {
86
-
87
- }
88
- return mod;
89
-
90
- case "default-export-flatten":
91
-
92
- return mod;
93
-
94
- case "object-auto-flatten":
95
-
96
- return mod[decisions.preferredName];
97
-
98
- case "parent-level-flatten": {
99
-
100
- const exportValue = mod[Object.keys(mod).filter((k) => k !== "default")[0]];
101
- return { [decisions.preferredName]: exportValue };
102
- }
103
-
104
- case "filename-folder-match-flatten":
105
-
106
- return mod;
107
- }
108
- }
109
-
110
-
111
- if (decisions.preferredName && decisions.preferredName !== moduleName) {
112
- return { [decisions.preferredName]: mod };
113
- }
114
-
115
-
116
- return { [moduleName]: mod };
117
- }
118
-
119
-
120
- const categoryModules = {};
121
- const { categoryName, processedModules, subdirectoryDecisions } = decisions;
122
-
123
-
124
- for (const moduleDecision of processedModules) {
125
- const { moduleName, mod, type, apiPathKey, shouldFlatten, flattenType, specialHandling, processedExports } = moduleDecision;
126
-
127
- if (specialHandling === "category-merge") {
128
-
129
- if (Object.prototype.hasOwnProperty.call(mod, categoryName) && typeof mod[categoryName] === "object" && mod[categoryName] !== null) {
130
- Object.assign(categoryModules, mod[categoryName]);
131
- for (const [key, value] of Object.entries(mod)) {
132
- if (key !== categoryName) categoryModules[instance._toapiPathKey(key)] = value;
133
- }
134
- } else {
135
- Object.assign(categoryModules, mod);
136
- }
137
- } else if (type === "function") {
138
-
139
- if (specialHandling === "multi-default-filename") {
140
- try {
141
- Object.defineProperty(mod, "name", { value: moduleName, configurable: true });
142
- } catch {
143
-
144
- }
145
- categoryModules[moduleName] = mod;
146
- } else if (specialHandling === "prefer-function-name") {
147
- categoryModules[apiPathKey] = mod;
148
- } else {
149
-
150
- categoryModules[apiPathKey] = mod;
151
- }
152
- } else if (type === "self-referential") {
153
-
154
- categoryModules[moduleName] = mod[moduleName] || mod;
155
- } else if (type === "object") {
156
-
157
- if (specialHandling === "preferred-export-names") {
158
- Object.assign(categoryModules, processedExports);
159
- } else if (shouldFlatten) {
160
- switch (flattenType) {
161
- case "single-default-object": {
162
-
163
-
164
- let flattened;
165
-
166
-
167
- const defaultExport = mod.default;
168
- const hasNamedExports = Object.keys(mod).some((k) => k !== "default");
169
-
170
- if (hasNamedExports && defaultExport && typeof defaultExport === "object") {
171
-
172
- const isProxy = utilTypes?.isProxy?.(defaultExport) ?? false;
173
-
174
- if (isProxy) {
175
-
176
- flattened = defaultExport;
177
- let assignmentFailed = false;
178
-
179
- const failedMap = new Map();
180
-
181
-
182
- for (const [key, value] of Object.entries(mod)) {
183
- if (key !== "default") {
184
- try {
185
- flattened[key] = value;
186
- } catch (e) {
187
-
188
- assignmentFailed = true;
189
- failedMap.set(key, value);
190
- if (instance.config?.debug) {
191
- console.warn(
192
- `Could not assign '${key}' to proxy object in module '${moduleName}' at '${categoryPath}':`,
193
- e.message
194
- );
195
- }
196
- }
197
- }
198
- }
199
-
200
-
201
- if (assignmentFailed) {
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
- const originalProxy = flattened;
212
- flattened = new Proxy(originalProxy, {
213
- get(target, prop, receiver) {
214
-
215
- if (failedMap.has(prop)) return failedMap.get(prop);
216
-
217
-
218
- return Reflect.get(target, prop, receiver);
219
- },
220
- has(target, prop) {
221
-
222
- if (failedMap.has(prop)) return true;
223
- return Reflect.has(target, prop);
224
- },
225
- ownKeys(target) {
226
- const originalKeys = Reflect.ownKeys(target);
227
- const failedKeys = Array.from(failedMap.keys());
228
- return [...new Set([...originalKeys, ...failedKeys])];
229
- },
230
- getOwnPropertyDescriptor(target, prop) {
231
- if (failedMap.has(prop)) {
232
- return { configurable: true, enumerable: true, value: failedMap.get(prop) };
233
- }
234
- return Reflect.getOwnPropertyDescriptor(target, prop);
235
- }
236
- });
237
- }
238
- } else {
239
-
240
- flattened = { ...defaultExport };
241
- for (const [key, value] of Object.entries(mod)) {
242
- if (key !== "default") {
243
- flattened[key] = value;
244
- }
245
- }
246
- }
247
- } else {
248
-
249
- flattened = defaultExport;
250
- }
251
-
252
- categoryModules[apiPathKey] = flattened;
253
- break;
254
- }
255
- case "multi-default-no-default": {
256
-
257
- const moduleKeys = Object.keys(mod).filter((k) => k !== "default");
258
- for (const key of moduleKeys) {
259
- categoryModules[key] = mod[key];
260
- }
261
- break;
262
- }
263
- case "single-named-export-match":
264
-
265
- categoryModules[apiPathKey] = mod[apiPathKey];
266
- break;
267
- case "category-name-match-flatten": {
268
-
269
- const moduleKeys = Object.keys(mod).filter((k) => k !== "default");
270
- for (const key of moduleKeys) {
271
- categoryModules[key] = mod[key];
272
- }
273
- break;
274
- }
275
- }
276
- } else {
277
-
278
- categoryModules[apiPathKey] = mod;
279
- }
280
- }
281
- }
282
-
283
-
284
- for (const subDirDecision of subdirectoryDecisions) {
285
- if (subDirDecision.shouldRecurse) {
286
- const { name, path: subDirPath, apiPathKey } = subDirDecision;
287
- let subModule;
288
-
289
- if (mode === "lazy" && typeof subdirHandler === "function") {
290
- subModule = subdirHandler({
291
- subDirEntry: { name },
292
- subDirPath,
293
- key: apiPathKey,
294
- categoryModules,
295
- currentDepth,
296
- maxDepth
297
- });
298
- } else {
299
- subModule = await buildCategoryStructure(subDirPath, {
300
- currentDepth: currentDepth + 1,
301
- maxDepth,
302
- mode: "eager",
303
- instance
304
- });
305
- }
306
-
307
-
308
-
309
- if (
310
- typeof subModule === "function" &&
311
- subModule.name &&
312
- subModule.name.toLowerCase() === apiPathKey.toLowerCase() &&
313
- subModule.name !== apiPathKey
314
- ) {
315
-
316
- categoryModules[subModule.name] = subModule;
317
- } else {
318
- categoryModules[apiPathKey] = subModule;
319
- }
320
- }
321
- }
322
-
323
-
324
-
325
- let defaultFunctions = [];
326
- for (const moduleDecision of processedModules) {
327
- const { mod, type } = moduleDecision;
328
- if (type === "function" && typeof mod === "function") {
329
- defaultFunctions.push({ mod, moduleDecision });
330
- }
331
- }
332
-
333
-
334
-
335
-
336
- if (defaultFunctions.length === 1 && Object.keys(categoryModules).length === 1) {
337
- const categoryDefaultFunction = defaultFunctions[0].mod;
338
-
339
-
340
- if (categoryDefaultFunction.name !== categoryName) {
341
- try {
342
- Object.defineProperty(categoryDefaultFunction, "name", { value: categoryName, configurable: true });
343
- } catch {
344
-
345
- }
346
- }
347
-
348
- return categoryDefaultFunction;
349
- }
350
-
351
-
352
- const keys = Object.keys(categoryModules);
353
- if (keys.length === 1) {
354
- const singleKey = keys[0];
355
- if (singleKey === categoryName) {
356
- const single = categoryModules[singleKey];
357
- if (typeof single === "function") {
358
- if (single.name !== categoryName) {
359
- try {
360
- Object.defineProperty(single, "name", { value: categoryName, configurable: true });
361
- } catch {
362
-
363
- }
364
- }
365
- return single;
366
- } else if (single && typeof single === "object" && !Array.isArray(single)) {
367
- return single;
368
- }
369
- }
370
- }
371
-
372
- return categoryModules;
373
- }
374
-
375
-
376
-
377
-
378
-
379
-
380
- export async function buildRootAPI(dir, options = {}) {
381
- const { lazy = false, maxDepth = Infinity, instance } = options;
382
-
383
- if (!instance || typeof instance._shouldIncludeFile !== "function" || typeof instance._loadCategory !== "function") {
384
- throw new Error("buildRootAPI requires a valid slothlet instance");
385
- }
386
-
387
- const debug = instance.config?.debug || false;
388
-
389
- if (debug) {
390
- console.log(`[DEBUG] buildRootAPI called with dir: ${dir}, lazy: ${lazy}, maxDepth: ${maxDepth}`);
391
- }
392
-
393
- const entries = await fs.readdir(dir, { withFileTypes: true });
394
- const api = {};
395
- let rootDefaultFunction = null;
396
-
397
-
398
- const moduleFiles = entries.filter((e) => instance._shouldIncludeFile(e));
399
-
400
- if (moduleFiles.length > 0) {
401
-
402
- const analysis = await multidefault_analyzeModules(moduleFiles, dir, { debug, instance });
403
- const { hasMultipleDefaultExports, selfReferentialFiles } = analysis;
404
-
405
-
406
- for (const entry of moduleFiles) {
407
- const ext = path.extname(entry.name);
408
- const fileName = path.basename(entry.name, ext);
409
- const apiPathKey = instance._toapiPathKey(fileName);
410
-
411
- const analysis = await analyzeModule(path.join(dir, entry.name), {
412
- debug,
413
- instance
414
- });
415
- const mod = processModuleFromAnalysis(analysis, {
416
- debug,
417
- instance
418
- });
419
-
420
-
421
- processModuleForAPI({
422
- mod,
423
- fileName,
424
- apiPathKey,
425
- hasMultipleDefaultExports,
426
- isSelfReferential: selfReferentialFiles.has(fileName),
427
- api,
428
- getRootDefault: () => rootDefaultFunction,
429
- setRootDefault: (fn) => {
430
- rootDefaultFunction = fn;
431
- },
432
- context: {
433
- debug,
434
- mode: "root",
435
- totalModules: moduleFiles.length
436
- }
437
- });
438
- }
439
- }
440
-
441
-
442
- for (const entry of entries) {
443
- if (entry.isDirectory() && !entry.name.startsWith(".")) {
444
- const categoryPath = path.join(dir, entry.name);
445
-
446
- if (lazy) {
447
-
448
- api[instance._toapiPathKey(entry.name)] = await buildCategoryStructure(categoryPath, {
449
- currentDepth: 1,
450
- maxDepth,
451
- mode: "lazy",
452
- subdirHandler: (ctx) => {
453
-
454
- return instance._loadCategory(ctx.subDirPath, ctx.currentDepth, ctx.maxDepth);
455
- },
456
- instance
457
- });
458
- } else {
459
-
460
- api[instance._toapiPathKey(entry.name)] = await buildCategoryStructure(categoryPath, {
461
- currentDepth: 1,
462
- maxDepth,
463
- mode: "eager",
464
- instance
465
- });
466
- }
467
- }
468
- }
469
-
470
-
471
- let finalApi;
472
- if (debug) {
473
- console.log(`[DEBUG] Final assembly: rootDefaultFunction=${!!rootDefaultFunction}`);
474
- console.log(`[DEBUG] API object keys before final assembly:`, Object.keys(api));
475
- }
476
-
477
- if (rootDefaultFunction) {
478
-
479
- Object.assign(rootDefaultFunction, api);
480
- finalApi = rootDefaultFunction;
481
-
482
- if (debug) {
483
- console.log(`[DEBUG] Applied root contributor pattern - final API is function`);
484
- }
485
- } else {
486
-
487
- finalApi = api;
488
-
489
- if (debug) {
490
- console.log(`[DEBUG] No root function - final API is object`);
491
- }
492
- }
493
-
494
- return finalApi;
495
- }