重构成功
This commit is contained in:
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 };
|
||||
};
|
||||
Reference in New Issue
Block a user