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