@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.
- package/AGENT-USAGE.md +355 -325
- package/README.md +554 -238
- 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 +123 -117
- 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 +525 -744
- package/docs/API-RULES.md +338 -486
- package/index.cjs +4 -4
- package/index.mjs +82 -45
- package/package.json +143 -30
- 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 +184 -111
- 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 -553
- package/dist/lib/helpers/api_builder/analysis.mjs +0 -532
- package/dist/lib/helpers/api_builder/construction.mjs +0 -495
- package/dist/lib/helpers/api_builder/decisions.mjs +0 -748
- 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 -154
- package/dist/lib/modes/slothlet_lazy.mjs +0 -594
- package/docs/API-RULES-CONDITIONS.md +0 -712
- 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 -102
- 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
- package/types/index.d.mts.map +0 -1
|
@@ -0,0 +1,355 @@
|
|
|
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
|
+
import { readdir, stat } from "node:fs/promises";
|
|
21
|
+
import { join, extname, basename, resolve } from "node:path";
|
|
22
|
+
import { pathToFileURL } from "node:url";
|
|
23
|
+
import { createRequire } from "node:module";
|
|
24
|
+
import { ComponentBase } from "@cldmv/slothlet/factories/component-base";
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
export class Loader extends ComponentBase {
|
|
28
|
+
static slothletProperty = "loader";
|
|
29
|
+
|
|
30
|
+
constructor(slothlet) {
|
|
31
|
+
super(slothlet);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
async loadModule(filePath, instanceID, moduleID, cacheBust = null) {
|
|
36
|
+
try {
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
if (filePath.endsWith(".cjs")) {
|
|
40
|
+
return this.#loadCJSIsolated(filePath);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
const isTypeScript = filePath.endsWith(".ts") || filePath.endsWith(".mts");
|
|
45
|
+
const typescriptConfig = this.slothlet.config?.typescript;
|
|
46
|
+
|
|
47
|
+
let moduleUrl;
|
|
48
|
+
|
|
49
|
+
if (isTypeScript && typescriptConfig?.enabled) {
|
|
50
|
+
const mode = typescriptConfig.mode;
|
|
51
|
+
if (mode === "strict") {
|
|
52
|
+
|
|
53
|
+
if (!typescriptConfig.types?.output) {
|
|
54
|
+
throw new this.SlothletError("TS_STRICT_REQUIRES_OUTPUT", {}, null, { validationError: true });
|
|
55
|
+
}
|
|
56
|
+
if (!typescriptConfig.types?.interfaceName) {
|
|
57
|
+
throw new this.SlothletError("TS_STRICT_REQUIRES_INTERFACE_NAME", {}, null, { validationError: true });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
if (!this.slothlet._typesGenerated) {
|
|
62
|
+
const { fork } = await import("child_process");
|
|
63
|
+
const path = await import("path");
|
|
64
|
+
const { fileURLToPath } = await import("url");
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
68
|
+
const scriptPath = path.resolve(__dirname, "../../../tools/build/generate-types-worker.mjs");
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
const childConfig = JSON.stringify({
|
|
73
|
+
dir: this.slothlet.config.root || this.slothlet.config.dir,
|
|
74
|
+
mode: "eager",
|
|
75
|
+
typescript: {
|
|
76
|
+
enabled: true,
|
|
77
|
+
mode: "fast"
|
|
78
|
+
},
|
|
79
|
+
types: typescriptConfig.types
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
await new Promise((resolve, reject) => {
|
|
84
|
+
const child = fork(scriptPath, [], {
|
|
85
|
+
stdio: ["pipe", "pipe", "pipe", "ipc"],
|
|
86
|
+
env: { ...process.env, SLOTHLET_CONFIG: childConfig }
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
let errorOutput = "";
|
|
90
|
+
|
|
91
|
+
child.stderr?.on("data", (data) => {
|
|
92
|
+
errorOutput += data.toString();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
child.on("message", (msg) => {
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
if (msg.type === "success") {
|
|
99
|
+
this.slothlet._typesGenerated = true;
|
|
100
|
+
resolve();
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
} else if (msg.type === "error") {
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
reject(new this.SlothletError("TS_TYPE_GENERATION_FAILED", {}, { message: msg.error }));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
child.on("error", (error) => {
|
|
115
|
+
reject(new this.SlothletError("TS_TYPE_GENERATION_FORK_FAILED", {}, error));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
child.on("exit", (code) => {
|
|
119
|
+
if (code !== 0 && !this.slothlet._typesGenerated) {
|
|
120
|
+
reject(
|
|
121
|
+
new this.SlothletError("TS_TYPE_GENERATION_PROCESS_EXITED", { code, output: errorOutput }, null, {
|
|
122
|
+
validationError: true
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
const { transformTypeScriptStrict, createDataUrl, formatDiagnostics } = await import("@cldmv/slothlet/processors/typescript");
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
const result = await transformTypeScriptStrict(filePath, {
|
|
135
|
+
target: typescriptConfig.target,
|
|
136
|
+
module: typescriptConfig.module,
|
|
137
|
+
strict: typescriptConfig.strict,
|
|
138
|
+
typeDefinitionPath: typescriptConfig.types.output,
|
|
139
|
+
compilerOptions: typescriptConfig.compilerOptions
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
if (result.diagnostics && result.diagnostics.length > 0) {
|
|
144
|
+
|
|
145
|
+
const ts = await import("typescript");
|
|
146
|
+
const errors = formatDiagnostics(result.diagnostics, ts.default);
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
const error = new this.SlothletError("TS_TYPE_CHECK_ERRORS", { filePath, errors: errors.join("\n") }, null, {
|
|
150
|
+
validationError: true
|
|
151
|
+
});
|
|
152
|
+
error.diagnostics = result.diagnostics;
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
moduleUrl = createDataUrl(result.code);
|
|
158
|
+
} else {
|
|
159
|
+
|
|
160
|
+
const { transformTypeScript, createDataUrl } = await import("@cldmv/slothlet/processors/typescript");
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
const transformedCode = await transformTypeScript(filePath, {
|
|
164
|
+
target: typescriptConfig.target,
|
|
165
|
+
sourcemap: typescriptConfig.sourcemap
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
moduleUrl = createDataUrl(transformedCode);
|
|
170
|
+
}
|
|
171
|
+
} else {
|
|
172
|
+
|
|
173
|
+
const fileUrl = pathToFileURL(filePath).href;
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
moduleUrl = `${fileUrl}?slothlet_instance=${instanceID}`;
|
|
177
|
+
if (moduleID) {
|
|
178
|
+
moduleUrl += `&module=${moduleID}`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
if (cacheBust) {
|
|
185
|
+
moduleUrl += `&_reload=${cacheBust}`;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const module = await import(moduleUrl);
|
|
190
|
+
return module;
|
|
191
|
+
} catch (error) {
|
|
192
|
+
throw new this.SlothletError(
|
|
193
|
+
"MODULE_IMPORT_FAILED",
|
|
194
|
+
{
|
|
195
|
+
modulePath: filePath
|
|
196
|
+
},
|
|
197
|
+
error
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
#loadCJSIsolated(filePath) {
|
|
204
|
+
const requireFn = createRequire(filePath);
|
|
205
|
+
const resolved = requireFn.resolve(filePath);
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
delete requireFn.cache[resolved];
|
|
209
|
+
const exports = requireFn(resolved);
|
|
210
|
+
|
|
211
|
+
delete requireFn.cache[resolved];
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
const namespace = { default: exports };
|
|
217
|
+
if (exports !== null && typeof exports === "object") {
|
|
218
|
+
for (const key of Object.keys(exports)) {
|
|
219
|
+
if (key !== "default") {
|
|
220
|
+
namespace[key] = exports[key];
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return namespace;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
async scanDirectory(dir, options = {}) {
|
|
229
|
+
|
|
230
|
+
const typescriptConfig = this.slothlet.config?.typescript;
|
|
231
|
+
const defaultExtensions = [".mjs", ".cjs", ".js"];
|
|
232
|
+
const typescriptExtensions = typescriptConfig?.enabled ? [".ts", ".mts"] : [];
|
|
233
|
+
const allExtensions = [...defaultExtensions, ...typescriptExtensions];
|
|
234
|
+
|
|
235
|
+
const {
|
|
236
|
+
recursive = true,
|
|
237
|
+
extensions = allExtensions,
|
|
238
|
+
isRootScan = true,
|
|
239
|
+
currentDepth = 0,
|
|
240
|
+
maxDepth = Infinity,
|
|
241
|
+
fileFilter = null
|
|
242
|
+
} = options;
|
|
243
|
+
|
|
244
|
+
try {
|
|
245
|
+
await stat(dir);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
throw new this.SlothletError(
|
|
248
|
+
"INVALID_DIRECTORY",
|
|
249
|
+
{
|
|
250
|
+
dir
|
|
251
|
+
},
|
|
252
|
+
error
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const structure = {
|
|
257
|
+
files: [],
|
|
258
|
+
directories: []
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
262
|
+
|
|
263
|
+
for (const entry of entries) {
|
|
264
|
+
const fullPath = join(dir, entry.name);
|
|
265
|
+
|
|
266
|
+
if (entry.isDirectory()) {
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
if (fileFilter) {
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
if (recursive && currentDepth < maxDepth) {
|
|
275
|
+
const subStructure = await this.scanDirectory(fullPath, { ...options, isRootScan: false, currentDepth: currentDepth + 1 });
|
|
276
|
+
structure.directories.push({
|
|
277
|
+
path: fullPath,
|
|
278
|
+
name: entry.name,
|
|
279
|
+
children: subStructure
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
} else if (entry.isFile()) {
|
|
283
|
+
const ext = extname(entry.name);
|
|
284
|
+
if (extensions.includes(ext)) {
|
|
285
|
+
|
|
286
|
+
if (entry.name.startsWith("__")) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
if (fileFilter && !fileFilter(entry.name)) {
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const nameWithoutExt = basename(entry.name, ext);
|
|
296
|
+
structure.files.push({
|
|
297
|
+
path: fullPath,
|
|
298
|
+
name: nameWithoutExt,
|
|
299
|
+
fullName: entry.name,
|
|
300
|
+
moduleID: this.slothlet.helpers.sanitize.getModuleId(fullPath, dir)
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
if (isRootScan && structure.files.length === 0 && structure.directories.length === 0) {
|
|
308
|
+
new this.SlothletWarning("WARN_DIRECTORY_EMPTY", {
|
|
309
|
+
dir,
|
|
310
|
+
resolvedPath: resolve(dir)
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return structure;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
extractExports(module) {
|
|
319
|
+
const exports = {};
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
if (module.default !== undefined) {
|
|
323
|
+
exports.default = module.default;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
for (const key of Object.keys(module)) {
|
|
328
|
+
if (key !== "default" && key !== "module.exports" && typeof key === "string") {
|
|
329
|
+
exports[key] = module[key];
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
if (exports.default && typeof exports.default === "object" && exports.default !== null && "default" in exports.default) {
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
const rootNamedKeys = Object.keys(exports).filter((k) => k !== "default" && k !== "module.exports");
|
|
341
|
+
const defaultKeys = Object.keys(exports.default).filter((k) => k !== "default");
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
const isCJSPattern = rootNamedKeys.every((k) => k in exports.default);
|
|
345
|
+
|
|
346
|
+
if (isCJSPattern && defaultKeys.length > 0) {
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
exports.default = exports.default.default;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return exports;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
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 "fs";
|
|
22
|
+
import path from "path";
|
|
23
|
+
import { SlothletError } from "@cldmv/slothlet/errors";
|
|
24
|
+
|
|
25
|
+
let typescriptInstance = null;
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
async function getTypeScript() {
|
|
29
|
+
if (!typescriptInstance) {
|
|
30
|
+
try {
|
|
31
|
+
typescriptInstance = await import("typescript");
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
} catch (error) {
|
|
36
|
+
throw new SlothletError("TYPESCRIPT_NOT_INSTALLED", { feature: "type-generation" }, error);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
return typescriptInstance;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
export async function generateTypes(api, options) {
|
|
45
|
+
const ts = await getTypeScript();
|
|
46
|
+
|
|
47
|
+
if (!options.output) {
|
|
48
|
+
throw new SlothletError("INVALID_CONFIG", {
|
|
49
|
+
option: "types.output",
|
|
50
|
+
expected: "a string output path",
|
|
51
|
+
value: options.output,
|
|
52
|
+
hint: "Provide a string output path for the generated .d.ts file, e.g. './types/api.d.ts'.",
|
|
53
|
+
validationError: true
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!options.interfaceName) {
|
|
58
|
+
throw new SlothletError("INVALID_CONFIG", {
|
|
59
|
+
option: "types.interfaceName",
|
|
60
|
+
expected: "a string interface name",
|
|
61
|
+
value: options.interfaceName,
|
|
62
|
+
hint: "Provide a string interface name for the generated TypeScript interface, e.g. 'SlothletAPI'.",
|
|
63
|
+
validationError: true
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
const nodes = traverseAPI(api);
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
for (const node of nodes) {
|
|
72
|
+
if (node.metadata?.filePath) {
|
|
73
|
+
node.typeInfo = await extractTypesFromFile(node.metadata.filePath, ts);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
const declaration = generateDeclaration(nodes, options);
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
const outputPath = path.resolve(options.output);
|
|
82
|
+
const outputDir = path.dirname(outputPath);
|
|
83
|
+
|
|
84
|
+
if (!fs.existsSync(outputDir)) {
|
|
85
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
fs.writeFileSync(outputPath, declaration, "utf8");
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
output: declaration,
|
|
92
|
+
filePath: outputPath
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
function traverseAPI(api, currentPath = [], visited = new Set()) {
|
|
98
|
+
const nodes = [];
|
|
99
|
+
|
|
100
|
+
if (!api || typeof api !== "object") {
|
|
101
|
+
return nodes;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
if (visited.has(api)) {
|
|
106
|
+
return nodes;
|
|
107
|
+
}
|
|
108
|
+
visited.add(api);
|
|
109
|
+
|
|
110
|
+
for (const [key, value] of Object.entries(api)) {
|
|
111
|
+
|
|
112
|
+
if (key.startsWith("_") || key.startsWith("__") || key === "slothlet" || key === "shutdown" || key === "destroy") {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const nodePath = [...currentPath, key];
|
|
117
|
+
const metadata = value?.__metadata;
|
|
118
|
+
|
|
119
|
+
if (typeof value === "function") {
|
|
120
|
+
nodes.push({
|
|
121
|
+
type: "function",
|
|
122
|
+
path: nodePath,
|
|
123
|
+
value,
|
|
124
|
+
metadata
|
|
125
|
+
});
|
|
126
|
+
} else if (typeof value === "object" && value !== null) {
|
|
127
|
+
nodes.push({
|
|
128
|
+
type: "object",
|
|
129
|
+
path: nodePath,
|
|
130
|
+
value,
|
|
131
|
+
metadata
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
const childNodes = traverseAPI(value, nodePath, visited);
|
|
136
|
+
nodes.push(...childNodes);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return nodes;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
async function extractTypesFromFile(filePath, ts) {
|
|
145
|
+
try {
|
|
146
|
+
const source = fs.readFileSync(filePath, "utf8");
|
|
147
|
+
const sourceFile = ts.createSourceFile(filePath, source, ts.ScriptTarget.Latest, true);
|
|
148
|
+
|
|
149
|
+
const exports = [];
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
function visit(node) {
|
|
153
|
+
|
|
154
|
+
if (ts.isFunctionDeclaration(node) && node.name) {
|
|
155
|
+
const hasExport = node.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
if (hasExport) {
|
|
159
|
+
exports.push({
|
|
160
|
+
name: node.name.text,
|
|
161
|
+
type: "function",
|
|
162
|
+
signature: extractFunctionSignature(node, source, ts)
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
if (ts.isVariableStatement(node)) {
|
|
169
|
+
const hasExport = node.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
170
|
+
if (hasExport) {
|
|
171
|
+
for (const decl of node.declarationList.declarations) {
|
|
172
|
+
if (!decl.name || !ts.isIdentifier(decl.name)) continue;
|
|
173
|
+
const init = decl.initializer;
|
|
174
|
+
if (!init) continue;
|
|
175
|
+
if (ts.isArrowFunction(init) || ts.isFunctionExpression(init)) {
|
|
176
|
+
exports.push({
|
|
177
|
+
name: decl.name.text,
|
|
178
|
+
type: "function",
|
|
179
|
+
signature: extractFunctionSignature(init, source, ts)
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
ts.forEachChild(node, visit);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
visit(sourceFile);
|
|
190
|
+
|
|
191
|
+
return { exports, sourceFile };
|
|
192
|
+
} catch (____error) {
|
|
193
|
+
|
|
194
|
+
return { exports: [] };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
function extractFunctionSignature(node, ____source, ____ts) {
|
|
200
|
+
const params = node.parameters
|
|
201
|
+
.map((p) => {
|
|
202
|
+
const name = p.name.getText(node.getSourceFile());
|
|
203
|
+
const type = p.type ? p.type.getText(node.getSourceFile()) : "any";
|
|
204
|
+
return `${name}: ${type}`;
|
|
205
|
+
})
|
|
206
|
+
.join(", ");
|
|
207
|
+
|
|
208
|
+
const returnType = node.type ? node.type.getText(node.getSourceFile()) : "any";
|
|
209
|
+
|
|
210
|
+
return `(${params}): ${returnType}`;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
function generateDeclaration(nodes, options) {
|
|
215
|
+
const interfaceName = options.interfaceName;
|
|
216
|
+
const lines = [];
|
|
217
|
+
|
|
218
|
+
lines.push("");
|
|
219
|
+
lines.push("");
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
const structure = {};
|
|
223
|
+
|
|
224
|
+
for (const node of nodes) {
|
|
225
|
+
if (node.type === "function") {
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
const fnName = node.path[node.path.length - 1];
|
|
230
|
+
const exportInfo = node.typeInfo?.exports?.find((e) => e.name === fnName);
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
const signature = exportInfo?.signature ?? "(...args: any[]): any";
|
|
234
|
+
setNestedProperty(structure, node.path, { type: "function", signature });
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
lines.push(`export interface ${interfaceName} {`);
|
|
242
|
+
generateInterfaceContent(structure, lines, 1);
|
|
243
|
+
lines.push("}");
|
|
244
|
+
lines.push("");
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
lines.push(`declare const self: ${interfaceName};`);
|
|
248
|
+
lines.push("");
|
|
249
|
+
|
|
250
|
+
return lines.join("\n");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
function setNestedProperty(obj, path, value) {
|
|
255
|
+
let current = obj;
|
|
256
|
+
|
|
257
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
258
|
+
const key = path[i];
|
|
259
|
+
if (!current[key]) {
|
|
260
|
+
current[key] = {};
|
|
261
|
+
}
|
|
262
|
+
current = current[key];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const lastKey = path[path.length - 1];
|
|
266
|
+
current[lastKey] = value;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
function generateInterfaceContent(structure, lines, indent) {
|
|
271
|
+
const indentation = "\t".repeat(indent);
|
|
272
|
+
|
|
273
|
+
for (const [key, value] of Object.entries(structure)) {
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
if (value.type === "function") {
|
|
277
|
+
lines.push(`${indentation}${key}${value.signature};`);
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
} else if (typeof value === "object" && value !== null) {
|
|
282
|
+
|
|
283
|
+
lines.push(`${indentation}${key}: {`);
|
|
284
|
+
generateInterfaceContent(value, lines, indent + 1);
|
|
285
|
+
lines.push(`${indentation}};`);
|
|
286
|
+
} else {
|
|
287
|
+
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
}
|
|
291
|
+
}
|