endef 2.0.0 → 2.0.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 +7 -13
- package/package.json +1 -1
- package/src/de.js +19 -6
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
`endef` 是一个纯 Node.js 的文件旁路转换工具。
|
|
4
4
|
|
|
5
|
-
它适合这样的场景:某些常见源码或文本后缀的文件在系统读取、复制、粘贴、编辑器打开时出现乱码或异常,但通过 Node.js 脚本读取仍然正常。`endef` 会先把目标文件内容写到同目录的 `.endef`
|
|
5
|
+
它适合这样的场景:某些常见源码或文本后缀的文件在系统读取、复制、粘贴、编辑器打开时出现乱码或异常,但通过 Node.js 脚本读取仍然正常。`endef` 会先把目标文件内容写到同目录的 `.endef` 副本里,再删除原文件,并把 `.endef` 副本重命名回原文件名。
|
|
6
6
|
|
|
7
7
|
示例:
|
|
8
8
|
|
|
@@ -20,7 +20,7 @@ README.md -> README.md.endef -> README.md
|
|
|
20
20
|
- 默认旁路后缀为 `.endef`
|
|
21
21
|
- `en` 默认转换所有匹配后缀的文件
|
|
22
22
|
- `en --marker` 可以只转换命中 marker 的文件,marker 支持数组配置,也支持 CLI 手动指定多个
|
|
23
|
-
- `de`
|
|
23
|
+
- `de` 会删除原文件,并把 `.endef` 临时副本重命名回原文件名
|
|
24
24
|
- `pack` 会把恢复后的目录打包成 `.tar.gz`
|
|
25
25
|
- 不引入生产依赖
|
|
26
26
|
|
|
@@ -66,7 +66,7 @@ endef en /path/to/project --marker E-SafeNet --marker BadText
|
|
|
66
66
|
endef en /path/to/project --marker E-SafeNet,BadText
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
删除原文件,并把 `.endef` 副本重命名回原文件名:
|
|
70
70
|
|
|
71
71
|
```bash
|
|
72
72
|
endef de /path/to/project
|
|
@@ -107,11 +107,11 @@ endef all [directory] [options]
|
|
|
107
107
|
|
|
108
108
|
`en` 会扫描目标目录,把匹配后缀的文件读取出来,并写成同目录的 `.endef` 副本。
|
|
109
109
|
|
|
110
|
-
`de` 会扫描目标目录中的 `.endef`
|
|
110
|
+
`de` 会扫描目标目录中的 `.endef` 文件,删除对应的原文件,然后把 `.endef` 副本重命名回原文件名。这个命令会直接修改文件,请在执行前确认目录和备份策略。
|
|
111
111
|
|
|
112
112
|
`pack` 会按当前配置扫描当前目录,把没有被 `excludeDirs` 排除的文件打包成 `.tar.gz`。如果输出文件已存在,会直接覆盖。
|
|
113
113
|
|
|
114
|
-
`all` 会按顺序执行 `en`、`de`、`pack
|
|
114
|
+
`all` 会按顺序执行 `en`、`de`、`pack`,适合确认配置后一次性完成旁路转换、重命名恢复和打包。
|
|
115
115
|
|
|
116
116
|
## 参数
|
|
117
117
|
|
|
@@ -222,19 +222,13 @@ endef en . --marker E-SafeNet --marker BadText
|
|
|
222
222
|
file.ext.endef
|
|
223
223
|
```
|
|
224
224
|
|
|
225
|
-
|
|
225
|
+
删除原文件后重命名为:
|
|
226
226
|
|
|
227
227
|
```text
|
|
228
228
|
file.ext
|
|
229
229
|
```
|
|
230
230
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
```text
|
|
234
|
-
file.ext.endef
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
如果 `file.ext` 已经存在,会被 `.endef` 副本内容覆盖。这个行为符合工具的核心假设:原文件可能已经被异常内容污染,而 `.endef` 是通过旁路读取保存出来的可恢复内容。
|
|
231
|
+
如果 `file.ext` 已经存在,会先被删除,然后由 `.endef` 副本重命名得到新的 `file.ext`。这个行为符合工具的核心假设:原文件可能已经被异常内容污染,而 `.endef` 是通过旁路读取保存出来的可恢复内容。
|
|
238
232
|
|
|
239
233
|
## 打包策略
|
|
240
234
|
|
package/package.json
CHANGED
package/src/de.js
CHANGED
|
@@ -30,26 +30,39 @@ async function restoreFiles(directory, options = {}) {
|
|
|
30
30
|
})
|
|
31
31
|
|
|
32
32
|
await Promise.all(tasks)
|
|
33
|
-
console.log(`Done. restored=${stats.restored},
|
|
33
|
+
console.log(`Done. restored=${stats.restored}, removed=${stats.removed}, renamed=${stats.renamed}, failed=${stats.failed}`)
|
|
34
34
|
return stats
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
async function restoreOne(filePath, config, stats) {
|
|
38
38
|
const targetPath = filePath.slice(0, -config.suffix.length)
|
|
39
|
-
const
|
|
39
|
+
const targetExists = await exists(targetPath)
|
|
40
40
|
|
|
41
|
-
await fs.
|
|
42
|
-
await fs.
|
|
41
|
+
await fs.rm(targetPath, { force: true })
|
|
42
|
+
await fs.rename(filePath, targetPath)
|
|
43
43
|
|
|
44
44
|
stats.restored += 1
|
|
45
|
-
|
|
45
|
+
if (targetExists) {
|
|
46
|
+
stats.removed += 1
|
|
47
|
+
}
|
|
48
|
+
stats.renamed += 1
|
|
46
49
|
console.log(`Restored: ${filePath} -> ${targetPath}`)
|
|
47
50
|
}
|
|
48
51
|
|
|
52
|
+
async function exists(filePath) {
|
|
53
|
+
try {
|
|
54
|
+
await fs.access(filePath)
|
|
55
|
+
return true
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
49
61
|
function createStats() {
|
|
50
62
|
return {
|
|
51
63
|
restored: 0,
|
|
52
|
-
|
|
64
|
+
removed: 0,
|
|
65
|
+
renamed: 0,
|
|
53
66
|
failed: 0
|
|
54
67
|
}
|
|
55
68
|
}
|