109 lines
5.9 KiB
JavaScript
109 lines
5.9 KiB
JavaScript
// 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 directories = entries.filter(e => e.isDirectory());
|
||
const flat = !!reference && directories.length === 0;
|
||
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 = flat ? [{ path: '', number: 1 }] : ordered(directories.map(entry => {
|
||
const match = /^([##]?)\s*(\d+)/u.exec(entry.name);
|
||
return { path: entry.name, number: Number(match?.[2]), revealAll: !!match?.[1] };
|
||
}), '部位目录', issues);
|
||
if (!groups.length) issues.push('没有找到编号部位目录');
|
||
const parts = [];
|
||
const readSequence = (g, directory, files, subgroup) => {
|
||
const relativePath = name => directory ? `${directory}/${name}` : name;
|
||
for (const e of files.filter(e => e.isDirectory() || e.isSymbolicLink())) issues.push(`不支持嵌套目录或链接:${relativePath(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: relativePath(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 pair = /^([##!!]?)\s*(\d+)([a-zA-Z]?)(\+|[-_.、 ]+)(\d+)(?:[-_.、 ]+(.+)|([^\d\s._、-].*))?$/u.exec(stem);
|
||
const single = pair ? null : /^(\d+)[-_.、 ]+(.+)$/u.exec(stem);
|
||
const isolated = pair?.[4] === '+';
|
||
const revealAll = /[##]/u.test(pair?.[1] ?? '');
|
||
const combine = /[!!]/u.test(pair?.[1] ?? '');
|
||
if ((revealAll && isolated) || (combine && (subgroup !== 'finish' || Number(pair[2]) !== g.number))) {
|
||
issues.push(`步骤标记与所属目录不一致:${relativePath(e.name)}`);
|
||
}
|
||
return [{ group: g, number: Number(pair?.[5] ?? single?.[1]), stem,
|
||
name: pair ? pair[6] ?? pair[7] ?? stem : single?.[2], path: relativePath(e.name), isolated, revealAll,
|
||
stepLabel: pair && (isolated || pair[1]) ? `${pair[1]}${pair[2]}${pair[3]}${pair[4]}${pair[5]}` : undefined }];
|
||
});
|
||
if (!items.length) issues.push(`${directory || '零件目录'} 没有 GLB 零件`);
|
||
const sequence = ordered(items, directory || '零件目录', 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.map(item => ({ ...item, directory, subgroup })));
|
||
};
|
||
for (const group of groups) {
|
||
const files = flat ? entries : children(path.join(root, group.path));
|
||
const subdirs = files.filter(entry => entry.isDirectory());
|
||
if (!subdirs.length) {
|
||
readSequence(group, group.path, files);
|
||
continue;
|
||
}
|
||
if (files.some(entry => isGlb(entry) || entry.isSymbolicLink())) issues.push(`${group.path} 的分件请统一放入小组件文件夹`);
|
||
const subgroups = new Map();
|
||
for (const entry of subdirs) {
|
||
const combine = /^[!!]\s*(\d+)(?!\d)/u.exec(entry.name);
|
||
const ordinary = /^(\d+)([a-zA-Z])(?:$|[^a-zA-Z])/u.exec(entry.name);
|
||
const key = combine ? 'finish' : ordinary?.[2].toUpperCase();
|
||
if (!key || Number((combine ?? ordinary)[1]) !== group.number || subgroups.has(key)) {
|
||
issues.push(`小组件目录编号无效或重复:${group.path}/${entry.name}`);
|
||
continue;
|
||
}
|
||
subgroups.set(key, entry.name);
|
||
}
|
||
const letters = [...subgroups.keys()].filter(key => key !== 'finish').sort();
|
||
if (!letters.length || letters.some((key, index) => key.charCodeAt(0) !== 65 + index)) issues.push(`${group.path} 的小组件须从 A 起连续编号`);
|
||
for (const key of [...letters, ...(subgroups.has('finish') ? ['finish'] : [])]) {
|
||
const directory = `${group.path}/${subgroups.get(key)}`;
|
||
readSequence(group, directory, children(path.join(root, directory)), key);
|
||
}
|
||
}
|
||
if (issues.length) throw new Error('输入检查失败,未生成装配 JSON:\n' + issues.join('\n'));
|
||
return { whole: reference ? path.resolve(reference) : models[0].name, parts };
|
||
}
|