35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
import { relative, sep } from 'node:path'
|
|
|
|
function relativeWorkspacePath(workspaceRoot, targetPath) {
|
|
const path = relative(workspaceRoot, targetPath)
|
|
if (!path || path === '..' || path.startsWith(`..${sep}`)) {
|
|
throw new Error('Spine 3.8 工程文件必须位于连接器工作目录内')
|
|
}
|
|
return path
|
|
}
|
|
|
|
export function normalizeSpine38ImagesPath(imagesDirectory) {
|
|
const path = String(imagesDirectory || 'images')
|
|
.replace(/\\/g, '/')
|
|
.replace(/^\.\/+/, '')
|
|
.replace(/\/+$/, '')
|
|
return path || 'images'
|
|
}
|
|
|
|
export function prepareSpine38SkeletonJson(skeleton, imagesDirectory) {
|
|
const prepared = JSON.parse(JSON.stringify(skeleton))
|
|
prepared.skeleton ||= {}
|
|
// Spine 3.8 的命令行导入器对 "./images/" 的处理并不稳定,某些工程会
|
|
// 将其固化为导入时的临时绝对路径。使用无前缀、无尾斜杠的工程内路径。
|
|
prepared.skeleton.images = normalizeSpine38ImagesPath(imagesDirectory)
|
|
return `${JSON.stringify(prepared, null, 2)}\n`
|
|
}
|
|
|
|
export function createSpine38ImportOptions({ workspaceRoot, inputPath, outputPath }) {
|
|
return {
|
|
cwd: workspaceRoot,
|
|
inputPath: relativeWorkspacePath(workspaceRoot, inputPath),
|
|
outputPath: relativeWorkspacePath(workspaceRoot, outputPath),
|
|
}
|
|
}
|