@kezh/my-custom-js-converter 1.4.0 → 1.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/.claude/plan.md CHANGED
@@ -1,53 +1,65 @@
1
- # 实现 @ 路径别名支持
1
+ # 日志优化方案
2
2
 
3
- ## 目标
4
- 在 `config.js` 中添加开关配置,支持 `@` 写法:`const mod = require('@/util/@3.0.0/mod.js')`,其中 `@` 代表项目根目录(baseDir)。
3
+ ## 现状问题
5
4
 
6
- ## 修改文件
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. **缺少汇总** — 处理完成后没有统计摘要
7
11
 
8
- ### 1. `src/config/main_config.ts` — 添加配置项
9
- 在 `EncodeConfig` 中添加 `useAlias: boolean` 开关(默认 `false`)。
12
+ ## 优化方案
10
13
 
11
- ### 2. `src/config/index.ts` — 添加默认值
12
- 在 `defConfig.encode` 中添加 `useAlias: false`。
14
+ ### 1. 新建 `src/util/logger.ts` — 统一日志工具
13
15
 
14
- ### 3. `src/scanf/index.ts` — `scanfRequieMod` 支持 @ 路径
15
- 当 `modPath` 以 `@/` 或 `@\\` 开头时,将 `@` 替换为 `baseDir`(项目根目录),然后再进行路径解析。
16
-
17
- 修改逻辑:
18
16
  ```typescript
19
- export function scanfRequieMod(baseDir: string, modPath: string): string {
20
- // 支持 @ 别名:@/xxx => baseDir/xxx
21
- if (modPath.startsWith("@/") || modPath.startsWith("@\\")) {
22
- modPath = modPath.replace(/^@[/\\]/, baseDir + path.sep);
23
- }
24
- // ... 原有逻辑不变
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
+ };
25
26
  }
26
27
  ```
27
28
 
28
- ### 4. `src/util/ast.ts` — `importMods` 支持 @ 路径生成
29
- decode 时生成 require 语句,如果 `config.encode.useAlias` 为 `true`,则生成 `@/...` 格式而非相对路径。
29
+ 简洁、无外部依赖、终端友好。
30
30
 
31
- 需要将 `config` 传入 `importMods` 函数,或在函数内读取全局 config。
31
+ ### 2. 日志对照表
32
32
 
33
- 修改逻辑:
34
- ```typescript
35
- // 当 useAlias 开启时,使用 @ 别名路径替代相对路径
36
- if (useAlias) {
37
- const aliasPath = srcPath.replace(baseDir, "").replace(/\\/g, "/");
38
- requireFrom = "@" + aliasPath;
39
- } else {
40
- requireFrom = relative(outDir, srcPath.replace(/(index)?\.js$/, "")).replace(/[\\]+/g, "/");
41
- }
42
- ```
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
+ - 汇总:处理文件数
43
55
 
44
- ### 5. `src/decode/index.ts` — 传递 config 给 importMods
45
- `importMods` 调用处需要传递 config 信息。
56
+ ### 4. 修改文件清单
46
57
 
47
- ## 实现顺序
48
- 1. 修改 `MainConfig` 添加 `useAlias` 字段
49
- 2. 修改 `initConfig` 添加默认值
50
- 3. 修改 `scanfRequieMod` 支持 `@` 路径解析
51
- 4. 修改 `importMods` 支持 `@` 路径生成
52
- 5. 修改 `decode/index.ts` 传递 config
53
- 6. 编译验证
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/TASKS.md CHANGED
@@ -1,36 +1,2 @@
1
1
  # 任务
