第一版
This commit is contained in:
58
cli.js
58
cli.js
@ -1,35 +1,35 @@
|
||||
#!/usr/bin/env node
|
||||
import color from "picocolors";
|
||||
import { checkToktx, scanImages, compressAll } from "./lib/compressor.js";
|
||||
import { runInteractive, showSummary } from "./lib/ui.js";
|
||||
import { showMainMenu } from "./lib/menu.js";
|
||||
|
||||
// 检查 toktx
|
||||
checkToktx();
|
||||
// 主循环
|
||||
while (true) {
|
||||
const selected = await showMainMenu();
|
||||
|
||||
// 运行交互界面
|
||||
const [exts, quality, encoding, mipmap, outputOpts] = await runInteractive();
|
||||
console.clear();
|
||||
console.log(color.cyan(`\n正在启动: ${selected.name}...\n`));
|
||||
|
||||
const config = { exts, quality, encoding, mipmap, outputOpts };
|
||||
|
||||
// 显示配置摘要
|
||||
showSummary(config);
|
||||
|
||||
// 扫描文件
|
||||
const { images, cwd } = scanImages(exts);
|
||||
|
||||
if (images.length === 0) {
|
||||
console.log(color.yellow("当前目录没有匹配的图片"));
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
console.log(`📁 找到 ${color.cyan(images.length)} 个待转换文件\n`);
|
||||
|
||||
// 执行压缩
|
||||
const { total, failed } = await compressAll(images, config, cwd);
|
||||
|
||||
// 显示结果
|
||||
if (failed > 0) {
|
||||
console.log(color.yellow(`\n⚠️ 完成,但有 ${failed} 个文件失败`));
|
||||
} else {
|
||||
console.log(color.green("\n🎉 全部文件压缩完成!"));
|
||||
try {
|
||||
const tool = await import(selected.module);
|
||||
const result = await tool.run();
|
||||
|
||||
// 返回主菜单
|
||||
if (result === "back") continue;
|
||||
|
||||
// 工具完成后退出
|
||||
break;
|
||||
} catch (err) {
|
||||
console.log(color.yellow(`\n⚠️ ${selected.name} 模块尚未实现`));
|
||||
console.log(color.dim(err.message));
|
||||
console.log(color.dim("\n按任意键返回主菜单..."));
|
||||
|
||||
// 等待按键
|
||||
await new Promise(resolve => {
|
||||
process.stdin.setRawMode(true);
|
||||
process.stdin.once("data", () => {
|
||||
process.stdin.setRawMode(false);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user