Files
vibemake/scripts/lib/numbered-parts.mjs
T

72 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Numeric order is authoritative. A CAD instance suffix (.003) is never a step.
import { readdirSync, statSync } from 'node:fs';
import path from 'node:path';
function ordered(items, parent, issues) {
const byNumber = new Map();
for (const item of items) {
if (!Number.isSafeInteger(item.number) || item.number < 1) {
issues.push(`缺少有效的开头编号:${item.path}`);
continue;
}
const names = byNumber.get(item.number) ?? [];
names.push(item.path);
byNumber.set(item.number, names);
}
const numbers = [...byNumber.keys()].sort((a, b) => a - b);
let expected = 1;
for (const n of numbers) {
if (n !== expected) issues.push(`${parent} 缺号:${expected}${expected === n - 1 ? '' : '–' + (n - 1)}`);
if (byNumber.get(n).length > 1) issues.push(`重复编号 ${n}:${byNumber.get(n).join('、')}`);
expected = n + 1;
}
return items.sort((a, b) => a.number - b.number);
}
export function numberedInput(root, { extraBefore, reference } = {}) {
const children = folder => readdirSync(folder, { withFileTypes: true }).filter(e => !e.name.startsWith('.') && e.name !== '__MACOSX');
const isGlb = e => e.isFile() && /\.glb$/i.test(e.name);
const entries = children(root), models = entries.filter(isGlb);
const issues = [];
if (reference) {
if (!/\.glb$/i.test(reference) || !statSync(reference, { throwIfNoEntry: false })?.isFile()) issues.push('指定的整机 GLB 不存在');
} else if (models.length !== 1) issues.push(`根目录应恰好有一个整机 GLB,实际 ${models.length} 个;整机在外部时请用 --reference 指定`);
const groups = ordered(entries.filter(e => e.isDirectory()).map(e => ({
path: e.name, number: Number(/^(\d+)/u.exec(e.name)?.[1]),
})), '部位目录', issues);
if (!groups.length) issues.push('没有找到编号部位目录');
const parts = [];
for (const g of groups) {
const files = children(path.join(root, g.path));
for (const e of files.filter(e => e.isDirectory() || e.isSymbolicLink())) issues.push(`不支持嵌套目录或链接:${g.path}/${e.name}`);
const extras = [];
const items = files.filter(isGlb).flatMap(e => {
const stem = e.name.slice(0,-4);
const extra = /^(\d+)[-_.、 ]+附加[-_.、 ]+(.+)$/u.exec(stem);
if (extra) {
extras.push({ group: g, stem, name: extra[2], path: g.path + '/' + e.name });
return [];
}
// For "1-23-tibia" inside either leg, 23 is the local step. The outer
// directory determines the group; do not sort by the first digit here.
// Some exports omit the separator before the name: "4-10STP39-14".
const match = /^\d+[-_.、 ]+(\d+)(?:[-_.、 ]+(.+)|([^\d\s._、-].*))$/u.exec(stem)
?? /^(\d+)[-_.、 ]+(.+)$/u.exec(stem);
return [{ group: g, number: Number(match?.[1]), stem, name: match?.[2] ?? match?.[3], path: g.path + '/' + e.name }];
});
if (!items.length) issues.push(`${g.path} 没有 GLB 零件`);
const sequence = ordered(items, g.path, issues);
if (extras.length) {
const index = extraBefore === 'end' ? sequence.length : sequence.findIndex(item => item.number === extraBefore);
if (index < 0 || extras.length > 1) {
issues.push(`请指定附加件的安装位置或给它编独立步号:${extras.map(item => item.path).join('、')}`);
} else {
sequence.splice(index, 0, { ...extras[0], number: 'extra', stepLabel: '附加' });
}
}
parts.push(...sequence);
}
if (issues.length) throw new Error('输入检查失败,未生成装配 JSON:\n' + issues.join('\n'));
return { whole: reference ? path.resolve(reference) : models[0].name, parts };
}