2
- 编写体验优化,当前编写导入模块仅支持`const mod = require('../../util/@3.0.0/mod.js');`,我希望能够在`config.js`配置中加入开关配置,支持@写法:`const mod = require('@/util/@3.0.0/mod.js');`
3
-
4
- # 实现方案
5
-
6
- 在 `EncodeConfig` 中添加 `useAlias: boolean` 开关:
7
- - `useAlias: true` — encode 阶段解析 `@/` 路径别名(`@` 代表项目根目录),decode 阶段生成 `@/` 路径
8
- - `useAlias: false`(默认)— 使用传统的相对路径
9
-
10
- ### 修改的文件
11
-
12
- | 文件 | 改动 |
13
- |------|------|
14
- | `src/config/main_config.ts` | `EncodeConfig` 添加 `useAlias` 字段 |
15
- | `src/config/index.ts` | 默认 `useAlias: false`;`__dirname` 不含 `node_modules` 时回退到 `process.cwd()`;尝试 `config.js` 和 `config.json` 两种配置文件 |
16
- | `src/scanf/index.ts` | `scanfRequieMod` 添加 `rootDir` 参数,支持 `@/` 路径解析 |
17
- | `src/util/ast.ts` | `getRequireModPaths` 传递 `rootDir`;`importMods` 根据 `useAlias` 生成 `@/` 或相对路径;修复相对路径缺少 `./` 前缀的问题 |
18
- | `src/util/parent_path.ts` | `getParentRootDir` 在 junction symlink 场景下回退到 `config.baseDir` |
19
- | `src/context/map_context.ts` | `buildMapContext` 根据 `useAlias` 条件传递 `rootDir` |
20
- | `src/decode/index.ts` | 传递 `config.encode.useAlias` 给 `importMods` |
21
-
22
- # 测试结果 ✅
23
-
24
- ### useAlias: true
25
- - **Encode**: `@/src/util/index` 正确解析为绝对路径,方法正确内联并添加 `__modName` 后缀
26
- - **Decode**: 生成 `require("@/src/util")` 和 `require("@/src/helper")`,`__modName` 后缀正确移除
27
-
28
- ### useAlias: false
29
- - **Encode**: 相对路径 `./util/index` 正常解析
30
- - **Decode**: 生成 `require("./util")` 和 `require("./helper")`,路径以 `./` 开头
31
-
32
- ### 修复的问题
33
- 1. `useAlias` 无效 — `scanfRequieMod` 缺少 `rootDir` 参数,`@` 路径无法解析
34
- 2. `Error: src\@\util\index.js is not exists!` — junction symlink 导致 `__dirname` 不含 `node_modules`,`getParentRootDir()` 返回 undefined
35
- 3. `require("util")` 缺少 `./` 前缀 — `path.relative()` 返回的路径不以 `./` 开头,Node.js 会当作 npm 包名
36
- 4. 配置文件查找失败 — 只尝试 `config.js`,不尝试 `config.json`
2
+ 项目encode和decode输出日志可读性差,请帮我优化日志。
@@ -11,6 +11,8 @@ const ast_1 = require("../util/ast");
11
11
  const parent_path_1 = require("../util/parent_path");
12
12
  const config_1 = require("../config");
13
13
  const os_1 = require("os");
