import { executeRPCCommand } from '@open-pencil/core/rpc' import { isAppMode, requireFile, rpc } from '#cli/app-client' import { appTargetRPCArgs, type AppTargetCLIArgs } from '#cli/app-target' import { loadDocument, prepareDocumentForRPC } from '#cli/headless' type RPCArgs = { [key: string]: unknown } function isRPCArgs(value: unknown): value is RPCArgs { return Boolean(value && typeof value === 'object' && !Array.isArray(value)) } export async function loadRPCData( file: string | undefined, command: string, args?: unknown, targetArgs?: AppTargetCLIArgs ): Promise { if (isAppMode(file)) { const rpcArgs: RPCArgs = isRPCArgs(args) ? { ...args } : {} if (targetArgs) Object.assign(rpcArgs, appTargetRPCArgs(targetArgs)) return rpc(command, rpcArgs) } const graph = await loadDocument(requireFile(file)) prepareDocumentForRPC(graph, command, args) return executeRPCCommand(graph, command, args) as Result }