@cldmv/slothlet 2.4.3 → 2.5.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/README.md +127 -6
- package/dist/lib/helpers/api_builder.mjs +1446 -0
- package/dist/lib/helpers/multidefault.mjs +197 -0
- package/dist/lib/modes/slothlet_eager.mjs +53 -141
- package/dist/lib/modes/slothlet_lazy.mjs +86 -132
- package/dist/slothlet.mjs +191 -376
- package/package.json +2 -1
- package/types/dist/lib/helpers/api_builder.d.mts +446 -0
- package/types/dist/lib/helpers/api_builder.d.mts.map +1 -0
- package/types/dist/lib/helpers/multidefault.d.mts +85 -0
- package/types/dist/lib/helpers/multidefault.d.mts.map +1 -0
- package/types/dist/lib/modes/slothlet_eager.d.mts +3 -4
- package/types/dist/lib/modes/slothlet_eager.d.mts.map +1 -1
- package/types/dist/lib/modes/slothlet_lazy.d.mts +3 -4
- package/types/dist/lib/modes/slothlet_lazy.d.mts.map +1 -1
- package/types/dist/slothlet.d.mts.map +1 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 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
|
+
import path from "path";
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async function multidefault_analyzeModules(moduleFiles, baseDir, debug = false) {
|
|
23
|
+
const selfReferentialFiles = new Set();
|
|
24
|
+
const rawModuleCache = new Map();
|
|
25
|
+
const defaultExportFiles = [];
|
|
26
|
+
let totalDefaultExports = 0;
|
|
27
|
+
|
|
28
|
+
if (debug) {
|
|
29
|
+
console.log(`[DEBUG] multidefault_analyzeModules: Processing ${moduleFiles.length} files in ${baseDir}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
for (const file of moduleFiles) {
|
|
34
|
+
const moduleExt = path.extname(file.name);
|
|
35
|
+
const fileName = path.basename(file.name, moduleExt);
|
|
36
|
+
const moduleFilePath = path.resolve(baseDir, file.name);
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
const rawImport = await import(`file://${moduleFilePath.replace(/\\/g, "/")}`);
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
const isCjsFile = moduleExt === ".cjs";
|
|
43
|
+
const rawMod = isCjsFile ? rawImport.default : rawImport;
|
|
44
|
+
rawModuleCache.set(file.name, rawMod);
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
const hasRealDefault = rawMod && "default" in rawMod;
|
|
48
|
+
if (hasRealDefault) {
|
|
49
|
+
totalDefaultExports++;
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
const isSelfReferential = multidefault_isSelfReferential(rawMod);
|
|
53
|
+
|
|
54
|
+
if (debug) {
|
|
55
|
+
console.log(`[DEBUG] multidefault_analyzeModules: Checking ${file.name}`);
|
|
56
|
+
console.log(`[DEBUG] - fileName: ${fileName}`);
|
|
57
|
+
console.log(`[DEBUG] - has default: ${hasRealDefault}`);
|
|
58
|
+
console.log(`[DEBUG] - isSelfReferential: ${isSelfReferential}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!isSelfReferential) {
|
|
62
|
+
|
|
63
|
+
defaultExportFiles.push({ fileName, rawModule: rawMod });
|
|
64
|
+
if (debug) {
|
|
65
|
+
console.log(`[DEBUG] Found default export in ${file.name} (non-self-referential)`);
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
selfReferentialFiles.add(fileName);
|
|
69
|
+
if (debug) {
|
|
70
|
+
console.log(`[DEBUG] Skipped ${file.name} - self-referential default export`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
} else if (debug) {
|
|
74
|
+
console.log(`[DEBUG] multidefault_analyzeModules: Checking ${file.name}`);
|
|
75
|
+
console.log(`[DEBUG] - fileName: ${fileName}`);
|
|
76
|
+
console.log(`[DEBUG] - has default: ${hasRealDefault}`);
|
|
77
|
+
if (isCjsFile) {
|
|
78
|
+
console.log(`[DEBUG] - CJS file unwrapped from Node.js wrapper`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
const hasMultipleDefaultExports = totalDefaultExports > 1;
|
|
85
|
+
|
|
86
|
+
if (debug) {
|
|
87
|
+
console.log(`[DEBUG] multidefault_analyzeModules results:`);
|
|
88
|
+
console.log(`[DEBUG] - selfReferentialFiles:`, Array.from(selfReferentialFiles));
|
|
89
|
+
console.log(`[DEBUG] - totalDefaultExports:`, totalDefaultExports);
|
|
90
|
+
console.log(`[DEBUG] - hasMultipleDefaultExports:`, hasMultipleDefaultExports);
|
|
91
|
+
console.log(`[DEBUG] - defaultExportFiles count:`, defaultExportFiles.length);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
totalDefaultExports,
|
|
96
|
+
hasMultipleDefaultExports,
|
|
97
|
+
selfReferentialFiles,
|
|
98
|
+
rawModuleCache,
|
|
99
|
+
defaultExportFiles
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
function multidefault_isSelfReferential(rawModule) {
|
|
105
|
+
if (!rawModule || !("default" in rawModule)) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
return Object.entries(rawModule).some(([key, value]) => key !== "default" && value === rawModule.default);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
function multidefault_getFlatteningDecision(options) {
|
|
115
|
+
const {
|
|
116
|
+
hasMultipleDefaultExports,
|
|
117
|
+
moduleHasDefault,
|
|
118
|
+
isSelfReferential,
|
|
119
|
+
moduleKeys,
|
|
120
|
+
apiPathKey,
|
|
121
|
+
totalModuleCount,
|
|
122
|
+
debug = false
|
|
123
|
+
} = options;
|
|
124
|
+
|
|
125
|
+
if (debug) {
|
|
126
|
+
console.log(`[DEBUG] multidefault_getFlatteningDecision: Analyzing flattening for ${apiPathKey}`);
|
|
127
|
+
console.log(`[DEBUG] - hasMultipleDefaultExports: ${hasMultipleDefaultExports}`);
|
|
128
|
+
console.log(`[DEBUG] - moduleHasDefault: ${moduleHasDefault}`);
|
|
129
|
+
console.log(`[DEBUG] - isSelfReferential: ${isSelfReferential}`);
|
|
130
|
+
console.log(`[DEBUG] - moduleKeys: [${moduleKeys.join(", ")}]`);
|
|
131
|
+
console.log(`[DEBUG] - totalModuleCount: ${totalModuleCount}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
if (isSelfReferential) {
|
|
136
|
+
return {
|
|
137
|
+
shouldFlatten: false,
|
|
138
|
+
flattenToRoot: false,
|
|
139
|
+
preserveAsNamespace: true,
|
|
140
|
+
reason: "self-referential default export"
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
if (hasMultipleDefaultExports) {
|
|
146
|
+
if (moduleHasDefault) {
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
shouldFlatten: false,
|
|
150
|
+
flattenToRoot: false,
|
|
151
|
+
preserveAsNamespace: true,
|
|
152
|
+
reason: "multi-default context with default export"
|
|
153
|
+
};
|
|
154
|
+
} else {
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
shouldFlatten: true,
|
|
158
|
+
flattenToRoot: true,
|
|
159
|
+
preserveAsNamespace: false,
|
|
160
|
+
reason: "multi-default context without default export"
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
if (moduleKeys.length === 1 && moduleKeys[0] === apiPathKey) {
|
|
168
|
+
return {
|
|
169
|
+
shouldFlatten: true,
|
|
170
|
+
flattenToRoot: false,
|
|
171
|
+
preserveAsNamespace: false,
|
|
172
|
+
reason: "single named export matching filename"
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
shouldFlatten: false,
|
|
191
|
+
flattenToRoot: false,
|
|
192
|
+
preserveAsNamespace: true,
|
|
193
|
+
reason: "default namespace preservation"
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export { multidefault_analyzeModules, multidefault_isSelfReferential, multidefault_getFlatteningDecision };
|
|
@@ -27,160 +27,76 @@ import path from "node:path";
|
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
export async function create(dir, rootLevel = true, maxDepth = Infinity, currentDepth = 0) {
|
|
30
|
+
export async function create(dir, maxDepth = Infinity, currentDepth = 0) {
|
|
32
31
|
|
|
32
|
+
const { processModuleForAPI } = await import("@cldmv/slothlet/helpers/api_builder");
|
|
33
33
|
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
34
34
|
const api = {};
|
|
35
|
-
|
|
36
|
-
const rootNamedExports = {};
|
|
37
|
-
|
|
38
35
|
let rootDefaultFunction = null;
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
for (const entry of moduleFiles) {
|
|
47
|
-
const ext = path.extname(entry.name);
|
|
48
|
-
const fileName = path.basename(entry.name, ext);
|
|
49
|
-
const mod = await this._loadSingleModule(path.join(dir, entry.name), true);
|
|
37
|
+
|
|
38
|
+
const moduleFiles = entries.filter((e) => this._shouldIncludeFile(e));
|
|
39
|
+
const defaultExportFiles = [];
|
|
40
|
+
|
|
41
|
+
const { multidefault_analyzeModules } = await import("@cldmv/slothlet/helpers/multidefault");
|
|
42
|
+
const analysis = await multidefault_analyzeModules(moduleFiles, dir, this.config.debug);
|
|
50
43
|
|
|
51
|
-
|
|
52
|
-
console.log(`[DEBUG] First pass - ${fileName}: hasDefault=${mod && "default" in mod}`);
|
|
53
|
-
}
|
|
44
|
+
const { totalDefaultExports, hasMultipleDefaultExports, selfReferentialFiles, defaultExportFiles: analysisDefaults } = analysis;
|
|
54
45
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
console.log(`[DEBUG] Added ${fileName} to defaultExportFiles (non-self-referential)`);
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
if (this.config.debug) {
|
|
66
|
-
console.log(`[DEBUG] Skipped ${fileName} - self-referential default export`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
46
|
+
|
|
47
|
+
defaultExportFiles.length = 0;
|
|
48
|
+
for (const { fileName } of analysisDefaults) {
|
|
49
|
+
const entry = moduleFiles.find((f) => path.basename(f.name, path.extname(f.name)) === fileName);
|
|
50
|
+
if (entry) {
|
|
51
|
+
const mod = await this._loadSingleModule(path.join(dir, entry.name));
|
|
52
|
+
defaultExportFiles.push({ entry, fileName, mod });
|
|
70
53
|
}
|
|
71
|
-
|
|
54
|
+
}
|
|
72
55
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
)
|
|
81
|
-
|
|
56
|
+
if (this.config.debug) {
|
|
57
|
+
console.log(`[DEBUG] Eager mode: Using shared multidefault utility results`);
|
|
58
|
+
console.log(
|
|
59
|
+
`[DEBUG] Detection result: ${totalDefaultExports} total defaults (${defaultExportFiles.length} non-self-referential + ${selfReferentialFiles.size} self-referential), hasMultipleDefaultExports=${hasMultipleDefaultExports}`
|
|
60
|
+
);
|
|
61
|
+
console.log(
|
|
62
|
+
`[DEBUG] Default export files:`,
|
|
63
|
+
defaultExportFiles.map((f) => f.fileName)
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
for (const entry of moduleFiles) {
|
|
69
|
+
const ext = path.extname(entry.name);
|
|
70
|
+
const fileName = path.basename(entry.name, ext);
|
|
71
|
+
const apiPathKey = this._toapiPathKey(fileName);
|
|
72
|
+
const mod = await this._loadSingleModule(path.join(dir, entry.name));
|
|
82
73
|
|
|
83
74
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (key !== "default") {
|
|
102
|
-
api[apiKey][key] = value;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (this.config.debug) {
|
|
107
|
-
console.log(`[DEBUG] Multi-default in eager mode: using filename '${apiKey}' for default export`);
|
|
108
|
-
}
|
|
109
|
-
} else {
|
|
110
|
-
|
|
111
|
-
if (this.config.debug) {
|
|
112
|
-
console.log(
|
|
113
|
-
`[DEBUG] Processing traditional default: hasMultipleDefaultExports=${hasMultipleDefaultExports}, isSelfReferential=${isSelfReferential}, rootDefaultFunction=${!!rootDefaultFunction}`
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
if ((!hasMultipleDefaultExports || isSelfReferential) && !rootDefaultFunction) {
|
|
117
|
-
rootDefaultFunction = mod.default;
|
|
118
|
-
if (this.config.debug) {
|
|
119
|
-
console.log(`[DEBUG] Set rootDefaultFunction to:`, mod.default.name);
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
if (this.config.debug) {
|
|
123
|
-
console.log(`[DEBUG] Skipped setting rootDefaultFunction due to multi-default scenario`);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
for (const [key, value] of Object.entries(mod)) {
|
|
127
|
-
if (key !== "default") api[key] = value;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
} else {
|
|
131
|
-
|
|
132
|
-
if (this.config.debug) {
|
|
133
|
-
console.log(`[DEBUG] Processing non-function or named-only exports for ${fileName}`);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (isSelfReferential) {
|
|
137
|
-
|
|
138
|
-
if (this.config.debug) {
|
|
139
|
-
console.log(`[DEBUG] Self-referential ${fileName}: preserving as namespace`);
|
|
140
|
-
}
|
|
141
|
-
api[apiKey] = mod.default;
|
|
142
|
-
} else if (hasMultipleDefaultExports) {
|
|
143
|
-
|
|
144
|
-
if (this.config.debug) {
|
|
145
|
-
console.log(`[DEBUG] Multi-default context: flattening ${fileName} exports to root`);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const eager_addNamedExportsToApi = (exports, api, rootNamedExports) => {
|
|
149
|
-
for (const [key, value] of Object.entries(exports)) {
|
|
150
|
-
if (key !== "default") {
|
|
151
|
-
api[key] = value;
|
|
152
|
-
rootNamedExports[key] = value;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
if (mod && "default" in mod) {
|
|
158
|
-
|
|
159
|
-
api[apiKey] = mod.default;
|
|
160
|
-
|
|
161
|
-
eager_addNamedExportsToApi(mod, api, rootNamedExports);
|
|
162
|
-
} else {
|
|
163
|
-
|
|
164
|
-
eager_addNamedExportsToApi(mod, api, rootNamedExports);
|
|
165
|
-
}
|
|
166
|
-
} else {
|
|
167
|
-
|
|
168
|
-
if (this.config.debug) {
|
|
169
|
-
console.log(`[DEBUG] Traditional context: preserving ${fileName} as namespace`);
|
|
170
|
-
}
|
|
171
|
-
api[apiKey] = mod;
|
|
172
|
-
for (const [key, value] of Object.entries(mod)) {
|
|
173
|
-
rootNamedExports[key] = value;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
75
|
+
const isSelfReferential = selfReferentialFiles.has(fileName);
|
|
76
|
+
|
|
77
|
+
processModuleForAPI({
|
|
78
|
+
mod,
|
|
79
|
+
fileName,
|
|
80
|
+
apiPathKey,
|
|
81
|
+
hasMultipleDefaultExports,
|
|
82
|
+
isSelfReferential,
|
|
83
|
+
api,
|
|
84
|
+
getRootDefault: () => rootDefaultFunction,
|
|
85
|
+
setRootDefault: (fn) => {
|
|
86
|
+
rootDefaultFunction = fn;
|
|
87
|
+
},
|
|
88
|
+
context: {
|
|
89
|
+
debug: this.config.debug,
|
|
90
|
+
mode: "root",
|
|
91
|
+
totalModules: moduleFiles.length
|
|
176
92
|
}
|
|
177
|
-
}
|
|
93
|
+
});
|
|
178
94
|
}
|
|
179
95
|
|
|
180
96
|
for (const entry of entries) {
|
|
181
97
|
if (entry.isDirectory() && !entry.name.startsWith(".") && currentDepth < maxDepth) {
|
|
182
98
|
const categoryPath = path.join(dir, entry.name);
|
|
183
|
-
api[this.
|
|
99
|
+
api[this._toapiPathKey(entry.name)] = await this._loadCategory(categoryPath, currentDepth + 1, maxDepth);
|
|
184
100
|
}
|
|
185
101
|
}
|
|
186
102
|
|
|
@@ -202,9 +118,5 @@ export async function create(dir, rootLevel = true, maxDepth = Infinity, current
|
|
|
202
118
|
}
|
|
203
119
|
}
|
|
204
120
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
121
|
return finalApi;
|
|
210
122
|
}
|
|
@@ -18,159 +18,86 @@
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
import fs from "node:fs/promises";
|
|
21
|
+
import { readdirSync } from "node:fs";
|
|
21
22
|
import path from "node:path";
|
|
22
23
|
import { runWithCtx } from "@cldmv/slothlet/runtime";
|
|
24
|
+
import { processModuleForAPI } from "@cldmv/slothlet/helpers/api_builder";
|
|
23
25
|
|
|
24
26
|
|
|
25
|
-
export async function create(dir,
|
|
27
|
+
export async function create(dir, maxDepth = Infinity, currentDepth = 0) {
|
|
26
28
|
const instance = this;
|
|
27
29
|
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
28
30
|
let api = {};
|
|
29
31
|
let rootDefaultFn = null;
|
|
30
32
|
|
|
31
33
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const defaultExportFiles = [];
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const selfReferentialFiles = new Set();
|
|
39
|
-
const rawModuleCache = new Map();
|
|
40
|
-
|
|
41
|
-
for (const entry of moduleFiles) {
|
|
42
|
-
const ext = path.extname(entry.name);
|
|
43
|
-
const fileName = path.basename(entry.name, ext);
|
|
44
|
-
|
|
45
|
-
if (instance.config.debug) {
|
|
46
|
-
console.log(`[DEBUG] First pass processing: ${fileName}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const modulePath = path.resolve(dir, entry.name);
|
|
51
|
-
const rawMod = await import(`file://${modulePath.replace(/\\/g, "/")}`);
|
|
52
|
-
rawModuleCache.set(entry.name, rawMod);
|
|
53
|
-
|
|
54
|
-
if (instance.config.debug && fileName === "config") {
|
|
55
|
-
console.log(`[DEBUG] First pass - raw config keys:`, Object.keys(rawMod || {}));
|
|
56
|
-
console.log(`[DEBUG] First pass - raw config has default:`, rawMod && "default" in rawMod);
|
|
57
|
-
}
|
|
34
|
+
|
|
35
|
+
const moduleFiles = entries.filter((e) => instance._shouldIncludeFile(e));
|
|
36
|
+
const defaultExportFiles = [];
|
|
58
37
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
38
|
+
|
|
39
|
+
const { multidefault_analyzeModules } = await import("@cldmv/slothlet/helpers/multidefault");
|
|
40
|
+
const analysis = await multidefault_analyzeModules(moduleFiles, dir, instance.config.debug);
|
|
62
41
|
|
|
63
|
-
|
|
64
|
-
if (instance.config.debug && fileName === "config") {
|
|
65
|
-
console.log(`[DEBUG] First pass - ${fileName} self-referential check:`);
|
|
66
|
-
console.log(`[DEBUG] - rawMod.default === rawMod.config: ${rawMod.default === rawMod.config}`);
|
|
67
|
-
console.log(`[DEBUG] - isSelfReferential result: ${isSelfReferential}`);
|
|
68
|
-
}
|
|
42
|
+
const { totalDefaultExports, hasMultipleDefaultExports, selfReferentialFiles, defaultExportFiles: analysisDefaults } = analysis;
|
|
69
43
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
} else {
|
|
78
|
-
selfReferentialFiles.add(fileName);
|
|
79
|
-
if (instance.config.debug) {
|
|
80
|
-
console.log(`[DEBUG] Skipped ${fileName} - self-referential default export`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
44
|
+
|
|
45
|
+
defaultExportFiles.length = 0;
|
|
46
|
+
for (const { fileName } of analysisDefaults) {
|
|
47
|
+
const entry = moduleFiles.find((f) => path.basename(f.name, path.extname(f.name)) === fileName);
|
|
48
|
+
if (entry) {
|
|
49
|
+
const mod = await instance._loadSingleModule(path.join(dir, entry.name));
|
|
50
|
+
defaultExportFiles.push({ entry, fileName, mod });
|
|
84
51
|
}
|
|
52
|
+
}
|
|
85
53
|
|
|
86
|
-
|
|
54
|
+
if (instance.config.debug) {
|
|
55
|
+
console.log(`[DEBUG] Lazy mode: Using shared multidefault utility results`);
|
|
56
|
+
console.log(`[DEBUG] - totalDefaultExports: ${totalDefaultExports}`);
|
|
57
|
+
console.log(`[DEBUG] - hasMultipleDefaultExports: ${hasMultipleDefaultExports}`);
|
|
58
|
+
console.log(`[DEBUG] - selfReferentialFiles: ${Array.from(selfReferentialFiles)}`);
|
|
59
|
+
}
|
|
87
60
|
|
|
88
|
-
|
|
89
|
-
|
|
61
|
+
|
|
62
|
+
const processedModuleCache = new Map();
|
|
90
63
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
let mod = null;
|
|
98
|
-
const existingDefault = defaultExportFiles.find((def) => def.fileName === fileName);
|
|
99
|
-
if (existingDefault) {
|
|
100
|
-
mod = existingDefault.mod;
|
|
101
|
-
} else {
|
|
102
|
-
|
|
103
|
-
mod = await instance._loadSingleModule(path.join(dir, entry.name), true);
|
|
104
|
-
processedModuleCache.set(entry.name, mod);
|
|
105
|
-
}
|
|
64
|
+
for (const entry of moduleFiles) {
|
|
65
|
+
const ext = path.extname(entry.name);
|
|
66
|
+
const fileName = path.basename(entry.name, ext);
|
|
67
|
+
const apiPathKey = instance._toapiPathKey(fileName);
|
|
106
68
|
|
|
69
|
+
|
|
70
|
+
let mod = null;
|
|
71
|
+
const existingDefault = defaultExportFiles.find((def) => def.fileName === fileName);
|
|
72
|
+
if (existingDefault) {
|
|
73
|
+
mod = existingDefault.mod;
|
|
74
|
+
} else {
|
|
107
75
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (hasMultipleDefaultExports && !isSelfReferential) {
|
|
112
|
-
|
|
113
|
-
api[apiKey] = mod.default;
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
for (const [key, value] of Object.entries(mod)) {
|
|
117
|
-
if (key !== "default") {
|
|
118
|
-
api[apiKey][key] = value;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
76
|
+
mod = await instance._loadSingleModule(path.join(dir, entry.name));
|
|
77
|
+
processedModuleCache.set(entry.name, mod);
|
|
78
|
+
}
|
|
121
79
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
} else if (isSelfReferential) {
|
|
126
|
-
|
|
127
|
-
if (instance.config.debug) {
|
|
128
|
-
console.log(`[DEBUG] Self-referential default export: preserving ${fileName} as namespace`);
|
|
129
|
-
}
|
|
130
|
-
api[apiKey] = mod;
|
|
131
|
-
} else {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (!hasMultipleDefaultExports && !rootDefaultFn) {
|
|
135
|
-
rootDefaultFn = mod.default;
|
|
136
|
-
}
|
|
137
|
-
for (const [k, v] of Object.entries(mod)) {
|
|
138
|
-
if (k !== "default") api[k] = v;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
} else {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (instance.config.debug) {
|
|
145
|
-
console.log(`[DEBUG] Processing non-default exports for ${fileName}`);
|
|
146
|
-
}
|
|
80
|
+
|
|
81
|
+
const isSelfReferential = selfReferentialFiles.has(fileName);
|
|
147
82
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
} else {
|
|
166
|
-
|
|
167
|
-
if (instance.config.debug) {
|
|
168
|
-
console.log(`[DEBUG] Traditional context: preserving ${fileName} as namespace`);
|
|
169
|
-
}
|
|
170
|
-
api[apiKey] = mod;
|
|
171
|
-
}
|
|
83
|
+
|
|
84
|
+
processModuleForAPI({
|
|
85
|
+
mod,
|
|
86
|
+
fileName,
|
|
87
|
+
apiPathKey,
|
|
88
|
+
hasMultipleDefaultExports,
|
|
89
|
+
isSelfReferential,
|
|
90
|
+
api,
|
|
91
|
+
getRootDefault: () => rootDefaultFn,
|
|
92
|
+
setRootDefault: (fn) => {
|
|
93
|
+
rootDefaultFn = fn;
|
|
94
|
+
},
|
|
95
|
+
context: {
|
|
96
|
+
debug: instance.config.debug,
|
|
97
|
+
mode: "root",
|
|
98
|
+
totalModules: moduleFiles.length
|
|
172
99
|
}
|
|
173
|
-
}
|
|
100
|
+
});
|
|
174
101
|
}
|
|
175
102
|
|
|
176
103
|
|
|
@@ -182,10 +109,37 @@ export async function create(dir, rootLevel = true, maxDepth = Infinity, current
|
|
|
182
109
|
|
|
183
110
|
for (const entry of entries) {
|
|
184
111
|
if (entry.isDirectory() && !entry.name.startsWith(".") && currentDepth < maxDepth) {
|
|
185
|
-
const key = instance.
|
|
112
|
+
const key = instance._toapiPathKey(entry.name);
|
|
186
113
|
const subDirPath = path.join(dir, entry.name);
|
|
187
114
|
const parent = api;
|
|
188
115
|
const depth = 1;
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
const subEntries = readdirSync(subDirPath, { withFileTypes: true });
|
|
120
|
+
const hasFiles = subEntries.some(
|
|
121
|
+
(subEntry) =>
|
|
122
|
+
subEntry.isFile() &&
|
|
123
|
+
!subEntry.name.startsWith(".") &&
|
|
124
|
+
(subEntry.name.endsWith(".mjs") || subEntry.name.endsWith(".cjs") || subEntry.name.endsWith(".js"))
|
|
125
|
+
);
|
|
126
|
+
const hasSubdirs = subEntries.some((subEntry) => subEntry.isDirectory() && !subEntry.name.startsWith("."));
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
if (!hasFiles && !hasSubdirs) {
|
|
130
|
+
if (instance?.config?.debug) {
|
|
131
|
+
console.log(`[lazy][debug] empty folder detected during traversal: ${subDirPath} -> adding {}`);
|
|
132
|
+
}
|
|
133
|
+
parent[key] = {};
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
} catch (error) {
|
|
137
|
+
|
|
138
|
+
if (instance?.config?.debug) {
|
|
139
|
+
console.log(`[lazy][debug] error reading directory ${subDirPath}: ${error.message}, creating lazy proxy`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
189
143
|
const proxy = createFolderProxy({
|
|
190
144
|
subDirPath,
|
|
191
145
|
key,
|