Files
youle_cocos/cocoscreator_projects/scripts/legacy-layer-to-cocos-prefab-cli.mjs
T

55 lines
2.2 KiB
JavaScript

#!/usr/bin/env node
// CLI 入口: Layer XML → Cocos prefab JSON (+ 对比 diff, 如提供 YouleNexus 现存 prefab).
//
// 用法: node legacy-layer-to-cocos-prefab-cli.mjs <input.xml> [<youle-prefab>] <output-dir> [--target-name <name>]
//
// 设计要点:
// - 只输出到隔离目录 (out/legacy-migration/), 不直接覆盖 YouleNexus 原 prefab.
// - targetName 只影响输出的 prefab/root 名称与 atlas 选择; 默认使用 XML layerName.
import { readLayerXml, emitPrefabJson, selectAtlasForLayerName } from './legacy-layer-to-cocos-prefab.mjs';
import { writeFileSync, mkdirSync, existsSync } from 'node:fs';
import { execSync } from 'node:child_process';
import { join } from 'node:path';
const args = process.argv.slice(2);
let inputXml = null;
let youlePrefab = null;
let outputDir = null;
let targetName = null;
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (arg === '--target-name') {
targetName = args[++i];
if (!targetName) throw new Error('--target-name requires a value');
continue;
}
if (!inputXml) inputXml = arg;
else if (!youlePrefab) youlePrefab = arg;
else if (!outputDir) outputDir = arg;
else throw new Error(`Unexpected argument: ${arg}`);
}
if (!inputXml || !outputDir) {
console.error('Usage: node legacy-layer-to-cocos-prefab-cli.mjs <input.xml> <youle-prefab-path?> <output-dir> [--target-name <name>]');
process.exit(1);
}
const layer = readLayerXml(inputXml);
const outputName = targetName || layer.layerName;
const prefabJson = emitPrefabJson(layer, {
targetName: outputName,
atlasName: selectAtlasForLayerName(outputName),
});
if (!existsSync(outputDir)) mkdirSync(outputDir, { recursive: true });
const outputPath = join(outputDir, `${outputName}.prefab`);
writeFileSync(outputPath, JSON.stringify(prefabJson, null, 2));
console.log(`Generated: ${outputPath}`);
if (youlePrefab && existsSync(youlePrefab)) {
const diffPath = `${outputPath}.diff`;
// `|| true` 确保 diff 找到差异时返回非零退出码不致 CLI 崩溃.
const diff = execSync(`diff -u "${youlePrefab}" "${outputPath}" || true`, { encoding: 'utf8' });
writeFileSync(diffPath, diff);
console.log(`Diff: ${diffPath} (${diff.split('\n').length} lines)`);
}