encoding-aware-fs 0.1.5 → 0.1.7

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 CHANGED
@@ -11,8 +11,9 @@ AI 编码助手默认所有文件都是 UTF-8 编码,在读写 GB18030/GBK/GB2
11
11
  - **自动编码检测** — 读取文件时自动识别原始编码(GB18030/GBK/GB2312),返回 UTF-8 给 AI 工具
12
12
  - **透明回写** — 写入时自动将 UTF-8 内容转换回文件的原始编码
13
13
  - **行尾保持** — 检测并保持文件原有的 CRLF/LF 行尾格式,编辑后不会改变
14
+ - **目录级编码规则** — 通过 glob 模式为不同目录指定不同编码(如 `openspec/**` 用 UTF-8,`legacy/**` 用 GBK)
14
15
  - **三个 MCP 工具**:`read_file`、`write_file`、`edit_file` — 替代内置文件操作工具
15
- - **一键安装/卸载** — 支持 Claude Code 和 OpenCode
16
+ - **一键安装/卸载** — 支持 Claude Code 和 OpenCode,交互式配置编码规则
16
17
 
17
18
  ## 安装
18
19
 
@@ -41,6 +42,25 @@ npx encoding-aware-fs uninstall
41
42
  1. **读取时**:通过 BOM 和启发式算法检测文件编码,解码为 UTF-8 返回
42
43
  2. **写入/编辑时**:先读取原文件检测编码和行尾风格,在 UTF-8 下完成操作,写入前转回原编码并还原行尾格式
43
44
 
45
+ ## 项目配置
46
+
47
+ 项目根目录的 `.encoding-converter.json` 控制编码行为:
48
+
49
+ ```json
50
+ {
51
+ "sourceEncoding": "GB18030",
52
+ "confidenceThreshold": 0.8,
53
+ "overrides": [
54
+ { "pattern": "openspec/**", "sourceEncoding": "UTF-8" },
55
+ { "pattern": "legacy/**", "sourceEncoding": "GBK" }
56
+ ]
57
+ }
58
+ ```
59
+
60
+ - `sourceEncoding`:检测不确定时的默认编码,也用于新建文件
61
+ - `confidenceThreshold`:自动检测的最小置信度(0-1)
62
+ - `overrides`:可选的目录级编码规则数组,每条规则包含 `pattern`(glob 模式)和 `sourceEncoding`;多个规则匹配时,最具体的模式优先
63
+
44
64
  ## 开发
45
65
 
