codexmate 0.0.28 → 0.0.29
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 +421 -421
- package/README.zh.md +354 -354
- package/cli/agents-files.js +224 -224
- package/cli/archive-helpers.js +446 -446
- package/cli/auth-profiles.js +375 -375
- package/cli/builtin-proxy.js +1725 -1620
- package/cli/claude-proxy.js +1022 -1022
- package/cli/config-bootstrap.js +384 -384
- package/cli/config-health.js +338 -338
- package/cli/doctor-core.js +903 -903
- package/cli/import-skills-url.js +356 -356
- package/cli/openai-bridge.js +1576 -1489
- package/cli/openclaw-config.js +629 -629
- package/cli/session-convert-args.js +65 -65
- package/cli/session-convert-io.js +82 -82
- package/cli/session-convert.js +43 -43
- package/cli/session-usage.concurrent.js +28 -28
- package/cli/session-usage.js +118 -118
- package/cli/session-usage.models.js +176 -176
- package/cli/skills.js +1141 -1141
- package/cli/zip-commands.js +510 -510
- package/cli.js +15340 -15264
- package/lib/automation.js +404 -404
- package/lib/cli-file-utils.js +151 -151
- package/lib/cli-models-utils.js +440 -440
- package/lib/cli-network-utils.js +190 -190
- package/lib/cli-path-utils.js +85 -85
- package/lib/cli-session-utils.js +121 -121
- package/lib/cli-sessions.js +417 -417
- package/lib/cli-utils.js +155 -155
- package/lib/cli-webhook.js +126 -0
- package/lib/download-artifacts.js +92 -92
- package/lib/mcp-stdio.js +453 -453
- package/lib/task-orchestrator.js +869 -869
- package/lib/text-diff.js +303 -303
- package/lib/workflow-engine.js +340 -340
- package/package.json +76 -76
- package/plugins/README.md +20 -20
- package/plugins/README.zh-CN.md +20 -20
- package/plugins/prompt-templates/comment-polish/index.mjs +25 -25
- package/plugins/prompt-templates/computed.mjs +253 -253
- package/plugins/prompt-templates/index.mjs +8 -8
- package/plugins/prompt-templates/manifest.mjs +15 -15
- package/plugins/prompt-templates/methods.mjs +553 -553
- package/plugins/prompt-templates/overview.mjs +91 -91
- package/plugins/prompt-templates/ownership.mjs +19 -19
- package/plugins/prompt-templates/rule-ack/index.mjs +21 -21
- package/plugins/prompt-templates/storage.mjs +64 -64
- package/plugins/registry.mjs +16 -16
- package/web-ui/app.js +639 -625
- package/web-ui/index.html +36 -35
- package/web-ui/logic.agents-diff.mjs +386 -386
- package/web-ui/logic.claude.mjs +168 -168
- package/web-ui/logic.codex.mjs +69 -56
- package/web-ui/logic.mjs +5 -5
- package/web-ui/logic.runtime.mjs +128 -128
- package/web-ui/logic.session-convert.mjs +70 -70
- package/web-ui/logic.sessions.mjs +765 -765
- package/web-ui/modules/api.mjs +90 -90
- package/web-ui/modules/app.computed.dashboard.mjs +225 -225
- package/web-ui/modules/app.computed.index.mjs +17 -17
- package/web-ui/modules/app.computed.main-tabs.mjs +205 -205
- package/web-ui/modules/app.computed.session.mjs +999 -994
- package/web-ui/modules/app.constants.mjs +15 -15
- package/web-ui/modules/app.methods.agents.mjs +632 -632
- package/web-ui/modules/app.methods.claude-config.mjs +200 -190
- package/web-ui/modules/app.methods.codex-config.mjs +892 -892
- package/web-ui/modules/app.methods.index.mjs +94 -92
- package/web-ui/modules/app.methods.install.mjs +205 -205
- package/web-ui/modules/app.methods.navigation.mjs +761 -743
- package/web-ui/modules/app.methods.openclaw-core.mjs +814 -814
- package/web-ui/modules/app.methods.openclaw-editing.mjs +372 -372
- package/web-ui/modules/app.methods.openclaw-persist.mjs +369 -369
- package/web-ui/modules/app.methods.providers.mjs +493 -412
- package/web-ui/modules/app.methods.runtime.mjs +345 -345
- package/web-ui/modules/app.methods.session-actions.mjs +593 -593
- package/web-ui/modules/app.methods.session-browser.mjs +984 -984
- package/web-ui/modules/app.methods.session-timeline.mjs +479 -479
- package/web-ui/modules/app.methods.session-trash.mjs +438 -439
- package/web-ui/modules/app.methods.startup-claude.mjs +533 -533
- package/web-ui/modules/app.methods.task-orchestration.mjs +556 -556
- package/web-ui/modules/app.methods.webhook.mjs +79 -0
- package/web-ui/modules/config-mode.computed.mjs +124 -124
- package/web-ui/modules/config-template-confirm-pref.mjs +33 -33
- package/web-ui/modules/i18n.dict.mjs +3131 -2109
- package/web-ui/modules/i18n.mjs +62 -56
- package/web-ui/modules/plugins.computed.mjs +3 -3
- package/web-ui/modules/plugins.methods.mjs +3 -3
- package/web-ui/modules/plugins.storage.mjs +11 -11
- package/web-ui/modules/provider-url-display.mjs +17 -17
- package/web-ui/modules/sessions-filters-url.mjs +85 -85
- package/web-ui/modules/skills.computed.mjs +107 -107
- package/web-ui/modules/skills.methods.mjs +481 -481
- package/web-ui/partials/index/layout-footer.html +13 -13
- package/web-ui/partials/index/layout-header.html +500 -475
- package/web-ui/partials/index/modal-config-template-agents.html +174 -174
- package/web-ui/partials/index/modal-confirm-toast.html +32 -32
- package/web-ui/partials/index/modal-health-check.html +45 -45
- package/web-ui/partials/index/modal-openclaw-config.html +280 -280
- package/web-ui/partials/index/modal-skills.html +200 -200
- package/web-ui/partials/index/modals-basic.html +162 -165
- package/web-ui/partials/index/panel-config-claude.html +194 -188
- package/web-ui/partials/index/panel-config-codex.html +323 -312
- package/web-ui/partials/index/panel-config-openclaw.html +83 -83
- package/web-ui/partials/index/panel-dashboard.html +186 -186
- package/web-ui/partials/index/panel-docs.html +147 -147
- package/web-ui/partials/index/panel-market.html +177 -177
- package/web-ui/partials/index/panel-orchestration.html +391 -391
- package/web-ui/partials/index/panel-plugins.html +253 -253
- package/web-ui/partials/index/panel-sessions.html +316 -316
- package/web-ui/partials/index/panel-settings.html +190 -253
- package/web-ui/partials/index/panel-trash.html +88 -0
- package/web-ui/partials/index/panel-usage.html +371 -371
- package/web-ui/res/json5.min.js +1 -1
- package/web-ui/res/vue.global.prod.js +13 -13
- package/web-ui/session-helpers.mjs +576 -576
- package/web-ui/source-bundle.cjs +233 -233
- package/web-ui/styles/base-theme.css +281 -281
- package/web-ui/styles/controls-forms.css +422 -422
- package/web-ui/styles/dashboard.css +274 -274
- package/web-ui/styles/docs-panel.css +271 -247
- package/web-ui/styles/feedback.css +108 -108
- package/web-ui/styles/health-check-dialog.css +144 -144
- package/web-ui/styles/layout-shell.css +626 -606
- package/web-ui/styles/modals-core.css +466 -466
- package/web-ui/styles/navigation-panels.css +391 -391
- package/web-ui/styles/openclaw-structured.css +266 -266
- package/web-ui/styles/plugins-panel.css +523 -523
- package/web-ui/styles/responsive.css +454 -454
- package/web-ui/styles/sessions-list.css +419 -419
- package/web-ui/styles/sessions-preview.css +411 -411
- package/web-ui/styles/sessions-toolbar-trash.css +330 -330
- package/web-ui/styles/sessions-usage.css +1040 -1040
- package/web-ui/styles/settings-panel.css +349 -185
- package/web-ui/styles/skills-list.css +303 -303
- package/web-ui/styles/skills-market.css +406 -406
- package/web-ui/styles/task-orchestration.css +822 -822
- package/web-ui/styles/titles-cards.css +472 -472
- package/web-ui/styles/trash-panel.css +90 -0
- package/web-ui/styles/webhook.css +81 -0
- package/web-ui/styles.css +23 -21
- package/web-ui.html +17 -17
package/cli/zip-commands.js
CHANGED
|
@@ -1,510 +1,510 @@
|
|
|
1
|
-
function createZipCommandController(deps = {}) {
|
|
2
|
-
const {
|
|
3
|
-
fs,
|
|
4
|
-
path,
|
|
5
|
-
execSync,
|
|
6
|
-
process,
|
|
7
|
-
yauzl,
|
|
8
|
-
ensureDir,
|
|
9
|
-
formatTimestampForFileName,
|
|
10
|
-
inspectZipArchiveLimits,
|
|
11
|
-
resolveZipTool,
|
|
12
|
-
resolveUnzipTool,
|
|
13
|
-
zipWithLibrary,
|
|
14
|
-
unzipWithLibrary,
|
|
15
|
-
DEFAULT_EXTRACT_SUFFIXES,
|
|
16
|
-
MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
17
|
-
MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES,
|
|
18
|
-
} = deps;
|
|
19
|
-
|
|
20
|
-
if (!fs) throw new Error('createZipCommandController 缺少 fs');
|
|
21
|
-
if (!path) throw new Error('createZipCommandController 缺少 path');
|
|
22
|
-
if (typeof execSync !== 'function') throw new Error('createZipCommandController 缺少 execSync');
|
|
23
|
-
if (!process) throw new Error('createZipCommandController 缺少 process');
|
|
24
|
-
if (!yauzl) throw new Error('createZipCommandController 缺少 yauzl');
|
|
25
|
-
if (typeof ensureDir !== 'function') throw new Error('createZipCommandController 缺少 ensureDir');
|
|
26
|
-
if (typeof formatTimestampForFileName !== 'function') throw new Error('createZipCommandController 缺少 formatTimestampForFileName');
|
|
27
|
-
if (typeof inspectZipArchiveLimits !== 'function') throw new Error('createZipCommandController 缺少 inspectZipArchiveLimits');
|
|
28
|
-
if (typeof resolveZipTool !== 'function') throw new Error('createZipCommandController 缺少 resolveZipTool');
|
|
29
|
-
if (typeof resolveUnzipTool !== 'function') throw new Error('createZipCommandController 缺少 resolveUnzipTool');
|
|
30
|
-
if (typeof zipWithLibrary !== 'function') throw new Error('createZipCommandController 缺少 zipWithLibrary');
|
|
31
|
-
if (typeof unzipWithLibrary !== 'function') throw new Error('createZipCommandController 缺少 unzipWithLibrary');
|
|
32
|
-
if (!Array.isArray(DEFAULT_EXTRACT_SUFFIXES)) throw new Error('createZipCommandController 缺少 DEFAULT_EXTRACT_SUFFIXES');
|
|
33
|
-
if (!Number.isFinite(MAX_SKILLS_ZIP_ENTRY_COUNT)) throw new Error('createZipCommandController 缺少 MAX_SKILLS_ZIP_ENTRY_COUNT');
|
|
34
|
-
if (!Number.isFinite(MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES)) throw new Error('createZipCommandController 缺少 MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES');
|
|
35
|
-
|
|
36
|
-
async function cmdZip(targetPath, options = {}) {
|
|
37
|
-
if (!targetPath) {
|
|
38
|
-
console.error('用法: codexmate zip <文件或文件夹路径> [--max:压缩级别]');
|
|
39
|
-
console.log('\n示例:');
|
|
40
|
-
console.log(' codexmate zip ./myproject');
|
|
41
|
-
console.log(' codexmate zip ./myproject --max:9');
|
|
42
|
-
console.log(' codexmate zip D:/data/folder --max:1');
|
|
43
|
-
console.log('\n压缩级别: 0(仅存储) ~ 9(极限压缩), 默认: 5');
|
|
44
|
-
process.exit(1);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const absPath = path.resolve(targetPath);
|
|
48
|
-
if (!fs.existsSync(absPath)) {
|
|
49
|
-
console.error('错误: 路径不存在:', absPath);
|
|
50
|
-
process.exit(1);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const compressionLevel = options.max !== undefined ? options.max : 5;
|
|
54
|
-
if (compressionLevel < 0 || compressionLevel > 9) {
|
|
55
|
-
console.error('错误: 压缩级别必须在 0-9 之间');
|
|
56
|
-
process.exit(1);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const baseName = path.basename(absPath);
|
|
60
|
-
const outputDir = path.dirname(absPath);
|
|
61
|
-
const outputPath = path.join(outputDir, `${baseName}.zip`);
|
|
62
|
-
|
|
63
|
-
const zipTool = resolveZipTool();
|
|
64
|
-
const useZipCmd = zipTool.type === 'zip';
|
|
65
|
-
|
|
66
|
-
console.log('\n压缩配置:');
|
|
67
|
-
console.log(' 源路径:', absPath);
|
|
68
|
-
console.log(' 输出文件:', outputPath);
|
|
69
|
-
console.log(' 压缩工具:', useZipCmd ? '系统 zip' : 'zip-lib');
|
|
70
|
-
if (useZipCmd) {
|
|
71
|
-
console.log(' 压缩级别:', compressionLevel);
|
|
72
|
-
} else {
|
|
73
|
-
console.log(' 压缩级别: 固定(zip-lib 不支持 --max,已忽略)');
|
|
74
|
-
}
|
|
75
|
-
console.log('\n开始压缩...\n');
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
if (useZipCmd) {
|
|
79
|
-
const cmd = `"${zipTool.cmd}" -${compressionLevel} -q -r "${outputPath}" "${absPath}"`;
|
|
80
|
-
execSync(cmd, { stdio: 'ignore' });
|
|
81
|
-
} else {
|
|
82
|
-
await zipWithLibrary(absPath, outputPath);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
console.log('✓ 压缩完成!');
|
|
86
|
-
console.log(' 输出文件:', outputPath);
|
|
87
|
-
console.log();
|
|
88
|
-
} catch (e) {
|
|
89
|
-
console.error('压缩失败:', e.message);
|
|
90
|
-
process.exit(1);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async function cmdUnzip(zipPath, outputDir) {
|
|
95
|
-
if (!zipPath) {
|
|
96
|
-
console.error('用法: codexmate unzip <zip文件路径> [输出目录]');
|
|
97
|
-
console.log('\n示例:');
|
|
98
|
-
console.log(' codexmate unzip ./archive.zip');
|
|
99
|
-
console.log(' codexmate unzip ./archive.zip ./output');
|
|
100
|
-
console.log(' codexmate unzip D:/data/file.zip D:/extracted');
|
|
101
|
-
process.exit(1);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const absZipPath = path.resolve(zipPath);
|
|
105
|
-
if (!fs.existsSync(absZipPath)) {
|
|
106
|
-
console.error('错误: 文件不存在:', absZipPath);
|
|
107
|
-
process.exit(1);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (!absZipPath.toLowerCase().endsWith('.zip')) {
|
|
111
|
-
console.error('错误: 仅支持 .zip 文件');
|
|
112
|
-
process.exit(1);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const baseName = path.basename(absZipPath, '.zip');
|
|
116
|
-
const defaultOutputDir = path.join(path.dirname(absZipPath), baseName);
|
|
117
|
-
const absOutputDir = outputDir ? path.resolve(outputDir) : defaultOutputDir;
|
|
118
|
-
|
|
119
|
-
resolveUnzipTool();
|
|
120
|
-
|
|
121
|
-
console.log('\n解压配置:');
|
|
122
|
-
console.log(' 源文件:', absZipPath);
|
|
123
|
-
console.log(' 输出目录:', absOutputDir);
|
|
124
|
-
console.log(' 解压工具:', 'zip-lib');
|
|
125
|
-
console.log('\n开始解压...\n');
|
|
126
|
-
|
|
127
|
-
try {
|
|
128
|
-
await unzipWithLibrary(absZipPath, absOutputDir);
|
|
129
|
-
console.log('✓ 解压完成!');
|
|
130
|
-
console.log(' 输出目录:', absOutputDir);
|
|
131
|
-
console.log();
|
|
132
|
-
} catch (e) {
|
|
133
|
-
console.error('解压失败:', e.message);
|
|
134
|
-
process.exit(1);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function splitExtractSuffixInput(rawValue) {
|
|
139
|
-
if (Array.isArray(rawValue)) {
|
|
140
|
-
return rawValue.flatMap((item) => splitExtractSuffixInput(item));
|
|
141
|
-
}
|
|
142
|
-
if (typeof rawValue !== 'string') {
|
|
143
|
-
return [];
|
|
144
|
-
}
|
|
145
|
-
return rawValue
|
|
146
|
-
.split(/[,\s]+/g)
|
|
147
|
-
.map((item) => item.trim())
|
|
148
|
-
.filter(Boolean);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function normalizeExtractSuffix(rawSuffix, fallbackSuffixes = DEFAULT_EXTRACT_SUFFIXES) {
|
|
152
|
-
const fallbackItems = splitExtractSuffixInput(fallbackSuffixes);
|
|
153
|
-
const sourceItems = splitExtractSuffixInput(rawSuffix);
|
|
154
|
-
const source = sourceItems.length > 0 ? sourceItems : fallbackItems;
|
|
155
|
-
const dedup = new Set();
|
|
156
|
-
|
|
157
|
-
for (const item of source) {
|
|
158
|
-
const lower = item.toLowerCase();
|
|
159
|
-
if (!lower) {
|
|
160
|
-
continue;
|
|
161
|
-
}
|
|
162
|
-
const normalized = lower.startsWith('.') ? lower : `.${lower}`;
|
|
163
|
-
if (normalized.length > 1) {
|
|
164
|
-
dedup.add(normalized);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (dedup.size === 0) {
|
|
169
|
-
return [...DEFAULT_EXTRACT_SUFFIXES];
|
|
170
|
-
}
|
|
171
|
-
return Array.from(dedup);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function buildDefaultExtractOutputDir(baseCwd = process.cwd()) {
|
|
175
|
-
const normalizedCwd = path.resolve(baseCwd);
|
|
176
|
-
const parentDir = path.dirname(normalizedCwd);
|
|
177
|
-
const timestamp = formatTimestampForFileName().replace(/-/g, '');
|
|
178
|
-
return path.join(parentDir, timestamp);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function sanitizeNameSegment(rawValue, fallback = 'item') {
|
|
182
|
-
const value = typeof rawValue === 'string' ? rawValue.trim() : '';
|
|
183
|
-
const sanitized = value
|
|
184
|
-
.replace(/[^\w.-]+/g, '_')
|
|
185
|
-
.replace(/^_+|_+$/g, '');
|
|
186
|
-
return sanitized || fallback;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function resolveDuplicateOutputPath(outputDir, originalFileName, zipPath = '', counters = new Map()) {
|
|
190
|
-
const fallbackName = `file${path.extname(originalFileName || '')}`;
|
|
191
|
-
const fileName = path.basename(originalFileName || '') || fallbackName;
|
|
192
|
-
const firstChoice = path.join(outputDir, fileName);
|
|
193
|
-
const firstChoiceKey = `exact:${fileName}`;
|
|
194
|
-
if (!counters.has(firstChoiceKey)) {
|
|
195
|
-
counters.set(firstChoiceKey, true);
|
|
196
|
-
if (!fs.existsSync(firstChoice)) {
|
|
197
|
-
return firstChoice;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const ext = path.extname(fileName);
|
|
202
|
-
const baseName = path.basename(fileName, ext);
|
|
203
|
-
const safeBaseName = sanitizeNameSegment(baseName, 'file');
|
|
204
|
-
const zipBaseName = sanitizeNameSegment(path.basename(zipPath || '', '.zip'), 'zip');
|
|
205
|
-
const duplicateKey = `dup:${safeBaseName}|${zipBaseName}|${ext}`;
|
|
206
|
-
let index = counters.has(duplicateKey) ? counters.get(duplicateKey) : 1;
|
|
207
|
-
|
|
208
|
-
for (; index <= 100000; index++) {
|
|
209
|
-
const candidateName = `${safeBaseName}__${zipBaseName}__${index}${ext}`;
|
|
210
|
-
const candidatePath = path.join(outputDir, candidateName);
|
|
211
|
-
if (!fs.existsSync(candidatePath)) {
|
|
212
|
-
counters.set(duplicateKey, index + 1);
|
|
213
|
-
return candidatePath;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
throw new Error(`重名文件过多,无法生成唯一文件名: ${fileName}`);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function collectZipFilesFromDir(rootDir, recursive = true) {
|
|
221
|
-
const queue = [rootDir];
|
|
222
|
-
const result = [];
|
|
223
|
-
|
|
224
|
-
while (queue.length > 0) {
|
|
225
|
-
const currentDir = queue.shift();
|
|
226
|
-
let entries = [];
|
|
227
|
-
try {
|
|
228
|
-
entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
229
|
-
} catch (e) {
|
|
230
|
-
throw new Error(`读取目录失败: ${currentDir} (${e.message})`);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
for (const entry of entries) {
|
|
234
|
-
const entryPath = path.join(currentDir, entry.name);
|
|
235
|
-
if (entry.isDirectory()) {
|
|
236
|
-
if (recursive) {
|
|
237
|
-
queue.push(entryPath);
|
|
238
|
-
}
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
if (entry.isFile() && entry.name.toLowerCase().endsWith('.zip')) {
|
|
242
|
-
result.push(entryPath);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
result.sort((a, b) => a.localeCompare(b));
|
|
248
|
-
return result;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function extractMatchedEntriesFromZip(zipPath, outputDir, suffixes, duplicateCounters = new Map()) {
|
|
252
|
-
const normalizedSuffixes = normalizeExtractSuffix(suffixes);
|
|
253
|
-
return new Promise((resolve, reject) => {
|
|
254
|
-
yauzl.open(zipPath, { lazyEntries: true, autoClose: false }, (openErr, zipFile) => {
|
|
255
|
-
if (openErr) {
|
|
256
|
-
reject(openErr);
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
259
|
-
if (!zipFile) {
|
|
260
|
-
reject(new Error('无法读取 ZIP 文件'));
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
let settled = false;
|
|
265
|
-
let matched = 0;
|
|
266
|
-
let extracted = 0;
|
|
267
|
-
let skippedDir = 0;
|
|
268
|
-
let skippedExt = 0;
|
|
269
|
-
|
|
270
|
-
const finish = (err) => {
|
|
271
|
-
if (settled) return;
|
|
272
|
-
settled = true;
|
|
273
|
-
try {
|
|
274
|
-
zipFile.close();
|
|
275
|
-
} catch (_) {}
|
|
276
|
-
if (err) {
|
|
277
|
-
reject(err);
|
|
278
|
-
} else {
|
|
279
|
-
resolve({ matched, extracted, skippedDir, skippedExt });
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
zipFile.on('entry', (entry) => {
|
|
284
|
-
if (settled) return;
|
|
285
|
-
const rawEntryName = typeof entry.fileName === 'string' ? entry.fileName : '';
|
|
286
|
-
const normalizedEntryName = rawEntryName.replace(/\\/g, '/');
|
|
287
|
-
|
|
288
|
-
if (!normalizedEntryName || normalizedEntryName.endsWith('/')) {
|
|
289
|
-
skippedDir += 1;
|
|
290
|
-
zipFile.readEntry();
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const entryBaseName = path.basename(normalizedEntryName);
|
|
295
|
-
const lowerBaseName = entryBaseName.toLowerCase();
|
|
296
|
-
const matchedSuffix = normalizedSuffixes.some((suffix) => lowerBaseName.endsWith(suffix));
|
|
297
|
-
if (!entryBaseName || !matchedSuffix) {
|
|
298
|
-
skippedExt += 1;
|
|
299
|
-
zipFile.readEntry();
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
matched += 1;
|
|
304
|
-
zipFile.openReadStream(entry, (streamErr, readStream) => {
|
|
305
|
-
if (streamErr || !readStream) {
|
|
306
|
-
finish(streamErr || new Error('无法读取 ZIP 条目流'));
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
let completed = false;
|
|
311
|
-
const outputPath = resolveDuplicateOutputPath(outputDir, entryBaseName, zipPath, duplicateCounters);
|
|
312
|
-
const writeStream = fs.createWriteStream(outputPath);
|
|
313
|
-
const fail = (writeErr) => {
|
|
314
|
-
if (completed) return;
|
|
315
|
-
completed = true;
|
|
316
|
-
try {
|
|
317
|
-
readStream.destroy();
|
|
318
|
-
} catch (_) {}
|
|
319
|
-
try {
|
|
320
|
-
writeStream.destroy();
|
|
321
|
-
} catch (_) {}
|
|
322
|
-
try {
|
|
323
|
-
if (fs.existsSync(outputPath)) {
|
|
324
|
-
fs.unlinkSync(outputPath);
|
|
325
|
-
}
|
|
326
|
-
} catch (_) {}
|
|
327
|
-
finish(writeErr);
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
readStream.on('error', fail);
|
|
331
|
-
writeStream.on('error', fail);
|
|
332
|
-
writeStream.on('finish', () => {
|
|
333
|
-
if (completed || settled) return;
|
|
334
|
-
completed = true;
|
|
335
|
-
extracted += 1;
|
|
336
|
-
zipFile.readEntry();
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
readStream.pipe(writeStream);
|
|
340
|
-
});
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
zipFile.on('end', () => {
|
|
344
|
-
finish(null);
|
|
345
|
-
});
|
|
346
|
-
zipFile.on('error', (zipErr) => {
|
|
347
|
-
finish(zipErr);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
zipFile.readEntry();
|
|
351
|
-
});
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
async function cmdUnzipExt(zipDirPath, outputDir, options = {}) {
|
|
356
|
-
if (!zipDirPath) {
|
|
357
|
-
console.error('用法: codexmate unzip-ext <zip目录> [输出目录] [--ext:后缀[,后缀...]] [--no-recursive]');
|
|
358
|
-
console.log('\n示例:');
|
|
359
|
-
console.log(' codexmate unzip-ext ./archives');
|
|
360
|
-
console.log(' codexmate unzip-ext ./archives ./output --ext:json,txt');
|
|
361
|
-
console.log(' codexmate unzip-ext D:/data/zips --ext:txt --no-recursive');
|
|
362
|
-
console.log(' 说明: 默认递归扫描子目录,可通过 --no-recursive 关闭递归');
|
|
363
|
-
process.exit(1);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
const recursive = options.recursive !== false;
|
|
367
|
-
const suffixes = normalizeExtractSuffix(options.ext);
|
|
368
|
-
const absZipDir = path.resolve(zipDirPath);
|
|
369
|
-
const absOutputDir = outputDir ? path.resolve(outputDir) : buildDefaultExtractOutputDir(process.cwd());
|
|
370
|
-
|
|
371
|
-
if (!fs.existsSync(absZipDir)) {
|
|
372
|
-
console.error('错误: 目录不存在:', absZipDir);
|
|
373
|
-
process.exit(1);
|
|
374
|
-
}
|
|
375
|
-
try {
|
|
376
|
-
if (!fs.statSync(absZipDir).isDirectory()) {
|
|
377
|
-
console.error('错误: 仅支持目录路径:', absZipDir);
|
|
378
|
-
process.exit(1);
|
|
379
|
-
}
|
|
380
|
-
} catch (e) {
|
|
381
|
-
console.error('错误: 无法读取目录信息:', e.message);
|
|
382
|
-
process.exit(1);
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
let zipFiles = [];
|
|
386
|
-
try {
|
|
387
|
-
zipFiles = collectZipFilesFromDir(absZipDir, recursive);
|
|
388
|
-
} catch (e) {
|
|
389
|
-
console.error('扫描 ZIP 文件失败:', e.message);
|
|
390
|
-
process.exit(1);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
if (zipFiles.length === 0) {
|
|
394
|
-
console.error('错误: 未找到任何 ZIP 文件');
|
|
395
|
-
process.exit(1);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
ensureDir(absOutputDir);
|
|
399
|
-
|
|
400
|
-
console.log('\n批量解压配置:');
|
|
401
|
-
console.log(' ZIP 目录:', absZipDir);
|
|
402
|
-
console.log(' 输出目录:', absOutputDir);
|
|
403
|
-
console.log(' 后缀过滤:', suffixes.join(', '));
|
|
404
|
-
console.log(' 递归扫描:', recursive ? '是' : '否');
|
|
405
|
-
console.log(' ZIP 数量:', zipFiles.length);
|
|
406
|
-
console.log('\n开始提取...\n');
|
|
407
|
-
|
|
408
|
-
let totalMatched = 0;
|
|
409
|
-
let totalExtracted = 0;
|
|
410
|
-
let totalSkippedDir = 0;
|
|
411
|
-
let totalSkippedExt = 0;
|
|
412
|
-
const failed = [];
|
|
413
|
-
const duplicateCounters = new Map();
|
|
414
|
-
|
|
415
|
-
for (const zipFilePath of zipFiles) {
|
|
416
|
-
try {
|
|
417
|
-
await inspectZipArchiveLimits(zipFilePath, {
|
|
418
|
-
maxEntryCount: MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
419
|
-
maxUncompressedBytes: MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES
|
|
420
|
-
});
|
|
421
|
-
const result = await extractMatchedEntriesFromZip(zipFilePath, absOutputDir, suffixes, duplicateCounters);
|
|
422
|
-
totalMatched += result.matched;
|
|
423
|
-
totalExtracted += result.extracted;
|
|
424
|
-
totalSkippedDir += result.skippedDir;
|
|
425
|
-
totalSkippedExt += result.skippedExt;
|
|
426
|
-
console.log(`✓ ${path.basename(zipFilePath)}: 命中 ${result.matched},提取 ${result.extracted}`);
|
|
427
|
-
} catch (e) {
|
|
428
|
-
failed.push({ zipFilePath, message: e && e.message ? e.message : String(e) });
|
|
429
|
-
console.error(`✗ ${path.basename(zipFilePath)}: ${e && e.message ? e.message : e}`);
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
console.log('\n提取结果:');
|
|
434
|
-
console.log(' 输出目录:', absOutputDir);
|
|
435
|
-
console.log(' 扫描 ZIP:', zipFiles.length);
|
|
436
|
-
console.log(' 命中条目:', totalMatched);
|
|
437
|
-
console.log(' 已提取:', totalExtracted);
|
|
438
|
-
console.log(' 已跳过(目录条目):', totalSkippedDir);
|
|
439
|
-
console.log(' 已跳过(后缀不匹配):', totalSkippedExt);
|
|
440
|
-
if (failed.length > 0) {
|
|
441
|
-
console.error(' 失败数量:', failed.length);
|
|
442
|
-
for (const item of failed) {
|
|
443
|
-
console.error(` - ${item.zipFilePath}: ${item.message}`);
|
|
444
|
-
}
|
|
445
|
-
process.exit(1);
|
|
446
|
-
}
|
|
447
|
-
console.log();
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
function parseZipCommandArgs(args = []) {
|
|
451
|
-
const options = {};
|
|
452
|
-
let targetPath = null;
|
|
453
|
-
for (let i = 0; i < args.length; i++) {
|
|
454
|
-
const arg = args[i];
|
|
455
|
-
if (typeof arg !== 'string' || !arg) continue;
|
|
456
|
-
if (arg.startsWith('--max:')) {
|
|
457
|
-
options.max = parseInt(arg.substring(6), 10);
|
|
458
|
-
} else if (!targetPath) {
|
|
459
|
-
targetPath = arg;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
return { targetPath, options };
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
function parseUnzipExtCommandArgs(args = []) {
|
|
466
|
-
const options = {
|
|
467
|
-
ext: [],
|
|
468
|
-
recursive: true
|
|
469
|
-
};
|
|
470
|
-
let zipDirPath = null;
|
|
471
|
-
let outputDir = null;
|
|
472
|
-
for (let i = 0; i < args.length; i++) {
|
|
473
|
-
const arg = args[i];
|
|
474
|
-
if (typeof arg !== 'string' || !arg) continue;
|
|
475
|
-
if (arg.startsWith('--ext:')) {
|
|
476
|
-
options.ext.push(...splitExtractSuffixInput(arg.substring(6)));
|
|
477
|
-
} else if (arg.startsWith('--ext=')) {
|
|
478
|
-
options.ext.push(...splitExtractSuffixInput(arg.substring(6)));
|
|
479
|
-
} else if (arg === '--ext') {
|
|
480
|
-
const nextArg = args[i + 1];
|
|
481
|
-
if (typeof nextArg === 'string' && !nextArg.startsWith('--')) {
|
|
482
|
-
options.ext.push(...splitExtractSuffixInput(nextArg));
|
|
483
|
-
i += 1;
|
|
484
|
-
}
|
|
485
|
-
} else if (arg === '--recursive') {
|
|
486
|
-
options.recursive = true;
|
|
487
|
-
} else if (arg === '--no-recursive') {
|
|
488
|
-
options.recursive = false;
|
|
489
|
-
} else if (!zipDirPath) {
|
|
490
|
-
zipDirPath = arg;
|
|
491
|
-
} else if (!outputDir) {
|
|
492
|
-
outputDir = arg;
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
return { zipDirPath, outputDir, options };
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
return {
|
|
499
|
-
cmdZip,
|
|
500
|
-
cmdUnzip,
|
|
501
|
-
cmdUnzipExt,
|
|
502
|
-
splitExtractSuffixInput,
|
|
503
|
-
parseZipCommandArgs,
|
|
504
|
-
parseUnzipExtCommandArgs
|
|
505
|
-
};
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
module.exports = {
|
|
509
|
-
createZipCommandController
|
|
510
|
-
};
|
|
1
|
+
function createZipCommandController(deps = {}) {
|
|
2
|
+
const {
|
|
3
|
+
fs,
|
|
4
|
+
path,
|
|
5
|
+
execSync,
|
|
6
|
+
process,
|
|
7
|
+
yauzl,
|
|
8
|
+
ensureDir,
|
|
9
|
+
formatTimestampForFileName,
|
|
10
|
+
inspectZipArchiveLimits,
|
|
11
|
+
resolveZipTool,
|
|
12
|
+
resolveUnzipTool,
|
|
13
|
+
zipWithLibrary,
|
|
14
|
+
unzipWithLibrary,
|
|
15
|
+
DEFAULT_EXTRACT_SUFFIXES,
|
|
16
|
+
MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
17
|
+
MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES,
|
|
18
|
+
} = deps;
|
|
19
|
+
|
|
20
|
+
if (!fs) throw new Error('createZipCommandController 缺少 fs');
|
|
21
|
+
if (!path) throw new Error('createZipCommandController 缺少 path');
|
|
22
|
+
if (typeof execSync !== 'function') throw new Error('createZipCommandController 缺少 execSync');
|
|
23
|
+
if (!process) throw new Error('createZipCommandController 缺少 process');
|
|
24
|
+
if (!yauzl) throw new Error('createZipCommandController 缺少 yauzl');
|
|
25
|
+
if (typeof ensureDir !== 'function') throw new Error('createZipCommandController 缺少 ensureDir');
|
|
26
|
+
if (typeof formatTimestampForFileName !== 'function') throw new Error('createZipCommandController 缺少 formatTimestampForFileName');
|
|
27
|
+
if (typeof inspectZipArchiveLimits !== 'function') throw new Error('createZipCommandController 缺少 inspectZipArchiveLimits');
|
|
28
|
+
if (typeof resolveZipTool !== 'function') throw new Error('createZipCommandController 缺少 resolveZipTool');
|
|
29
|
+
if (typeof resolveUnzipTool !== 'function') throw new Error('createZipCommandController 缺少 resolveUnzipTool');
|
|
30
|
+
if (typeof zipWithLibrary !== 'function') throw new Error('createZipCommandController 缺少 zipWithLibrary');
|
|
31
|
+
if (typeof unzipWithLibrary !== 'function') throw new Error('createZipCommandController 缺少 unzipWithLibrary');
|
|
32
|
+
if (!Array.isArray(DEFAULT_EXTRACT_SUFFIXES)) throw new Error('createZipCommandController 缺少 DEFAULT_EXTRACT_SUFFIXES');
|
|
33
|
+
if (!Number.isFinite(MAX_SKILLS_ZIP_ENTRY_COUNT)) throw new Error('createZipCommandController 缺少 MAX_SKILLS_ZIP_ENTRY_COUNT');
|
|
34
|
+
if (!Number.isFinite(MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES)) throw new Error('createZipCommandController 缺少 MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES');
|
|
35
|
+
|
|
36
|
+
async function cmdZip(targetPath, options = {}) {
|
|
37
|
+
if (!targetPath) {
|
|
38
|
+
console.error('用法: codexmate zip <文件或文件夹路径> [--max:压缩级别]');
|
|
39
|
+
console.log('\n示例:');
|
|
40
|
+
console.log(' codexmate zip ./myproject');
|
|
41
|
+
console.log(' codexmate zip ./myproject --max:9');
|
|
42
|
+
console.log(' codexmate zip D:/data/folder --max:1');
|
|
43
|
+
console.log('\n压缩级别: 0(仅存储) ~ 9(极限压缩), 默认: 5');
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const absPath = path.resolve(targetPath);
|
|
48
|
+
if (!fs.existsSync(absPath)) {
|
|
49
|
+
console.error('错误: 路径不存在:', absPath);
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const compressionLevel = options.max !== undefined ? options.max : 5;
|
|
54
|
+
if (compressionLevel < 0 || compressionLevel > 9) {
|
|
55
|
+
console.error('错误: 压缩级别必须在 0-9 之间');
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const baseName = path.basename(absPath);
|
|
60
|
+
const outputDir = path.dirname(absPath);
|
|
61
|
+
const outputPath = path.join(outputDir, `${baseName}.zip`);
|
|
62
|
+
|
|
63
|
+
const zipTool = resolveZipTool();
|
|
64
|
+
const useZipCmd = zipTool.type === 'zip';
|
|
65
|
+
|
|
66
|
+
console.log('\n压缩配置:');
|
|
67
|
+
console.log(' 源路径:', absPath);
|
|
68
|
+
console.log(' 输出文件:', outputPath);
|
|
69
|
+
console.log(' 压缩工具:', useZipCmd ? '系统 zip' : 'zip-lib');
|
|
70
|
+
if (useZipCmd) {
|
|
71
|
+
console.log(' 压缩级别:', compressionLevel);
|
|
72
|
+
} else {
|
|
73
|
+
console.log(' 压缩级别: 固定(zip-lib 不支持 --max,已忽略)');
|
|
74
|
+
}
|
|
75
|
+
console.log('\n开始压缩...\n');
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
if (useZipCmd) {
|
|
79
|
+
const cmd = `"${zipTool.cmd}" -${compressionLevel} -q -r "${outputPath}" "${absPath}"`;
|
|
80
|
+
execSync(cmd, { stdio: 'ignore' });
|
|
81
|
+
} else {
|
|
82
|
+
await zipWithLibrary(absPath, outputPath);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
console.log('✓ 压缩完成!');
|
|
86
|
+
console.log(' 输出文件:', outputPath);
|
|
87
|
+
console.log();
|
|
88
|
+
} catch (e) {
|
|
89
|
+
console.error('压缩失败:', e.message);
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function cmdUnzip(zipPath, outputDir) {
|
|
95
|
+
if (!zipPath) {
|
|
96
|
+
console.error('用法: codexmate unzip <zip文件路径> [输出目录]');
|
|
97
|
+
console.log('\n示例:');
|
|
98
|
+
console.log(' codexmate unzip ./archive.zip');
|
|
99
|
+
console.log(' codexmate unzip ./archive.zip ./output');
|
|
100
|
+
console.log(' codexmate unzip D:/data/file.zip D:/extracted');
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const absZipPath = path.resolve(zipPath);
|
|
105
|
+
if (!fs.existsSync(absZipPath)) {
|
|
106
|
+
console.error('错误: 文件不存在:', absZipPath);
|
|
107
|
+
process.exit(1);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!absZipPath.toLowerCase().endsWith('.zip')) {
|
|
111
|
+
console.error('错误: 仅支持 .zip 文件');
|
|
112
|
+
process.exit(1);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const baseName = path.basename(absZipPath, '.zip');
|
|
116
|
+
const defaultOutputDir = path.join(path.dirname(absZipPath), baseName);
|
|
117
|
+
const absOutputDir = outputDir ? path.resolve(outputDir) : defaultOutputDir;
|
|
118
|
+
|
|
119
|
+
resolveUnzipTool();
|
|
120
|
+
|
|
121
|
+
console.log('\n解压配置:');
|
|
122
|
+
console.log(' 源文件:', absZipPath);
|
|
123
|
+
console.log(' 输出目录:', absOutputDir);
|
|
124
|
+
console.log(' 解压工具:', 'zip-lib');
|
|
125
|
+
console.log('\n开始解压...\n');
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
await unzipWithLibrary(absZipPath, absOutputDir);
|
|
129
|
+
console.log('✓ 解压完成!');
|
|
130
|
+
console.log(' 输出目录:', absOutputDir);
|
|
131
|
+
console.log();
|
|
132
|
+
} catch (e) {
|
|
133
|
+
console.error('解压失败:', e.message);
|
|
134
|
+
process.exit(1);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function splitExtractSuffixInput(rawValue) {
|
|
139
|
+
if (Array.isArray(rawValue)) {
|
|
140
|
+
return rawValue.flatMap((item) => splitExtractSuffixInput(item));
|
|
141
|
+
}
|
|
142
|
+
if (typeof rawValue !== 'string') {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
return rawValue
|
|
146
|
+
.split(/[,\s]+/g)
|
|
147
|
+
.map((item) => item.trim())
|
|
148
|
+
.filter(Boolean);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function normalizeExtractSuffix(rawSuffix, fallbackSuffixes = DEFAULT_EXTRACT_SUFFIXES) {
|
|
152
|
+
const fallbackItems = splitExtractSuffixInput(fallbackSuffixes);
|
|
153
|
+
const sourceItems = splitExtractSuffixInput(rawSuffix);
|
|
154
|
+
const source = sourceItems.length > 0 ? sourceItems : fallbackItems;
|
|
155
|
+
const dedup = new Set();
|
|
156
|
+
|
|
157
|
+
for (const item of source) {
|
|
158
|
+
const lower = item.toLowerCase();
|
|
159
|
+
if (!lower) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const normalized = lower.startsWith('.') ? lower : `.${lower}`;
|
|
163
|
+
if (normalized.length > 1) {
|
|
164
|
+
dedup.add(normalized);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (dedup.size === 0) {
|
|
169
|
+
return [...DEFAULT_EXTRACT_SUFFIXES];
|
|
170
|
+
}
|
|
171
|
+
return Array.from(dedup);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function buildDefaultExtractOutputDir(baseCwd = process.cwd()) {
|
|
175
|
+
const normalizedCwd = path.resolve(baseCwd);
|
|
176
|
+
const parentDir = path.dirname(normalizedCwd);
|
|
177
|
+
const timestamp = formatTimestampForFileName().replace(/-/g, '');
|
|
178
|
+
return path.join(parentDir, timestamp);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function sanitizeNameSegment(rawValue, fallback = 'item') {
|
|
182
|
+
const value = typeof rawValue === 'string' ? rawValue.trim() : '';
|
|
183
|
+
const sanitized = value
|
|
184
|
+
.replace(/[^\w.-]+/g, '_')
|
|
185
|
+
.replace(/^_+|_+$/g, '');
|
|
186
|
+
return sanitized || fallback;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function resolveDuplicateOutputPath(outputDir, originalFileName, zipPath = '', counters = new Map()) {
|
|
190
|
+
const fallbackName = `file${path.extname(originalFileName || '')}`;
|
|
191
|
+
const fileName = path.basename(originalFileName || '') || fallbackName;
|
|
192
|
+
const firstChoice = path.join(outputDir, fileName);
|
|
193
|
+
const firstChoiceKey = `exact:${fileName}`;
|
|
194
|
+
if (!counters.has(firstChoiceKey)) {
|
|
195
|
+
counters.set(firstChoiceKey, true);
|
|
196
|
+
if (!fs.existsSync(firstChoice)) {
|
|
197
|
+
return firstChoice;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const ext = path.extname(fileName);
|
|
202
|
+
const baseName = path.basename(fileName, ext);
|
|
203
|
+
const safeBaseName = sanitizeNameSegment(baseName, 'file');
|
|
204
|
+
const zipBaseName = sanitizeNameSegment(path.basename(zipPath || '', '.zip'), 'zip');
|
|
205
|
+
const duplicateKey = `dup:${safeBaseName}|${zipBaseName}|${ext}`;
|
|
206
|
+
let index = counters.has(duplicateKey) ? counters.get(duplicateKey) : 1;
|
|
207
|
+
|
|
208
|
+
for (; index <= 100000; index++) {
|
|
209
|
+
const candidateName = `${safeBaseName}__${zipBaseName}__${index}${ext}`;
|
|
210
|
+
const candidatePath = path.join(outputDir, candidateName);
|
|
211
|
+
if (!fs.existsSync(candidatePath)) {
|
|
212
|
+
counters.set(duplicateKey, index + 1);
|
|
213
|
+
return candidatePath;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
throw new Error(`重名文件过多,无法生成唯一文件名: ${fileName}`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function collectZipFilesFromDir(rootDir, recursive = true) {
|
|
221
|
+
const queue = [rootDir];
|
|
222
|
+
const result = [];
|
|
223
|
+
|
|
224
|
+
while (queue.length > 0) {
|
|
225
|
+
const currentDir = queue.shift();
|
|
226
|
+
let entries = [];
|
|
227
|
+
try {
|
|
228
|
+
entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
229
|
+
} catch (e) {
|
|
230
|
+
throw new Error(`读取目录失败: ${currentDir} (${e.message})`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
for (const entry of entries) {
|
|
234
|
+
const entryPath = path.join(currentDir, entry.name);
|
|
235
|
+
if (entry.isDirectory()) {
|
|
236
|
+
if (recursive) {
|
|
237
|
+
queue.push(entryPath);
|
|
238
|
+
}
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (entry.isFile() && entry.name.toLowerCase().endsWith('.zip')) {
|
|
242
|
+
result.push(entryPath);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
result.sort((a, b) => a.localeCompare(b));
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function extractMatchedEntriesFromZip(zipPath, outputDir, suffixes, duplicateCounters = new Map()) {
|
|
252
|
+
const normalizedSuffixes = normalizeExtractSuffix(suffixes);
|
|
253
|
+
return new Promise((resolve, reject) => {
|
|
254
|
+
yauzl.open(zipPath, { lazyEntries: true, autoClose: false }, (openErr, zipFile) => {
|
|
255
|
+
if (openErr) {
|
|
256
|
+
reject(openErr);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (!zipFile) {
|
|
260
|
+
reject(new Error('无法读取 ZIP 文件'));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
let settled = false;
|
|
265
|
+
let matched = 0;
|
|
266
|
+
let extracted = 0;
|
|
267
|
+
let skippedDir = 0;
|
|
268
|
+
let skippedExt = 0;
|
|
269
|
+
|
|
270
|
+
const finish = (err) => {
|
|
271
|
+
if (settled) return;
|
|
272
|
+
settled = true;
|
|
273
|
+
try {
|
|
274
|
+
zipFile.close();
|
|
275
|
+
} catch (_) {}
|
|
276
|
+
if (err) {
|
|
277
|
+
reject(err);
|
|
278
|
+
} else {
|
|
279
|
+
resolve({ matched, extracted, skippedDir, skippedExt });
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
zipFile.on('entry', (entry) => {
|
|
284
|
+
if (settled) return;
|
|
285
|
+
const rawEntryName = typeof entry.fileName === 'string' ? entry.fileName : '';
|
|
286
|
+
const normalizedEntryName = rawEntryName.replace(/\\/g, '/');
|
|
287
|
+
|
|
288
|
+
if (!normalizedEntryName || normalizedEntryName.endsWith('/')) {
|
|
289
|
+
skippedDir += 1;
|
|
290
|
+
zipFile.readEntry();
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const entryBaseName = path.basename(normalizedEntryName);
|
|
295
|
+
const lowerBaseName = entryBaseName.toLowerCase();
|
|
296
|
+
const matchedSuffix = normalizedSuffixes.some((suffix) => lowerBaseName.endsWith(suffix));
|
|
297
|
+
if (!entryBaseName || !matchedSuffix) {
|
|
298
|
+
skippedExt += 1;
|
|
299
|
+
zipFile.readEntry();
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
matched += 1;
|
|
304
|
+
zipFile.openReadStream(entry, (streamErr, readStream) => {
|
|
305
|
+
if (streamErr || !readStream) {
|
|
306
|
+
finish(streamErr || new Error('无法读取 ZIP 条目流'));
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
let completed = false;
|
|
311
|
+
const outputPath = resolveDuplicateOutputPath(outputDir, entryBaseName, zipPath, duplicateCounters);
|
|
312
|
+
const writeStream = fs.createWriteStream(outputPath);
|
|
313
|
+
const fail = (writeErr) => {
|
|
314
|
+
if (completed) return;
|
|
315
|
+
completed = true;
|
|
316
|
+
try {
|
|
317
|
+
readStream.destroy();
|
|
318
|
+
} catch (_) {}
|
|
319
|
+
try {
|
|
320
|
+
writeStream.destroy();
|
|
321
|
+
} catch (_) {}
|
|
322
|
+
try {
|
|
323
|
+
if (fs.existsSync(outputPath)) {
|
|
324
|
+
fs.unlinkSync(outputPath);
|
|
325
|
+
}
|
|
326
|
+
} catch (_) {}
|
|
327
|
+
finish(writeErr);
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
readStream.on('error', fail);
|
|
331
|
+
writeStream.on('error', fail);
|
|
332
|
+
writeStream.on('finish', () => {
|
|
333
|
+
if (completed || settled) return;
|
|
334
|
+
completed = true;
|
|
335
|
+
extracted += 1;
|
|
336
|
+
zipFile.readEntry();
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
readStream.pipe(writeStream);
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
zipFile.on('end', () => {
|
|
344
|
+
finish(null);
|
|
345
|
+
});
|
|
346
|
+
zipFile.on('error', (zipErr) => {
|
|
347
|
+
finish(zipErr);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
zipFile.readEntry();
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
async function cmdUnzipExt(zipDirPath, outputDir, options = {}) {
|
|
356
|
+
if (!zipDirPath) {
|
|
357
|
+
console.error('用法: codexmate unzip-ext <zip目录> [输出目录] [--ext:后缀[,后缀...]] [--no-recursive]');
|
|
358
|
+
console.log('\n示例:');
|
|
359
|
+
console.log(' codexmate unzip-ext ./archives');
|
|
360
|
+
console.log(' codexmate unzip-ext ./archives ./output --ext:json,txt');
|
|
361
|
+
console.log(' codexmate unzip-ext D:/data/zips --ext:txt --no-recursive');
|
|
362
|
+
console.log(' 说明: 默认递归扫描子目录,可通过 --no-recursive 关闭递归');
|
|
363
|
+
process.exit(1);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const recursive = options.recursive !== false;
|
|
367
|
+
const suffixes = normalizeExtractSuffix(options.ext);
|
|
368
|
+
const absZipDir = path.resolve(zipDirPath);
|
|
369
|
+
const absOutputDir = outputDir ? path.resolve(outputDir) : buildDefaultExtractOutputDir(process.cwd());
|
|
370
|
+
|
|
371
|
+
if (!fs.existsSync(absZipDir)) {
|
|
372
|
+
console.error('错误: 目录不存在:', absZipDir);
|
|
373
|
+
process.exit(1);
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
if (!fs.statSync(absZipDir).isDirectory()) {
|
|
377
|
+
console.error('错误: 仅支持目录路径:', absZipDir);
|
|
378
|
+
process.exit(1);
|
|
379
|
+
}
|
|
380
|
+
} catch (e) {
|
|
381
|
+
console.error('错误: 无法读取目录信息:', e.message);
|
|
382
|
+
process.exit(1);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
let zipFiles = [];
|
|
386
|
+
try {
|
|
387
|
+
zipFiles = collectZipFilesFromDir(absZipDir, recursive);
|
|
388
|
+
} catch (e) {
|
|
389
|
+
console.error('扫描 ZIP 文件失败:', e.message);
|
|
390
|
+
process.exit(1);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (zipFiles.length === 0) {
|
|
394
|
+
console.error('错误: 未找到任何 ZIP 文件');
|
|
395
|
+
process.exit(1);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
ensureDir(absOutputDir);
|
|
399
|
+
|
|
400
|
+
console.log('\n批量解压配置:');
|
|
401
|
+
console.log(' ZIP 目录:', absZipDir);
|
|
402
|
+
console.log(' 输出目录:', absOutputDir);
|
|
403
|
+
console.log(' 后缀过滤:', suffixes.join(', '));
|
|
404
|
+
console.log(' 递归扫描:', recursive ? '是' : '否');
|
|
405
|
+
console.log(' ZIP 数量:', zipFiles.length);
|
|
406
|
+
console.log('\n开始提取...\n');
|
|
407
|
+
|
|
408
|
+
let totalMatched = 0;
|
|
409
|
+
let totalExtracted = 0;
|
|
410
|
+
let totalSkippedDir = 0;
|
|
411
|
+
let totalSkippedExt = 0;
|
|
412
|
+
const failed = [];
|
|
413
|
+
const duplicateCounters = new Map();
|
|
414
|
+
|
|
415
|
+
for (const zipFilePath of zipFiles) {
|
|
416
|
+
try {
|
|
417
|
+
await inspectZipArchiveLimits(zipFilePath, {
|
|
418
|
+
maxEntryCount: MAX_SKILLS_ZIP_ENTRY_COUNT,
|
|
419
|
+
maxUncompressedBytes: MAX_SKILLS_ZIP_UNCOMPRESSED_BYTES
|
|
420
|
+
});
|
|
421
|
+
const result = await extractMatchedEntriesFromZip(zipFilePath, absOutputDir, suffixes, duplicateCounters);
|
|
422
|
+
totalMatched += result.matched;
|
|
423
|
+
totalExtracted += result.extracted;
|
|
424
|
+
totalSkippedDir += result.skippedDir;
|
|
425
|
+
totalSkippedExt += result.skippedExt;
|
|
426
|
+
console.log(`✓ ${path.basename(zipFilePath)}: 命中 ${result.matched},提取 ${result.extracted}`);
|
|
427
|
+
} catch (e) {
|
|
428
|
+
failed.push({ zipFilePath, message: e && e.message ? e.message : String(e) });
|
|
429
|
+
console.error(`✗ ${path.basename(zipFilePath)}: ${e && e.message ? e.message : e}`);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
console.log('\n提取结果:');
|
|
434
|
+
console.log(' 输出目录:', absOutputDir);
|
|
435
|
+
console.log(' 扫描 ZIP:', zipFiles.length);
|
|
436
|
+
console.log(' 命中条目:', totalMatched);
|
|
437
|
+
console.log(' 已提取:', totalExtracted);
|
|
438
|
+
console.log(' 已跳过(目录条目):', totalSkippedDir);
|
|
439
|
+
console.log(' 已跳过(后缀不匹配):', totalSkippedExt);
|
|
440
|
+
if (failed.length > 0) {
|
|
441
|
+
console.error(' 失败数量:', failed.length);
|
|
442
|
+
for (const item of failed) {
|
|
443
|
+
console.error(` - ${item.zipFilePath}: ${item.message}`);
|
|
444
|
+
}
|
|
445
|
+
process.exit(1);
|
|
446
|
+
}
|
|
447
|
+
console.log();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function parseZipCommandArgs(args = []) {
|
|
451
|
+
const options = {};
|
|
452
|
+
let targetPath = null;
|
|
453
|
+
for (let i = 0; i < args.length; i++) {
|
|
454
|
+
const arg = args[i];
|
|
455
|
+
if (typeof arg !== 'string' || !arg) continue;
|
|
456
|
+
if (arg.startsWith('--max:')) {
|
|
457
|
+
options.max = parseInt(arg.substring(6), 10);
|
|
458
|
+
} else if (!targetPath) {
|
|
459
|
+
targetPath = arg;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return { targetPath, options };
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function parseUnzipExtCommandArgs(args = []) {
|
|
466
|
+
const options = {
|
|
467
|
+
ext: [],
|
|
468
|
+
recursive: true
|
|
469
|
+
};
|
|
470
|
+
let zipDirPath = null;
|
|
471
|
+
let outputDir = null;
|
|
472
|
+
for (let i = 0; i < args.length; i++) {
|
|
473
|
+
const arg = args[i];
|
|
474
|
+
if (typeof arg !== 'string' || !arg) continue;
|
|
475
|
+
if (arg.startsWith('--ext:')) {
|
|
476
|
+
options.ext.push(...splitExtractSuffixInput(arg.substring(6)));
|
|
477
|
+
} else if (arg.startsWith('--ext=')) {
|
|
478
|
+
options.ext.push(...splitExtractSuffixInput(arg.substring(6)));
|
|
479
|
+
} else if (arg === '--ext') {
|
|
480
|
+
const nextArg = args[i + 1];
|
|
481
|
+
if (typeof nextArg === 'string' && !nextArg.startsWith('--')) {
|
|
482
|
+
options.ext.push(...splitExtractSuffixInput(nextArg));
|
|
483
|
+
i += 1;
|
|
484
|
+
}
|
|
485
|
+
} else if (arg === '--recursive') {
|
|
486
|
+
options.recursive = true;
|
|
487
|
+
} else if (arg === '--no-recursive') {
|
|
488
|
+
options.recursive = false;
|
|
489
|
+
} else if (!zipDirPath) {
|
|
490
|
+
zipDirPath = arg;
|
|
491
|
+
} else if (!outputDir) {
|
|
492
|
+
outputDir = arg;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return { zipDirPath, outputDir, options };
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return {
|
|
499
|
+
cmdZip,
|
|
500
|
+
cmdUnzip,
|
|
501
|
+
cmdUnzipExt,
|
|
502
|
+
splitExtractSuffixInput,
|
|
503
|
+
parseZipCommandArgs,
|
|
504
|
+
parseUnzipExtCommandArgs
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
module.exports = {
|
|
509
|
+
createZipCommandController
|
|
510
|
+
};
|