14
+ const logger_1 = require("../util/logger");
15
+ const logger = (0, logger_1.createLogger)("map");
14
16
  class MapContext {
15
17
  constructor(srcPath = "", entry = "", outDir = "", initMap = false) {
16
18
  this.map = new map_config_1.MapConfig();
@@ -59,7 +61,7 @@ class MapContext {
59
61
  }
60
62
  static readFromLocal(inDir) {
61
63
  const inPath = path_1.default.join(inDir, MapContext.SAVE_FILE);
62
- console.log(`input map => ${inPath}`);
64
+ logger.info(`read map: ${inPath}`);
63
65
  try {
64
66
  const json = fs_1.default.readFileSync(inPath, { encoding: "utf-8" });
65
67
  const map = JSON.parse(json);
@@ -68,7 +70,7 @@ class MapContext {
68
70
  return mapContext;
69
71
  }
70
72
  catch (err) {
71
- console.log("the map is not found!");
73
+ logger.warn("map file not found");
72
74
  // console.log(err);
73
75
  }
74
76
  return new MapContext();
@@ -81,7 +83,7 @@ class MapContext {
81
83
  }
82
84
  writeToLocal() {
83
85
  const outPath = path_1.default.join(this.outDir, MapContext.SAVE_FILE);
84
- console.log(`output map => ${outPath}`);
86
+ logger.info(`write map: ${outPath}`);
85
87
  const json = JSON.stringify(this.map, null, 4);
86
88
  try {
87
89
  fs_1.default.accessSync(this.outDir);
@@ -95,7 +97,7 @@ class MapContext {
95
97
  const srcAst = (0, ast_1.parseSrcAst)(inPath);
96
98
  const srcMethodNames = (0, ast_1.getObjectMethodNames)((0, ast_1.getObjectMethodsByEntryAndMethodNames)(srcAst, entry));
97
99
  if (!srcMethodNames.length) {
98
- console.log(`not found entry => ${entry}`);
100
+ logger.warn(`entry not found: ${entry}`);
99
101
  this.appendModMap(new map_config_1.Mod(inPath, new map_config_1.Dependencies()), root, entry);
100
102
  return;
101
103
  }
@@ -26,19 +26,26 @@ const map_context_1 = require("../context/map_context");
26
26
  const scanf_1 = require("../scanf");
27
27
  const ast_1 = require("../util/ast");
28
28
  const version_1 = require("../util/version");
29
+ const logger_1 = require("../util/logger");
30
+ const logger = (0, logger_1.createLogger)("decode");
29
31
  (function decoding(config) {
30
32
  return __awaiter(this, void 0, void 0, function* () {
31
33
  const targetDirs = (0, scanf_1.scanfCodeDirs)(config.baseDir, config.target);
32
- console.log("scanf dirs => ");
33
- console.log(targetDirs);
34
+ logger.step("扫描目录");
35
+ logger.info(`目标目录: ${targetDirs.join(", ")}`);
34
36
  const codeFiles = (0, scanf_1.scanfCodeFiles)(targetDirs, config.decode.file);
35
- for (const inPath of codeFiles) {
36
- console.log(`scanf file => ${inPath}`);
37
+ logger.info(`找到 ${codeFiles.length} 个编码文件 (${config.decode.file})`);
38
+ for (let i = 0; i < codeFiles.length; i++) {
39
+ const inPath = codeFiles[i];
37
40
  const outPath = path_1.default.join(path_1.default.dirname(inPath), config.decode.output);
38
41
  const settingDir = path_1.default.join(path_1.default.dirname(inPath), config.settingDir);
42
+ logger.step(`解码处理 [${i + 1}/${codeFiles.length}]`);
43
+ logger.info(`源文件: ${inPath}`);
39
44
  yield decoding$0(inPath, outPath, config, settingDir);
40
- console.log(`output => ${outPath}`);
45
+ logger.info(`输出: ${outPath}`);
41
46
  }
47
+ logger.step("完成");
48
+ logger.info(`共处理 ${codeFiles.length} 个文件`);
42
49
  });
43
50
  })(config_1.config);
44
51
  /**
@@ -11,18 +11,24 @@ const map_context_1 = require("../context/map_context");
11
11
  const ast_1 = require("../util/ast");
12
12
  const version_1 = require("../util/version");
13
13
  const core_1 = require("@babel/core");
14
+ const logger_1 = require("../util/logger");
15
+ const logger = (0, logger_1.createLogger)("encode");
14
16
  (function encoding(config) {
15
17
  const encodeConfig = config.encode;
16
18
  const targetDirs = (0, scanf_1.scanfCodeDirs)(config.baseDir, config.target);
17
- console.log("scanf dirs => ");
18
- console.log(targetDirs);
19
+ logger.step("扫描目录");
20
+ logger.info(`目标目录: ${targetDirs.join(", ")}`);
19
21
  const codeFiles = (0, scanf_1.scanfCodeFiles)(targetDirs, encodeConfig.file);
20
- codeFiles.forEach((inPath) => {
22
+ logger.info(`找到 ${codeFiles.length} 个入口文件 (${encodeConfig.file})`);
23
+ let totalMethods = 0;
24
+ codeFiles.forEach((inPath, index) => {
21
25
  const workDir = path_1.default.dirname(inPath);
22
26
  const settingDir = path_1.default.join(workDir, config.settingDir);
23
- console.log(`scanf file => ${inPath}`);
27
+ logger.step(`编码处理 [${index + 1}/${codeFiles.length}]`);
28
+ logger.info(`源文件: ${inPath}`);
24
29
  const mapContext = new map_context_1.MapContext(inPath, encodeConfig.entry, settingDir, true);
25
30
  const methods = encoding$0(mapContext, encodeConfig.entry);
31
+ totalMethods += methods.length;
26
32
  // 读取或创建版本号
27
33
  const version = (0, version_1.readOrCreateVersion)(settingDir);
28
34
  // 在onFormReady方法中注入版本日志
@@ -30,17 +36,19 @@ const core_1 = require("@babel/core");
30
36
  // 将onFormReady和onFormSubmit排到最前面
31
37
  reorderMethods(methods);
32
38
  const outPath = path_1.default.join(workDir, encodeConfig.output);
33
- console.log(`output file => ${outPath}`);
34
39
  (0, ast_1.outputObjectMethods)(outPath, methods);
40
+ logger.info(`方法数: ${methods.length}, 版本: ${version}`);
41
+ logger.info(`输出: ${outPath}`);
35
42
  // 版本号递增并写入
36
43
  (0, version_1.incrementVersion)(settingDir);
37
44
  });
45
+ logger.step("完成");
46
+ logger.info(`共处理 ${codeFiles.length} 个文件, ${totalMethods} 个方法`);
38
47
  })(config_1.config);
39
48
  function encoding$0(mapContext, entry) {
40
49
  const modNames = mapContext.getModNames();
41
50
  return modNames.flatMap((name) => {
42
51
  const srcAst = (0, ast_1.parseSrcAst)(mapContext.getAbsoluteSrcPathByMod(name));
43
- // console.log(`import => ${name}`);
44
52
  const srcMethods = [];
45
53
  const self = name === "self";
46
54
  if (self) {
@@ -8,6 +8,8 @@ exports.scanfCodeDirs = scanfCodeDirs;
8
8
  exports.scanfRequieMod = scanfRequieMod;
9
9
  const fs_1 = __importDefault(require("fs"));
10
10
  const path_1 = __importDefault(require("path"));
11
+ const logger_1 = require("../util/logger");
12
+ const logger = (0, logger_1.createLogger)("scanf");
11
13
  function scanfCodeFiles(dirPaths, filterName = "") {
12
14
  if (typeof dirPaths === "string") {
13
15
  return scanfCodeFiles$0(dirPaths, filterName);
@@ -20,7 +22,7 @@ function scanfCodeFiles(dirPaths, filterName = "") {
20
22
  function scanfCodeFiles$0(dirPath, filterName = "") {
21
23
  const filePaths = [];
22
24
  if (!fs_1.default.existsSync(dirPath)) {
23
- console.log(`路径不存在[${dirPath}]忽略`);
25
+ logger.warn(`dir not found, skipping: ${dirPath}`);
24
26
  return filePaths;
25
27
  }
26
28
  const files = fs_1.default.readdirSync(dirPath, {
package/core/util/ast.js CHANGED
@@ -25,6 +25,8 @@ const path_1 = require("path");
25
25
  const os_1 = require("os");
26
26
  const parent_path_1 = require("./parent_path");
27
27
  const config_1 = require("../config");
28
+ const logger_1 = require("./logger");
29
+ const logger = (0, logger_1.createLogger)("ast");
28
30
  function parseSrcAst(srcPath) {
29
31
  if (!srcPath) {
30
32
  return;
@@ -70,7 +72,7 @@ function getRequireModPaths(baseDir, srcAst, ignoreMod, rootDir) {
70
72
  if (item.init.callee.name === "require") {
71
73
  if (core_1.types.isIdentifier(item.id)) {
72
74
  if (ignoreMod === null || ignoreMod === void 0 ? void 0 : ignoreMod.includes(item.id.name)) {
73
- console.log(`ignore module => ${item.id.name}`);
75
+ logger.info(`ignore module: ${item.id.name}`);
74
76
  }
75
77
  else {
76
78
  return true;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * 统一日志工具模块
3
+ * 提供带 scope 前缀和状态图标的日志函数
4
+ */
5
+ export interface Logger {
6
+ info(msg: string): void;
7
+ warn(msg: string): void;
8
+ error(msg: string): void;
9
+ step(title: string): void;
10
+ }
11
+ /**
12
+ * 创建带 scope 前缀的 logger
13
+ * @param scope - 日志前缀,如 'encode' / 'decode'
14
+ */
15
+ export declare function createLogger(scope: string): Logger;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ /**
3
+ * 统一日志工具模块
4
+ * 提供带 scope 前缀和状态图标的日志函数
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.createLogger = createLogger;
8
+ /**
9
+ * 创建带 scope 前缀的 logger
10
+ * @param scope - 日志前缀,如 'encode' / 'decode'
11
+ */
12
+ function createLogger(scope) {
13
+ return {
14
+ info(msg) {
15
+ console.log(`[${scope}] ✔ ${msg}`);
16
+ },
17
+ warn(msg) {
18
+ console.log(`[${scope}] ⚠ ${msg}`);
19
+ },
20
+ error(msg) {
21
+ console.log(`[${scope}] ✖ ${msg}`);
22
+ },
23
+ step(title) {
24
+ const line = "─".repeat(Math.max(0, 50 - title.length));
25
+ console.log(`\n[${scope}] ── ${title} ${line}`);
26
+ }
27
+ };
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kezh/my-custom-js-converter",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "自定义代码转换器",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -1 +1 @@
1
- {"root":["./src/config/index.ts","./src/config/main_config.ts","./src/config/map_config.ts","./src/context/map_context.ts","./src/decode/index.ts","./src/encode/index.ts","./src/scanf/index.ts","./src/util/ast.ts","./src/util/parent_path.ts","./src/util/version.ts"],"version":"6.0.3"}
1
+ {"root":["./src/config/index.ts","./src/config/main_config.ts","./src/config/map_config.ts","./src/context/map_context.ts","./src/decode/index.ts","./src/encode/index.ts","./src/scanf/index.ts","./src/util/ast.ts","./src/util/logger.ts","./src/util/parent_path.ts","./src/util/version.ts"],"version":"6.0.3"}