46
66
  ```bash
package/dist/index.js CHANGED
@@ -29555,7 +29555,21 @@ function registerEditFile(server2, config3, allowedDirectories) {
29555
29555
  originalEncoding = resolvedSourceEncoding;
29556
29556
  }
29557
29557
  const utf8Content = isGBEncoding(originalEncoding) ? decodeToUtf8(buffer, originalEncoding) : buffer.toString("utf-8");
29558
- const modifiedContent = applyEditsToContent(utf8Content, args.edits);
29558
+ let modifiedContent;
29559
+ try {
29560
+ modifiedContent = applyEditsToContent(utf8Content, args.edits);
29561
+ } catch (editError) {
29562
+ const message = editError instanceof Error ? editError.message : String(editError);
29563
+ return {
29564
+ content: [{
29565
+ type: "text",
29566
+ text: `Error: ${message}
29567
+
29568
+ To fix this: call read_file with path "${args.path}" to get the current file content, then retry the edit with the exact text from the file. Do NOT use the built-in Edit tool \u2014 always use encoding-aware-fs MCP tools.`
29569
+ }],
29570
+ isError: true
29571
+ };
29572
+ }
29559
29573
  const diff = createUnifiedDiff(utf8Content, modifiedContent, args.path);
29560
29574
  if (!args.dryRun) {
29561
29575
  const finalContent = restoreLineEndings(modifiedContent, originalLineEnding);
@@ -29978,8 +29992,9 @@ overrides: ${existingConfig.overrides.length} rule(s)` : ""}`,
29978
29992
  confidenceThreshold = existingConfig.confidenceThreshold ?? 0.8;
29979
29993
  overrides = existingConfig.overrides ?? [];
29980
29994
  } else {
29981
- sourceEncoding = await clack2.text({
29995
+ sourceEncoding = await clack2.autocomplete({
29982
29996
  message: "Default source encoding",
29997
+ options: ENCODING_OPTIONS,
29983
29998
  initialValue: "GB18030"
29984
29999
  });
29985
30000
  if (clack2.isCancel(sourceEncoding)) {
@@ -30019,8 +30034,9 @@ overrides: ${existingConfig.overrides.length} rule(s)` : ""}`,
30019
30034
  clack2.cancel("Cancelled");
30020
30035
  process.exit(0);
30021
30036
  }
30022
- const encoding = await clack2.text({
30037
+ const encoding = await clack2.autocomplete({
30023
30038
  message: `Source encoding for ${pattern}`,
30039
+ options: ENCODING_OPTIONS,
30024
30040
  initialValue: "UTF-8"
30025
30041
  });
30026
30042
  if (clack2.isCancel(encoding)) {
@@ -30104,7 +30120,7 @@ async function runInstaller() {
30104
30120
  await promptEncodingConfig(cwd);
30105
30121
  clack2.outro("\u5B89\u88C5\u5B8C\u6210\uFF01\u8BF7\u91CD\u542F Claude Code / OpenCode \u4EE5\u751F\u6548");
30106
30122
  }
30107
- var fs10, path6, import_child_process2, import_util6, clack2, execFileAsync;
30123
+ var fs10, path6, import_child_process2, import_util6, clack2, ENCODING_OPTIONS, execFileAsync;
30108
30124
  var init_installer = __esm({
30109
30125
  "src/installer.ts"() {
30110
30126
  "use strict";
@@ -30114,6 +30130,15 @@ var init_installer = __esm({
30114
30130
  import_util6 = require("util");
30115
30131
  clack2 = __toESM(require("@clack/prompts"));
30116
30132
  init_config_io();
30133
+ ENCODING_OPTIONS = [
30134
+ { value: "GB18030", label: "GB18030 (\u4E2D\u6587\u9ED8\u8BA4)" },
30135
+ { value: "GBK", label: "GBK" },
30136
+ { value: "GB2312", label: "GB2312" },
30137
+ { value: "UTF-8", label: "UTF-8" },
30138
+ { value: "Big5", label: "Big5 (\u7E41\u4F53\u4E2D\u6587)" },
30139
+ { value: "Shift_JIS", label: "Shift_JIS (\u65E5\u6587)" },
30140
+ { value: "EUC-KR", label: "EUC-KR (\u97E9\u6587)" }
30141
+ ];
30117
30142
  execFileAsync = (0, import_util6.promisify)(import_child_process2.execFile);
30118
30143
  }
30119
30144
  });
package/dist/server.js CHANGED
@@ -28746,7 +28746,21 @@ function registerEditFile(server2, config3, allowedDirectories) {
28746
28746
  originalEncoding = resolvedSourceEncoding;
28747
28747
  }
28748
28748
  const utf8Content = isGBEncoding(originalEncoding) ? decodeToUtf8(buffer, originalEncoding) : buffer.toString("utf-8");
28749
- const modifiedContent = applyEditsToContent(utf8Content, args.edits);
28749
+ let modifiedContent;
28750
+ try {
28751
+ modifiedContent = applyEditsToContent(utf8Content, args.edits);
28752
+ } catch (editError) {
28753
+ const message = editError instanceof Error ? editError.message : String(editError);
28754
+ return {
28755
+ content: [{
28756
+ type: "text",
28757
+ text: `Error: ${message}
28758
+
28759
+ To fix this: call read_file with path "${args.path}" to get the current file content, then retry the edit with the exact text from the file. Do NOT use the built-in Edit tool \u2014 always use encoding-aware-fs MCP tools.`
28760
+ }],
28761
+ isError: true
28762
+ };
28763
+ }
28750
28764
  const diff = createUnifiedDiff(utf8Content, modifiedContent, args.path);
28751
28765
  if (!args.dryRun) {
28752
28766
  const finalContent = restoreLineEndings(modifiedContent, originalLineEnding);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "encoding-aware-fs",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Encoding-aware file operations MCP Server for AI tools working with GB18030 projects",
5
5
  "main": "dist/server.js",
6
6
  "bin": {