重构成功
This commit is contained in:
@ -1,61 +1,30 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import color from "picocolors";
|
||||
import { convert } from "./converters.js";
|
||||
import { runInteractive, showSummary } from "./ui.js";
|
||||
import { createStepUI } from "../../utils/stepui.js";
|
||||
import { title, getSteps } from "./config.js";
|
||||
import { stopKeypress, waitForKey } from "../../keyboard.js";
|
||||
import { record } from "../../stats.js";
|
||||
import { processFile } from "./service.js";
|
||||
|
||||
const FORMAT_EXT = {
|
||||
collada: ".dae",stp: ".stp", obj: ".obj", objnomtl: ".obj",
|
||||
stl: ".stl", stlb: ".stl", ply: ".ply", plyb: ".ply", "3ds": ".3ds",
|
||||
gltf2: ".gltf", glb2: ".glb",
|
||||
assbin: ".assbin", assxml: ".assxml", x3d: ".x3d",
|
||||
fbx: ".fbx", fbxa: ".fbx", "3mf": ".3mf", pbrt: ".pbrt", assjson: ".json"
|
||||
};
|
||||
|
||||
function getOutputExt(format) {
|
||||
return FORMAT_EXT[format] || "." + format;
|
||||
}
|
||||
|
||||
async function processFile(file, config) {
|
||||
const cwd = process.cwd();
|
||||
const { dir, name } = path.parse(file);
|
||||
const outputExt = getOutputExt(config.outputFormat);
|
||||
const outputFile = path.join(dir || "", `${name}${outputExt}`);
|
||||
const absolutePath = path.join(cwd, file);
|
||||
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
return { ok: false, file, reason: "文件不存在" };
|
||||
}
|
||||
|
||||
await convert(file, outputFile, config.outputFormat, cwd);
|
||||
return { ok: true, file, output: outputFile };
|
||||
}
|
||||
|
||||
async function waitForEsc() {
|
||||
return waitForKey(color.dim("按 Esc 返回"), key => key?.name === "escape" || (key?.ctrl && key?.name === "c"));
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const result = await runInteractive();
|
||||
const run = async () => {
|
||||
const ui = createStepUI({ title, getSteps });
|
||||
const result = await ui.runInteractive();
|
||||
if (!result) return "back";
|
||||
|
||||
const { results } = result;
|
||||
const config = {
|
||||
files: results[0] || [],
|
||||
outputFormat: results[1] || "glb2"
|
||||
files: result.results[0] || [],
|
||||
outputFormat: result.results[1] || "glb2"
|
||||
};
|
||||
|
||||
stopKeypress();
|
||||
showSummary([
|
||||
ui.showSummary([
|
||||
"源文件: " + (config.files.length ? config.files.join(", ") : "未选择"),
|
||||
"目标格式: " + config.outputFormat.toUpperCase()
|
||||
]);
|
||||
|
||||
if (!config.files.length) {
|
||||
console.log(color.yellow("未选择任何模型文件"));
|
||||
await waitForEsc();
|
||||
await waitForKey(color.dim("按 Esc 返回"), key => key?.name === "escape" || (key?.ctrl && key?.name === "c"));
|
||||
return "back";
|
||||
}
|
||||
|
||||
@ -88,16 +57,12 @@ async function run() {
|
||||
}
|
||||
|
||||
console.log("\n" + color.bgGreen(color.black(" 转换完成 ")));
|
||||
if (success) {
|
||||
console.log(color.green(`成功: ${success} 个`));
|
||||
}
|
||||
if (failed.length) {
|
||||
console.log(color.yellow(`失败: ${failed.length} 个`));
|
||||
}
|
||||
if (success) console.log(color.green(`成功: ${success} 个`));
|
||||
if (failed.length) console.log(color.yellow(`失败: ${failed.length} 个`));
|
||||
|
||||
await waitForEsc();
|
||||
await waitForKey(color.dim("按 Esc 返回"), key => key?.name === "escape" || (key?.ctrl && key?.name === "c"));
|
||||
return "back";
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
id: "convert",
|
||||
|
||||
Reference in New Issue
Block a user