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