@kezh/my-custom-js-converter 1.5.1 → 1.6.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/core/config/index.js +1 -0
- package/core/context/map_context.js +51 -10
- package/core/decode/index.js +156 -3
- package/core/scanf/index.js +71 -0
- package/core/util/ast.js +37 -0
- package/package.json +46 -42
- package/.claude/plan.md +0 -65
- package/CLAUDE.md +0 -50
- package/TASKS.md +0 -399
- package/core/config/index.d.ts +0 -2
- package/core/config/main_config.d.ts +0 -20
- package/core/config/map_config.d.ts +0 -15
- package/core/context/map_context.d.ts +0 -28
- package/core/decode/index.d.ts +0 -2
- package/core/encode/index.d.ts +0 -2
- package/core/scanf/index.d.ts +0 -18
- package/core/util/ast.d.ts +0 -84
- package/core/util/logger.d.ts +0 -15
- package/core/util/parent_path.d.ts +0 -5
- package/core/util/version.d.ts +0 -43
- package/test/fixtures/sample_project/config.json +0 -17
- package/test/manual_review.js +0 -288
- package/test/run_tests.js +0 -413
- package/tsconfig.json +0 -103
- package/tsconfig.tsbuildinfo +0 -1
package/core/config/index.js
CHANGED
|
@@ -59,6 +59,56 @@ class MapContext {
|
|
|
59
59
|
? path_1.default.resolve(parentRootDir, srcPath)
|
|
60
60
|
: srcPath;
|
|
61
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* 补充缺失的依赖到 mod.map
|
|
64
|
+
* 在 self.dependencies 下新增该依赖(让 importMods 生成 require),
|
|
65
|
+
* 并新增独立的 mod 条目(让 getAbsoluteSrcPathByMod 拿到源文件路径)。
|
|
66
|
+
* methods 留空数组,后续 npx encode 会自动补全。
|
|
67
|
+
* @param depName - 依赖名(如 "drawer")
|
|
68
|
+
* @param absoluteSrcPath - 依赖文件的绝对路径
|
|
69
|
+
*/
|
|
70
|
+
appendMissingDep(depName, absoluteSrcPath) {
|
|
71
|
+
this.referenceDep(depName);
|
|
72
|
+
// map[depName] = { src: <相对 parentRootDir 的路径>, dependencies: {} }
|
|
73
|
+
const parentRootDir = (0, parent_path_1.getParentRootDir)();
|
|
74
|
+
const src = parentRootDir
|
|
75
|
+
? path_1.default.relative(parentRootDir, absoluteSrcPath)
|
|
76
|
+
: absoluteSrcPath;
|
|
77
|
+
this.appendModMap(new map_config_1.Mod(src, new map_config_1.Dependencies()), false, depName);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 仅将已有 mod 条目引用到 self.dependencies(不新增/覆盖 mod 条目)。
|
|
81
|
+
* 用于 mod.map 中 mod 条目已存在(有 src)但未被 self 引用的情况:
|
|
82
|
+
* 只需让 importMods 生成 require、deleteModMethods 还原调用即可。
|
|
83
|
+
* @param depName - 依赖名(如 "drawer")
|
|
84
|
+
*/
|
|
85
|
+
referenceDep(depName) {
|
|
86
|
+
const selfMod = this.getMod("self");
|
|
87
|
+
if (selfMod) {
|
|
88
|
+
selfMod.dependencies[depName] = new map_config_1.Dependency([]);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
const mod = new map_config_1.Mod("", new map_config_1.Dependencies());
|
|
92
|
+
mod.dependencies[depName] = new map_config_1.Dependency([]);
|
|
93
|
+
this.appendModMap(mod, true);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 将当前 map 写入指定目录的 mod.map 文件
|
|
98
|
+
* @param outDir - 输出目录
|
|
99
|
+
*/
|
|
100
|
+
writeToLocalDir(outDir) {
|
|
101
|
+
const outPath = path_1.default.join(outDir, MapContext.SAVE_FILE);
|
|
102
|
+
logger.info(`write map: ${outPath}`);
|
|
103
|
+
const json = JSON.stringify(this.map, null, 4);
|
|
104
|
+
try {
|
|
105
|
+
fs_1.default.accessSync(outDir);
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
fs_1.default.mkdirSync(outDir);
|
|
109
|
+
}
|
|
110
|
+
fs_1.default.writeFileSync(outPath, json.replace(/\n/gm, os_1.EOL), "utf-8");
|
|
111
|
+
}
|
|
62
112
|
static readFromLocal(inDir) {
|
|
63
113
|
const inPath = path_1.default.join(inDir, MapContext.SAVE_FILE);
|
|
64
114
|
logger.info(`read map: ${inPath}`);
|
|
@@ -82,16 +132,7 @@ class MapContext {
|
|
|
82
132
|
this.writeToLocal();
|
|
83
133
|
}
|
|
84
134
|
writeToLocal() {
|
|
85
|
-
|
|
86
|
-
logger.info(`write map: ${outPath}`);
|
|
87
|
-
const json = JSON.stringify(this.map, null, 4);
|
|
88
|
-
try {
|
|
89
|
-
fs_1.default.accessSync(this.outDir);
|
|
90
|
-
}
|
|
91
|
-
catch (err) {
|
|
92
|
-
fs_1.default.mkdirSync(this.outDir);
|
|
93
|
-
}
|
|
94
|
-
fs_1.default.writeFileSync(outPath, json.replace(/\n/gm, os_1.EOL), "utf-8");
|
|
135
|
+
this.writeToLocalDir(this.outDir);
|
|
95
136
|
}
|
|
96
137
|
buildMapContext(inPath, entry, root) {
|
|
97
138
|
const srcAst = (0, ast_1.parseSrcAst)(inPath);
|
package/core/decode/index.js
CHANGED
|
@@ -21,12 +21,14 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
21
21
|
const os_1 = require("os");
|
|
22
22
|
const path_1 = __importDefault(require("path"));
|
|
23
23
|
const prettier_1 = __importDefault(require("prettier"));
|
|
24
|
+
const readline_1 = __importDefault(require("readline"));
|
|
24
25
|
const config_1 = require("../config");
|
|
25
26
|
const map_context_1 = require("../context/map_context");
|
|
26
27
|
const scanf_1 = require("../scanf");
|
|
27
28
|
const ast_1 = require("../util/ast");
|
|
28
29
|
const version_1 = require("../util/version");
|
|
29
30
|
const logger_1 = require("../util/logger");
|
|
31
|
+
const parent_path_1 = require("../util/parent_path");
|
|
30
32
|
const logger = (0, logger_1.createLogger)("decode");
|
|
31
33
|
(function decoding(config) {
|
|
32
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -35,15 +37,35 @@ const logger = (0, logger_1.createLogger)("decode");
|
|
|
35
37
|
logger.info(`目标目录: ${targetDirs.join(", ")}`);
|
|
36
38
|
const codeFiles = (0, scanf_1.scanfCodeFiles)(targetDirs, config.decode.file);
|
|
37
39
|
logger.info(`找到 ${codeFiles.length} 个编码文件 (${config.decode.file})`);
|
|
40
|
+
// 解析 libs 目录(用于补充缺失依赖)
|
|
41
|
+
const rootDir = (0, parent_path_1.getParentRootDir)() || config.baseDir;
|
|
42
|
+
const libDirs = (0, scanf_1.resolveLibDirs)(config.libs, config.baseDir, rootDir);
|
|
43
|
+
if (libDirs.length) {
|
|
44
|
+
logger.info(`依赖库搜索目录: ${libDirs.join(", ")}`);
|
|
45
|
+
}
|
|
46
|
+
const conflicts = [];
|
|
38
47
|
for (let i = 0; i < codeFiles.length; i++) {
|
|
39
48
|
const inPath = codeFiles[i];
|
|
40
49
|
const outPath = path_1.default.join(path_1.default.dirname(inPath), config.decode.output);
|
|
41
50
|
const settingDir = path_1.default.join(path_1.default.dirname(inPath), config.settingDir);
|
|
42
51
|
logger.step(`解码处理 [${i + 1}/${codeFiles.length}]`);
|
|
43
52
|
logger.info(`源文件: ${inPath}`);
|
|
44
|
-
yield decoding$0(inPath, outPath, config, settingDir);
|
|
53
|
+
yield decoding$0(inPath, outPath, config, settingDir, libDirs, conflicts);
|
|
45
54
|
logger.info(`输出: ${outPath}`);
|
|
46
55
|
}
|
|
56
|
+
// 批处理结束后处理重名冲突
|
|
57
|
+
if (conflicts.length) {
|
|
58
|
+
logger.step("处理重名依赖冲突");
|
|
59
|
+
const conflictFiles = yield resolveConflicts(conflicts, config);
|
|
60
|
+
// 重新 decode 已解决冲突的文件
|
|
61
|
+
for (const inPath of conflictFiles) {
|
|
62
|
+
const outPath = path_1.default.join(path_1.default.dirname(inPath), config.decode.output);
|
|
63
|
+
const settingDir = path_1.default.join(path_1.default.dirname(inPath), config.settingDir);
|
|
64
|
+
logger.step(`重新解码: ${inPath}`);
|
|
65
|
+
yield decoding$0(inPath, outPath, config, settingDir, libDirs, []);
|
|
66
|
+
logger.info(`输出: ${outPath}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
47
69
|
logger.step("完成");
|
|
48
70
|
logger.info(`共处理 ${codeFiles.length} 个文件`);
|
|
49
71
|
});
|
|
@@ -54,10 +76,12 @@ const logger = (0, logger_1.createLogger)("decode");
|
|
|
54
76
|
* @param outPath - 输入文件路径
|
|
55
77
|
* @param config - 配置
|
|
56
78
|
* @param settingDir - setting目录路径
|
|
79
|
+
* @param libDirs - 依赖库搜索目录(绝对路径)
|
|
80
|
+
* @param conflicts - 重名冲突收集列表(传入 null/空数组表示不记录冲突,如重跑时)
|
|
57
81
|
*/
|
|
58
|
-
function decoding$0(inPath, outPath, config, settingDir) {
|
|
82
|
+
function decoding$0(inPath, outPath, config, settingDir, libDirs, conflicts) {
|
|
59
83
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
const mapContext = map_context_1.MapContext.readFromLocal(
|
|
84
|
+
const mapContext = map_context_1.MapContext.readFromLocal(settingDir);
|
|
61
85
|
let src = fs_1.default.readFileSync(inPath, {
|
|
62
86
|
encoding: "utf-8"
|
|
63
87
|
});
|
|
@@ -66,6 +90,12 @@ function decoding$0(inPath, outPath, config, settingDir) {
|
|
|
66
90
|
if (!core_1.types.isNode(srcAst)) {
|
|
67
91
|
return;
|
|
68
92
|
}
|
|
93
|
+
// 补充缺失依赖:从 coded.js 提取未知 __{mod} 后缀,在 libs 目录搜索文件
|
|
94
|
+
const supplemented = supplementMissingDeps(srcAst, mapContext, libDirs, inPath, conflicts);
|
|
95
|
+
if (supplemented) {
|
|
96
|
+
// 有补充则先写回 mod.map,确保后续 importMods/deleteModMethods 能用到
|
|
97
|
+
mapContext.writeToLocalDir(settingDir);
|
|
98
|
+
}
|
|
69
99
|
(0, ast_1.importMods)(path_1.default.dirname(outPath), srcAst, mapContext, config.encode.useAlias);
|
|
70
100
|
(0, ast_1.deleteModMethods)(srcAst, mapContext);
|
|
71
101
|
// 从onFormReady方法中提取版本日志并移除
|
|
@@ -82,6 +112,129 @@ function decoding$0(inPath, outPath, config, settingDir) {
|
|
|
82
112
|
fs_1.default.writeFileSync(outPath, dist, "utf-8");
|
|
83
113
|
});
|
|
84
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* 补充缺失依赖到 mapContext
|
|
117
|
+
* 扫描 coded.js 中未知的 __{mod} 后缀,在 libs 目录搜索对应文件并补充到 mod.map。
|
|
118
|
+
* @returns 是否有补充(需要写回 mod.map)
|
|
119
|
+
*/
|
|
120
|
+
function supplementMissingDeps(srcAst, mapContext, libDirs, inPath, conflicts) {
|
|
121
|
+
if (!libDirs.length) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
// 收集已知依赖名:所有 mod 的 dependencies 的并集。
|
|
125
|
+
// 注意判定标准必须与 deleteModMethods 一致——一个 __{mod} 后缀能被还原,
|
|
126
|
+
// 当且仅当 mod 出现在某个 mod 的 dependencies 中(而非仅仅是 mod 名存在于 map)。
|
|
127
|
+
// 否则 mod 条目存在但未被任何 mod 引用时(如 self.dependencies 缺少该依赖),
|
|
128
|
+
// 调用既不会还原也不会 require,需识别为"未知"并补充到 self.dependencies。
|
|
129
|
+
const knownDeps = new Set();
|
|
130
|
+
mapContext.getModNames().forEach((modName) => {
|
|
131
|
+
mapContext.getDependencyNameByMod(modName).forEach((dep) => knownDeps.add(dep));
|
|
132
|
+
});
|
|
133
|
+
const unknownDeps = (0, ast_1.getUnknownDepNames)(srcAst, knownDeps);
|
|
134
|
+
if (!unknownDeps.length) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
let supplemented = false;
|
|
138
|
+
unknownDeps.forEach((depName) => {
|
|
139
|
+
// 若 mod.map 已有该 mod 条目且 src 有效,只需补到 self.dependencies 即可,
|
|
140
|
+
// 无需重新搜索 libs(避免重名冲突和不必要的 src 覆盖)。
|
|
141
|
+
const existingSrc = mapContext.getSrcPathByMod(depName);
|
|
142
|
+
if (existingSrc) {
|
|
143
|
+
mapContext.referenceDep(depName);
|
|
144
|
+
logger.info(`补充依赖引用: ${depName}(复用已有 src: ${existingSrc})`);
|
|
145
|
+
supplemented = true;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const candidates = (0, scanf_1.scanfLibMod)(libDirs, depName);
|
|
149
|
+
if (candidates.length === 0) {
|
|
150
|
+
logger.warn(`未找到依赖文件: ${depName}(在 libs 目录中无匹配)`);
|
|
151
|
+
}
|
|
152
|
+
else if (candidates.length === 1) {
|
|
153
|
+
mapContext.appendMissingDep(depName, candidates[0]);
|
|
154
|
+
logger.info(`补充依赖: ${depName} -> ${candidates[0]}`);
|
|
155
|
+
supplemented = true;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
conflicts.push({ inPath, depName, candidates });
|
|
159
|
+
logger.warn(`依赖重名: ${depName},找到 ${candidates.length} 个候选,稍后处理`);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
return supplemented;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* 交互式解决重名依赖冲突
|
|
166
|
+
* 逐个让用户选择依赖文件,更新对应 mod.map。
|
|
167
|
+
* @returns 需要重新 decode 的文件路径列表(去重)
|
|
168
|
+
*/
|
|
169
|
+
function resolveConflicts(conflicts, config) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
171
|
+
// 按 inPath 分组
|
|
172
|
+
const byFile = new Map();
|
|
173
|
+
conflicts.forEach((c) => {
|
|
174
|
+
if (!byFile.has(c.inPath)) {
|
|
175
|
+
byFile.set(c.inPath, []);
|
|
176
|
+
}
|
|
177
|
+
byFile.get(c.inPath).push(c);
|
|
178
|
+
});
|
|
179
|
+
const redecodeFiles = [];
|
|
180
|
+
for (const [inPath, fileConflicts] of byFile) {
|
|
181
|
+
const settingDir = path_1.default.join(path_1.default.dirname(inPath), config.settingDir);
|
|
182
|
+
const mapContext = map_context_1.MapContext.readFromLocal(settingDir);
|
|
183
|
+
let resolved = false;
|
|
184
|
+
logger.info(`文件: ${inPath}`);
|
|
185
|
+
for (const { depName, candidates } of fileConflicts) {
|
|
186
|
+
const choice = yield askConflictChoice(depName, candidates);
|
|
187
|
+
if (choice === null) {
|
|
188
|
+
// 用户跳过
|
|
189
|
+
logger.warn(`跳过依赖: ${depName}`);
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
mapContext.appendMissingDep(depName, candidates[choice]);
|
|
193
|
+
logger.info(`已选择: ${depName} -> ${candidates[choice]}`);
|
|
194
|
+
resolved = true;
|
|
195
|
+
}
|
|
196
|
+
if (resolved) {
|
|
197
|
+
mapContext.writeToLocalDir(settingDir);
|
|
198
|
+
if (!redecodeFiles.includes(inPath)) {
|
|
199
|
+
redecodeFiles.push(inPath);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return redecodeFiles;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* 交互式询问用户选择重名依赖的候选文件
|
|
208
|
+
* @returns 选择的候选序号,null 表示跳过
|
|
209
|
+
*/
|
|
210
|
+
function askConflictChoice(depName, candidates) {
|
|
211
|
+
return new Promise((resolve) => {
|
|
212
|
+
const rl = readline_1.default.createInterface({
|
|
213
|
+
input: process.stdin,
|
|
214
|
+
output: process.stdout
|
|
215
|
+
});
|
|
216
|
+
console.log(`\n 依赖 "${depName}" 找到 ${candidates.length} 个候选文件:`);
|
|
217
|
+
candidates.forEach((c, i) => {
|
|
218
|
+
console.log(` [${i + 1}] ${c}`);
|
|
219
|
+
});
|
|
220
|
+
console.log(` [s] 跳过该依赖`);
|
|
221
|
+
rl.question(` 请选择 [1-${candidates.length} / s]: `, (answer) => {
|
|
222
|
+
rl.close();
|
|
223
|
+
const trimmed = answer.trim().toLowerCase();
|
|
224
|
+
if (trimmed === "s") {
|
|
225
|
+
resolve(null);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const num = parseInt(trimmed, 10);
|
|
229
|
+
if (isNaN(num) || num < 1 || num > candidates.length) {
|
|
230
|
+
logger.warn(`无效输入,已跳过: ${depName}`);
|
|
231
|
+
resolve(null);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
resolve(num - 1);
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
}
|
|
85
238
|
/**
|
|
86
239
|
* 从onFormReady方法中提取版本日志并移除,将版本号写入version文件
|
|
87
240
|
* @param srcAst - 源码AST
|
package/core/scanf/index.js
CHANGED
|
@@ -5,6 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.scanfCodeFiles = scanfCodeFiles;
|
|
7
7
|
exports.scanfCodeDirs = scanfCodeDirs;
|
|
8
|
+
exports.resolveLibDirs = resolveLibDirs;
|
|
9
|
+
exports.scanfLibMod = scanfLibMod;
|
|
8
10
|
exports.scanfRequieMod = scanfRequieMod;
|
|
9
11
|
const fs_1 = __importDefault(require("fs"));
|
|
10
12
|
const path_1 = __importDefault(require("path"));
|
|
@@ -46,6 +48,75 @@ function scanfCodeDirs(baseDir, targetDirs) {
|
|
|
46
48
|
const dirs = new Set(targetDirs);
|
|
47
49
|
return [...dirs].map((dir) => `${path_1.default.join(baseDir, dir.replace(/\/|\\/, path_1.default.sep))}`);
|
|
48
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* 将 libs 配置解析为绝对路径数组
|
|
53
|
+
* 支持 "@/util" 别名路径(@ 代表项目根目录 rootDir)和 "util" 相对 baseDir 路径
|
|
54
|
+
* @param libs - libs 配置数组
|
|
55
|
+
* @param baseDir - 基础目录(相对路径的基准)
|
|
56
|
+
* @param rootDir - 项目根目录(@ 别名的基准,未提供时 @ 路径会被跳过并警告)
|
|
57
|
+
* @returns 绝对路径数组
|
|
58
|
+
*/
|
|
59
|
+
function resolveLibDirs(libs, baseDir, rootDir) {
|
|
60
|
+
return libs
|
|
61
|
+
.map((lib) => {
|
|
62
|
+
if (lib.startsWith("@/") || lib.startsWith("@\\")) {
|
|
63
|
+
if (!rootDir) {
|
|
64
|
+
logger.warn(`@ 别名路径需要 rootDir,已跳过: ${lib}`);
|
|
65
|
+
return "";
|
|
66
|
+
}
|
|
67
|
+
const sub = lib.replace(/^@[/\\]/, "");
|
|
68
|
+
return path_1.default.join(rootDir.replace(/[\\/]$/, ""), sub);
|
|
69
|
+
}
|
|
70
|
+
return path_1.default.join(baseDir, lib.replace(/\/|\\/, path_1.default.sep));
|
|
71
|
+
})
|
|
72
|
+
.filter((dir) => dir !== "");
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 在 libs 目录下搜索依赖模块文件
|
|
76
|
+
* 查找 modName.js(文件)和 modName/index.js(目录下入口)
|
|
77
|
+
* @param libDirs - 已解析的绝对路径目录数组
|
|
78
|
+
* @param modName - 依赖模块名(如 "drawer")
|
|
79
|
+
* @returns 所有匹配的绝对路径数组
|
|
80
|
+
*/
|
|
81
|
+
function scanfLibMod(libDirs, modName) {
|
|
82
|
+
const results = [];
|
|
83
|
+
const seen = new Set();
|
|
84
|
+
libDirs.forEach((libDir) => {
|
|
85
|
+
if (!fs_1.default.existsSync(libDir)) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
// 1. libDir/modName.js
|
|
89
|
+
const filePath = path_1.default.join(libDir, `${modName}.js`);
|
|
90
|
+
if (fs_1.default.existsSync(filePath) && !seen.has(filePath)) {
|
|
91
|
+
seen.add(filePath);
|
|
92
|
+
results.push(filePath);
|
|
93
|
+
}
|
|
94
|
+
// 2. libDir/modName/index.js(modName 为子目录)
|
|
95
|
+
const subDir = path_1.default.join(libDir, modName);
|
|
96
|
+
if (fs_1.default.existsSync(subDir) && fs_1.default.statSync(subDir).isDirectory()) {
|
|
97
|
+
const indexFile = path_1.default.join(subDir, "index.js");
|
|
98
|
+
if (fs_1.default.existsSync(indexFile) && !seen.has(indexFile)) {
|
|
99
|
+
seen.add(indexFile);
|
|
100
|
+
results.push(indexFile);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// 3. 递归搜索子目录(通用方法可能嵌套在子目录下)
|
|
104
|
+
const subDirs = fs_1.default
|
|
105
|
+
.readdirSync(libDir, { withFileTypes: true })
|
|
106
|
+
.filter((d) => d.isDirectory())
|
|
107
|
+
.map((d) => path_1.default.join(libDir, d.name));
|
|
108
|
+
subDirs.forEach((sub) => {
|
|
109
|
+
const nested = scanfLibMod([sub], modName);
|
|
110
|
+
nested.forEach((p) => {
|
|
111
|
+
if (!seen.has(p)) {
|
|
112
|
+
seen.add(p);
|
|
113
|
+
results.push(p);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
return results;
|
|
119
|
+
}
|
|
49
120
|
/**
|
|
50
121
|
* 扫描引入路径
|
|
51
122
|
* require('../../util')
|
package/core/util/ast.js
CHANGED
|
@@ -15,6 +15,7 @@ exports.getDependentMethodNames = getDependentMethodNames;
|
|
|
15
15
|
exports.importMods = importMods;
|
|
16
16
|
exports.deleteModMethods = deleteModMethods;
|
|
17
17
|
exports.getInlineMethodsByMethodName = getInlineMethodsByMethodName;
|
|
18
|
+
exports.getUnknownDepNames = getUnknownDepNames;
|
|
18
19
|
const fs_1 = __importDefault(require("fs"));
|
|
19
20
|
const parser_1 = require("@babel/parser");
|
|
20
21
|
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
@@ -455,3 +456,39 @@ function getInlineMethodsByMethodName(srcAst, entry, methodName, useMethodNameMa
|
|
|
455
456
|
});
|
|
456
457
|
return [...inlineMethods];
|
|
457
458
|
}
|
|
459
|
+
/**
|
|
460
|
+
* 从 coded.js 的 AST 中提取未知的依赖名(__{mod} 后缀中的 mod)
|
|
461
|
+
* 扫描 ObjectMethod 的 key 和 MemberExpression 的 property,提取 xxx__yyy 中的 yyy,
|
|
462
|
+
* 排除已知依赖名(mod.map 中已有的依赖)。
|
|
463
|
+
* @param srcAst - 源码AST
|
|
464
|
+
* @param knownDeps - 已知依赖名集合
|
|
465
|
+
* @returns 未知依赖名数组(去重)
|
|
466
|
+
*/
|
|
467
|
+
function getUnknownDepNames(srcAst, knownDeps) {
|
|
468
|
+
const depNames = new Set();
|
|
469
|
+
const collect = (name) => {
|
|
470
|
+
const split = name.split("__");
|
|
471
|
+
if (split.length < 2) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
// 取最后一段作为依赖名(与 deleteModMethods 的解析逻辑一致)
|
|
475
|
+
const depName = split[split.length - 1];
|
|
476
|
+
if (!depName || knownDeps.has(depName)) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
depNames.add(depName);
|
|
480
|
+
};
|
|
481
|
+
(0, traverse_1.default)(srcAst, {
|
|
482
|
+
ObjectMethod(path) {
|
|
483
|
+
if (core_1.types.isIdentifier(path.node.key)) {
|
|
484
|
+
collect(path.node.key.name);
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
MemberExpression(path) {
|
|
488
|
+
if (core_1.types.isIdentifier(path.node.property)) {
|
|
489
|
+
collect(path.node.property.name);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
return [...depNames];
|
|
494
|
+
}
|
package/package.json
CHANGED
|
@@ -1,42 +1,46 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@kezh/my-custom-js-converter",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "自定义代码转换器",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"@babel/
|
|
29
|
-
"@babel/
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"@types
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"node": "
|
|
41
|
-
|
|
42
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@kezh/my-custom-js-converter",
|
|
3
|
+
"version": "1.6.1",
|
|
4
|
+
"description": "自定义代码转换器",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"core/**/*.js",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
10
|
+
"bin": {
|
|
11
|
+
"encode": "core/encode/index.js",
|
|
12
|
+
"decode": "core/decode/index.js"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "npx tsc --build",
|
|
16
|
+
"test": "npm run build && node test/run_tests.js",
|
|
17
|
+
"test:manual": "npm run build && node test/manual_review.js"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=24.15.0"
|
|
21
|
+
},
|
|
22
|
+
"author": "shahow",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"babel"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@babel/cli": "^7.29.7",
|
|
29
|
+
"@babel/core": "^7.29.7",
|
|
30
|
+
"@babel/generator": "^7.29.7",
|
|
31
|
+
"@babel/parser": "^7.29.7",
|
|
32
|
+
"@babel/traverse": "^7.29.7",
|
|
33
|
+
"@babel/types": "^7.29.7",
|
|
34
|
+
"prettier": "^3.8.4"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/babel__core": "^7.20.5",
|
|
38
|
+
"@types/babel__generator": "^7.27.0",
|
|
39
|
+
"@types/babel__traverse": "^7.28.0",
|
|
40
|
+
"@types/node": "^25.9.3",
|
|
41
|
+
"typescript": "^6.0.3"
|
|
42
|
+
},
|
|
43
|
+
"volta": {
|
|
44
|
+
"node": "24.15.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/.claude/plan.md
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
# 日志优化方案
|
|
2
|
-
|
|
3
|
-
## 现状问题
|
|
4
|
-
|
|
5
|
-
1. **格式不统一** — 有的用 `=>` 分隔,有的直接打印数组,风格混乱
|
|
6
|
-
2. **缺少层级结构** — 所有日志平铺输出,无法区分步骤/阶段
|
|
7
|
-
3. **信息不完整** — `scanf dirs => ` 和数组分两行打印,`the map is not found!` 没有上下文
|
|
8
|
-
4. **缺少状态标识** — 没有成功/失败/警告等视觉区分
|
|
9
|
-
5. **中英混杂** — `路径不存在[...]忽略` vs `scanf dirs` vs `the map is not found!`
|
|
10
|
-
6. **缺少汇总** — 处理完成后没有统计摘要
|
|
11
|
-
|
|
12
|
-
## 优化方案
|
|
13
|
-
|
|
14
|
-
### 1. 新建 `src/util/logger.ts` — 统一日志工具
|
|
15
|
-
|
|
16
|
-
```typescript
|
|
17
|
-
export function createLogger(scope: string) {
|
|
18
|
-
return {
|
|
19
|
-
info(msg: string) { console.log(`[${scope}] ✔ ${msg}`); },
|
|
20
|
-
warn(msg: string) { console.log(`[${scope}] ⚠ ${msg}`); },
|
|
21
|
-
error(msg: string) { console.log(`[${scope}] ✖ ${msg}`); },
|
|
22
|
-
step(title: string) {
|
|
23
|
-
console.log(`\n[${scope}] ── ${title} ${'─'.repeat(Math.max(0, 50 - title.length))}`);
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
简洁、无外部依赖、终端友好。
|
|
30
|
-
|
|
31
|
-
### 2. 日志对照表
|
|
32
|
-
|
|
33
|
-
| 原始 | 优化后 |
|
|
34
|
-
|---|---|
|
|
35
|
-
| `console.log("scanf dirs => ")` + `console.log(targetDirs)` | `logger.info('scanf dirs: ' + targetDirs.join(', '))` |
|
|
36
|
-
| `console.log(\`scanf file => ${inPath}\`)` | `logger.info(\`scanf file: ${inPath}\`)` |
|
|
37
|
-
| `console.log(\`output file => ${outPath}\`)` | `logger.info(\`output: ${outPath}\`)` |
|
|
38
|
-
| `console.log(\`output => ${outPath}\`)` | `logger.info(\`output: ${outPath}\`)` |
|
|
39
|
-
| `console.log(\`input map => ${inPath}\`)` | `logger.info(\`read map: ${inPath}\`)` |
|
|
40
|
-
| `console.log("the map is not found!")` | `logger.warn('map file not found')` |
|
|
41
|
-
| `console.log(\`output map => ${outPath}\`)` | `logger.info(\`write map: ${outPath}\`)` |
|
|
42
|
-
| `console.log(\`not found entry => ${entry}\`)` | `logger.warn(\`entry not found: ${entry}\`)` |
|
|
43
|
-
| `console.log(\`路径不存在[${dirPath}]忽略\`)` | `logger.warn(\`dir not found, skipping: ${dirPath}\`)` |
|
|
44
|
-
| `console.log(\`ignore module => ${item.id.name}\`)` | `logger.info(\`ignore module: ${item.id.name}\`)` |
|
|
45
|
-
|
|
46
|
-
### 3. 增加步骤分隔和汇总
|
|
47
|
-
|
|
48
|
-
**encode** 流程增加:
|
|
49
|
-
- 步骤标题:扫描目录 → 查找文件 → 编码处理
|
|
50
|
-
- 汇总:处理文件数、方法总数、版本号
|
|
51
|
-
|
|
52
|
-
**decode** 流程增加:
|
|
53
|
-
- 步骤标题:扫描目录 → 查找文件 → 解码处理
|
|
54
|
-
- 汇总:处理文件数
|
|
55
|
-
|
|
56
|
-
### 4. 修改文件清单
|
|
57
|
-
|
|
58
|
-
| 文件 | 变更 |
|
|
59
|
-
|---|---|
|
|
60
|
-
| `src/util/logger.ts` | **新建** — 日志工具模块 |
|
|
61
|
-
| `src/encode/index.ts` | 替换 4 处 console.log,增加步骤和汇总 |
|
|
62
|
-
| `src/decode/index.ts` | 替换 4 处 console.log,增加步骤和汇总 |
|
|
63
|
-
| `src/context/map_context.ts` | 替换 3 处 console.log |
|
|
64
|
-
| `src/scanf/index.ts` | 替换 1 处 console.log |
|
|
65
|
-
| `src/util/ast.ts` | 替换 1 处 console.log |
|
package/CLAUDE.md
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
# CLAUDE.md
|
|
2
|
-
|
|
3
|
-
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
-
|
|
5
|
-
## Project Overview
|
|
6
|
-
|
|
7
|
-
`@kezh/my-custom-js-converter` is a Babel-based JavaScript code transformer that **encodes** (flattens) and **decodes** (restores) CommonJS module structures. It takes JavaScript files that use `require()` + object-oriented patterns and flattens them into a single object with renamed methods, then can reverse the process.
|
|
8
|
-
|
|
9
|
-
## Build & Run
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
pnpm install # install dependencies
|
|
13
|
-
pnpm build # compile TypeScript (npx tsc --build)
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
The compiled output goes to `core/` (gitignored). Source is in `src/`.
|
|
17
|
-
|
|
18
|
-
Two CLI entry points (declared in `package.json` bin):
|
|
19
|
-
- `encode` → `core/encode/index.js` — flattens modules into a single file
|
|
20
|
-
- `decode` → `core/decode/index.js` — restores the original module structure
|
|
21
|
-
|
|
22
|
-
No test framework is configured.
|
|
23
|
-
|
|
24
|
-
## Architecture
|
|
25
|
-
|
|
26
|
-
### Data Flow
|
|
27
|
-
|
|
28
|
-
**Encode**: Scan target dirs → find entry JS files → build dependency map (`MapContext`) → extract & rename methods from all modules → output single flattened file + `mod.map`
|
|
29
|
-
|
|
30
|
-
**Decode**: Read `mod.map` → read encoded file → re-insert `require` statements → remove inlined dependency methods → restore original method names → output restored file
|
|
31
|
-
|
|
32
|
-
### Key Modules
|
|
33
|
-
|
|
34
|
-
- **`src/config/`** — Configuration loading. `MainConfig` defines encode/decode settings (entry object name, file names, ignore list). Reads `config.json` from the parent project root when installed as a dependency.
|
|
35
|
-
- **`src/context/map_context.ts`** — Core orchestration. `MapContext` builds a dependency graph by recursively walking `require()` calls, then performs **filtering** (removing invalid method references) and **tree-shaking** (removing unused methods). Serializes to `mod.map` JSON file.
|
|
36
|
-
- **`src/util/ast.ts`** — All Babel AST manipulation: parsing, traversing, extracting object methods, renaming methods (appending `__modName` suffix), generating import statements, and reversing the renaming during decode.
|
|
37
|
-
- **`src/scanf/`** — File system scanning: finding target files by name, resolving `require()` paths (handles both `dir/index.js` and `file.js` patterns).
|
|
38
|
-
- **`src/util/parent_path.ts`** — Detects whether the tool is running inside `node_modules/` and resolves the parent project root.
|
|
39
|
-
|
|
40
|
-
### Encoding Convention
|
|
41
|
-
|
|
42
|
-
Methods from dependency modules are renamed with a `__modName` suffix (e.g., `helperMethod` becomes `helperMethod__util`). `this.method()` calls in the root module stay as-is; in dependency modules, `this.method()` becomes `this.method__modName()`. The decode step reverses this convention.
|
|
43
|
-
|
|
44
|
-
### Configuration
|
|
45
|
-
|
|
46
|
-
When used as a dependency, the tool reads `config.json` from the parent project's root. Default config:
|
|
47
|
-
- `encode.file`: `"index.js"`, `encode.output`: `"coded.js"`, `encode.entry`: `"customEvent"`
|
|
48
|
-
- `decode.file`: `"coded.js"`, `decode.output`: `"decoded.js"`, `decode.mount`: `"customEvent"`
|
|
49
|
-
- `settingDir`: `".setting"` (where `mod.map` is stored)
|
|
50
|
-
- `target`: directories to scan relative to `baseDir`
|