重构成功
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import { listGltfFiles } from "../../utils/gltf.js";
|
||||
|
||||
export const title = "glTF 扩展工具";
|
||||
|
||||
const extensionStep = {
|
||||
name: "扩展选项",
|
||||
type: "multiselect",
|
||||
@ -13,7 +15,7 @@ const extensionStep = {
|
||||
default: ["textureBasisu"]
|
||||
};
|
||||
|
||||
export function getSteps() {
|
||||
export const getSteps = () => {
|
||||
const files = listGltfFiles();
|
||||
const fileStep = {
|
||||
name: "文件选择",
|
||||
@ -24,4 +26,4 @@ export function getSteps() {
|
||||
};
|
||||
|
||||
return [fileStep, extensionStep];
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,76 +1,36 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import color from "picocolors";
|
||||
import { checkRequiredFiles, modifyGltfContent, listGltfFiles, BACKUP_SUFFIX } from "../../utils/gltf.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 { runGltfExtension } from "./service.js";
|
||||
|
||||
export function runGltfExtension(config = { files: [], extensions: ["textureBasisu"] }) {
|
||||
const cwd = process.cwd();
|
||||
const { files = [], extensions = ["textureBasisu"] } = config;
|
||||
const selectedExtensions = extensions.length ? extensions : ["textureBasisu"];
|
||||
|
||||
const { ok, missing } = checkRequiredFiles();
|
||||
if (!ok) {
|
||||
console.log(color.red("\n✖ 缺少必要文件: " + missing.join(", ")));
|
||||
console.log(color.dim("请确保当前目录包含 .ktx2、.gltf 和 .bin 文件\n"));
|
||||
return { success: false, count: 0 };
|
||||
}
|
||||
|
||||
const fallbackFiles = listGltfFiles();
|
||||
const gltfFiles = (files.length ? files : fallbackFiles).filter(f => f.toLowerCase().endsWith(".gltf"));
|
||||
if (!gltfFiles.length) {
|
||||
console.log(color.yellow("未选择可处理的 glTF 文件"));
|
||||
return { success: false, count: 0 };
|
||||
}
|
||||
|
||||
let count = 0;
|
||||
|
||||
for (const file of gltfFiles) {
|
||||
const fullPath = path.join(cwd, file);
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
console.log(color.yellow("跳过缺失的文件: " + file));
|
||||
continue;
|
||||
}
|
||||
const baseName = file.replace(/\.gltf$/i, "");
|
||||
const backupName = baseName + BACKUP_SUFFIX + ".gltf";
|
||||
const backupPath = path.join(cwd, backupName);
|
||||
|
||||
fs.copyFileSync(fullPath, backupPath);
|
||||
const modified = modifyGltfContent(fullPath, selectedExtensions);
|
||||
fs.writeFileSync(fullPath, JSON.stringify(modified, null, 2), "utf-8");
|
||||
console.log(color.green("✓ " + file + " (备份: " + backupName + ")"));
|
||||
count++;
|
||||
}
|
||||
|
||||
return { success: count > 0, count };
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const result = await runInteractive();
|
||||
const run = async () => {
|
||||
const ui = createStepUI({ title, getSteps });
|
||||
const result = await ui.runInteractive();
|
||||
if (!result) return "back";
|
||||
|
||||
stopKeypress();
|
||||
|
||||
const { results } = result;
|
||||
const config = {
|
||||
files: results[0] || [],
|
||||
extensions: results[1] || []
|
||||
files: result.results[0] || [],
|
||||
extensions: result.results[1] || []
|
||||
};
|
||||
|
||||
showSummary([
|
||||
ui.showSummary([
|
||||
"处理文件: " + (config.files.length ? config.files.join(", ") : "未选择"),
|
||||
"扩展选项: " + (config.extensions.length ? config.extensions.join(", ") : "未选择")
|
||||
]);
|
||||
|
||||
const { success, count } = runGltfExtension(config);
|
||||
if (success) {
|
||||
const { success, count, error } = runGltfExtension(config);
|
||||
if (error) {
|
||||
console.log(color.red("\n✖ " + error));
|
||||
} else if (success) {
|
||||
console.log(color.green("\n✓ 已修改 " + count + " 个 glTF 文件"));
|
||||
}
|
||||
|
||||
await waitForKey();
|
||||
return "back";
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
id: "gltf",
|
||||
|
||||
39
lib/plugins/gltf/service.js
Normal file
39
lib/plugins/gltf/service.js
Normal file
@ -0,0 +1,39 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { checkRequiredFiles, modifyGltfContent, listGltfFiles, BACKUP_SUFFIX } from "../../utils/gltf.js";
|
||||
|
||||
export const runGltfExtension = (config = { files: [], extensions: ["textureBasisu"] }) => {
|
||||
const cwd = process.cwd();
|
||||
const { files = [], extensions = ["textureBasisu"] } = config;
|
||||
const selectedExtensions = extensions.length ? extensions : ["textureBasisu"];
|
||||
|
||||
const { ok, missing } = checkRequiredFiles();
|
||||
if (!ok) {
|
||||
return { success: false, count: 0, error: "缺少必要文件: " + missing.join(", ") };
|
||||
}
|
||||
|
||||
const fallbackFiles = listGltfFiles();
|
||||
const gltfFiles = (files.length ? files : fallbackFiles).filter(f => f.toLowerCase().endsWith(".gltf"));
|
||||
if (!gltfFiles.length) {
|
||||
return { success: false, count: 0, error: "未选择可处理的 glTF 文件" };
|
||||
}
|
||||
|
||||
let count = 0;
|
||||
|
||||
for (const file of gltfFiles) {
|
||||
const fullPath = path.join(cwd, file);
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
continue;
|
||||
}
|
||||
const baseName = file.replace(/\.gltf$/i, "");
|
||||
const backupName = baseName + BACKUP_SUFFIX + ".gltf";
|
||||
const backupPath = path.join(cwd, backupName);
|
||||
|
||||
fs.copyFileSync(fullPath, backupPath);
|
||||
const modified = modifyGltfContent(fullPath, selectedExtensions);
|
||||
fs.writeFileSync(fullPath, JSON.stringify(modified, null, 2), "utf-8");
|
||||
count++;
|
||||
}
|
||||
|
||||
return { success: count > 0, count };
|
||||
};
|
||||
@ -1,9 +0,0 @@
|
||||
import { createStepUI } from "../../utils/stepui.js";
|
||||
import { getSteps } from "./config.js";
|
||||
|
||||
const ui = createStepUI({
|
||||
title: "glTF 扩展工具",
|
||||
getSteps
|
||||
});
|
||||
|
||||
export const { runInteractive, showSummary } = ui;
|
||||
Reference in New Issue
Block a user