dsh-zipfix 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +36 -2
- package/client.js +871 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +9 -0
- package/index.js +18 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +77 -4
- package/plugin/index.js +633 -0
- package/plugin/lib/detect.js +436 -0
- package/plugin/lib/encoding.js +369 -0
- package/plugin/lib/fix.js +1228 -0
- package/plugin/lib/report.js +135 -0
- package/plugin/lib/webapi.js +598 -0
- package/plugin/lib/zip.js +842 -0
- package/plugin/locale/en.json +6 -0
- package/plugin/locale/zh.json +6 -0
- package/ui/index.js +26 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 修复报告生成(Markdown + CSV)。
|
|
3
|
+
*
|
|
4
|
+
* 报告是「可审计」要求的载体:每条改动都记录 原名字 → 新名字 → 依据 → 校验结果,
|
|
5
|
+
* 失败项单独列出,绝不静默丢弃。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import fsp from 'node:fs/promises';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
|
|
11
|
+
/** 报告列定义(顺序即表格列顺序)。 */
|
|
12
|
+
const COLUMNS = [
|
|
13
|
+
{ key: 'action', label: '操作' },
|
|
14
|
+
{ key: 'original', label: '原名字' },
|
|
15
|
+
{ key: 'fixed', label: '新名字' },
|
|
16
|
+
{ key: 'basis', label: '依据' },
|
|
17
|
+
{ key: 'verify', label: '校验' },
|
|
18
|
+
{ key: 'note', label: '备注' },
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
function csvCell(value) {
|
|
22
|
+
const text = value === undefined || value === null ? '' : String(value);
|
|
23
|
+
if (/[",\r\n]/.test(text)) return `"${text.replace(/"/g, '""')}"`;
|
|
24
|
+
return text;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function markdownCell(value) {
|
|
28
|
+
const text = value === undefined || value === null ? '' : String(value);
|
|
29
|
+
return text.replace(/\|/g, '\\|').replace(/\r?\n/g, ' ');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 构造报告文本(不落盘)。
|
|
34
|
+
* @param {object} report
|
|
35
|
+
* @param {string} report.title 标题
|
|
36
|
+
* @param {string[]} [report.meta] 头部元信息行
|
|
37
|
+
* @param {Array<{label: string, value: string|number}>} [report.summary] 摘要键值
|
|
38
|
+
* @param {Array<object>} [report.rows] 明细行
|
|
39
|
+
* @param {Array<object>} [report.failures] 失败行
|
|
40
|
+
* @param {Array<string>} [report.notes] 说明与警告
|
|
41
|
+
* @returns {{markdown: string, csv: string}}
|
|
42
|
+
*/
|
|
43
|
+
export function buildReport({ title, generatedAt, meta = [], summary = [], rows = [], failures = [], notes = [] }) {
|
|
44
|
+
const lines = [];
|
|
45
|
+
lines.push(`# ${title}`);
|
|
46
|
+
lines.push('');
|
|
47
|
+
if (generatedAt) lines.push(`- 生成时间:${generatedAt}`);
|
|
48
|
+
for (const item of meta) lines.push(`- ${item.label}:${item.value}`);
|
|
49
|
+
lines.push('');
|
|
50
|
+
if (summary.length > 0) {
|
|
51
|
+
lines.push('## 摘要');
|
|
52
|
+
lines.push('');
|
|
53
|
+
lines.push('| 项目 | 值 |');
|
|
54
|
+
lines.push('| --- | --- |');
|
|
55
|
+
for (const item of summary) lines.push(`| ${markdownCell(item.label)} | ${markdownCell(item.value)} |`);
|
|
56
|
+
lines.push('');
|
|
57
|
+
}
|
|
58
|
+
if (rows.length > 0) {
|
|
59
|
+
lines.push('## 明细');
|
|
60
|
+
lines.push('');
|
|
61
|
+
lines.push(`| ${COLUMNS.map((column) => column.label).join(' | ')} |`);
|
|
62
|
+
lines.push(`| ${COLUMNS.map(() => '---').join(' | ')} |`);
|
|
63
|
+
for (const row of rows) {
|
|
64
|
+
lines.push(`| ${COLUMNS.map((column) => markdownCell(row[column.key])).join(' | ')} |`);
|
|
65
|
+
}
|
|
66
|
+
lines.push('');
|
|
67
|
+
}
|
|
68
|
+
if (failures.length > 0) {
|
|
69
|
+
lines.push('## 失败 / 待人工');
|
|
70
|
+
lines.push('');
|
|
71
|
+
lines.push('| 名字 | 原因 |');
|
|
72
|
+
lines.push('| --- | --- |');
|
|
73
|
+
for (const failure of failures) {
|
|
74
|
+
lines.push(`| ${markdownCell(failure.original ?? failure.path ?? '')} | ${markdownCell(failure.reason ?? '')} |`);
|
|
75
|
+
}
|
|
76
|
+
lines.push('');
|
|
77
|
+
}
|
|
78
|
+
if (notes.length > 0) {
|
|
79
|
+
lines.push('## 说明');
|
|
80
|
+
lines.push('');
|
|
81
|
+
for (const note of notes) lines.push(`- ${note}`);
|
|
82
|
+
lines.push('');
|
|
83
|
+
}
|
|
84
|
+
const csvLines = [];
|
|
85
|
+
csvLines.push(COLUMNS.map((column) => csvCell(column.label)).join(','));
|
|
86
|
+
for (const row of rows) csvLines.push(COLUMNS.map((column) => csvCell(row[column.key])).join(','));
|
|
87
|
+
for (const failure of failures) {
|
|
88
|
+
const row = { action: '待人工', original: failure.original ?? failure.path ?? '', fixed: '', basis: '', verify: '失败', note: failure.reason ?? '' };
|
|
89
|
+
csvLines.push(COLUMNS.map((column) => csvCell(row[column.key])).join(','));
|
|
90
|
+
}
|
|
91
|
+
return { markdown: `${lines.join('\n')}\n`, csv: `${csvLines.join('\r\n')}\r\n` };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 时间戳:YYYYMMDD_HHMMSS(本地时区)。 */
|
|
95
|
+
export function timestamp(date = new Date()) {
|
|
96
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
97
|
+
return (
|
|
98
|
+
`${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}` +
|
|
99
|
+
`_${pad(date.getHours())}${pad(date.getMinutes())}${pad(date.getSeconds())}`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 把报告写到目录下;目标已存在时追加序号,绝不覆盖。
|
|
105
|
+
* @param {string} dir 输出目录
|
|
106
|
+
* @param {string} stem 文件名主干(不含时间戳与扩展名)
|
|
107
|
+
* @param {{markdown: string, csv: string}} report 报告内容
|
|
108
|
+
* @returns {Promise<{markdownPath: string, csvPath: string}>}
|
|
109
|
+
*/
|
|
110
|
+
export async function writeReportFiles(dir, stem, report) {
|
|
111
|
+
await fsp.mkdir(dir, { recursive: true });
|
|
112
|
+
let attempt = 1;
|
|
113
|
+
let base = path.join(dir, `zipfix-${stem}-${timestamp()}`);
|
|
114
|
+
while (true) {
|
|
115
|
+
const markdownPath = `${base}.md`;
|
|
116
|
+
const csvPath = `${base}.csv`;
|
|
117
|
+
try {
|
|
118
|
+
await fsp.writeFile(markdownPath, report.markdown, { flag: 'wx' });
|
|
119
|
+
try {
|
|
120
|
+
await fsp.writeFile(csvPath, report.csv, { flag: 'wx' });
|
|
121
|
+
} catch (error) {
|
|
122
|
+
await fsp.rm(markdownPath, { force: true }).catch(() => {});
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
return { markdownPath, csvPath };
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error && error.code === 'EEXIST') {
|
|
128
|
+
attempt++;
|
|
129
|
+
base = path.join(dir, `zipfix-${stem}-${timestamp()}_${attempt}`);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|