(
nodes: SceneNode[],
preferredLang?: CodeLanguage,
resolveTokens?: boolean,
vectorMode: GetCodeParametersInput['vectorMode'] = 'smart',
runtimeOptions: GetCodeRuntimeOptions = {}
)
| 126 | |
| 127 | type RenderStep = 'render' | 'stringify' | 'transform' |
| 128 | |
| 129 | export async function handleGetCode( |
| 130 | nodes: SceneNode[], |
| 131 | preferredLang?: CodeLanguage, |
| 132 | resolveTokens?: boolean, |
| 133 | vectorMode: GetCodeParametersInput['vectorMode'] = 'smart', |
| 134 | runtimeOptions: GetCodeRuntimeOptions = {} |
| 135 | ): Promise<GetCodeResult> { |
| 136 | const trace = createTrace() |
| 137 | const { now, stamp } = trace |
| 138 | const traceInfo: TraceInfo = { now, stamp } |
| 139 | |
| 140 | if (nodes.length !== 1) { |
| 141 | throw new Error('Select exactly one node or provide a single root node id.') |
| 142 | } |
| 143 | |
| 144 | const node = nodes[0] |
| 145 | if (!node.visible) { |
| 146 | throw new Error('The selected node is not visible.') |
| 147 | } |
| 148 | |
| 149 | let t = now() |
| 150 | const tree = buildVisibleTree(nodes) |
| 151 | stamp('tree', t) |
| 152 | const rootId = tree.rootIds[0] |
| 153 | if (!rootId) { |
| 154 | throw new Error('No renderable nodes found for the current selection.') |
| 155 | } |
| 156 | if (tree.stats.capped) { |
| 157 | const depth = tree.stats.depthLimit ?? tree.stats.maxDepth |
| 158 | logger.warn(`[get_code] Tree depth capped at ${depth}; output may be incomplete.`) |
| 159 | } |
| 160 | |
| 161 | const config = currentCodegenConfig() |
| 162 | const pluginCode = activePlugin.value?.code |
| 163 | const codeBudget = runtimeOptions.unbounded ? resolveUnlimitedCodeBudget() : resolveCodeBudget() |
| 164 | const budgetPreflight = preflightGetCodeBudget(tree, rootId, { |
| 165 | maxResultBytes: codeBudget.maxResultBytes, |
| 166 | pluginEnabled: !!pluginCode, |
| 167 | unbounded: !!runtimeOptions.unbounded |
| 168 | }) |
| 169 | const earlyShell = budgetPreflight.kind === 'shell' |
| 170 | |
| 171 | t = now() |
| 172 | const variableCache = new Map<string, Variable | null>() |
| 173 | const cache = createGetCodeCacheContext(variableCache, { metrics: true }) |
| 174 | const mappings = buildVariableMappings(nodes, variableCache, cache.readers, { |
| 175 | traverseChildren: !earlyShell |
| 176 | }) |
| 177 | stamp('vars', t) |
| 178 | |
| 179 | const { pluginComponents, pluginSkipped } = |
| 180 | pluginCode && !earlyShell |
| 181 | ? await collectPluginOutput(tree, config, pluginCode, preferredLang) |
| 182 | : { pluginComponents: undefined, pluginSkipped: new Set<string>() } |
| 183 | |
| 184 | t = now() |
| 185 | const plan = earlyShell |
nothing calls this directly
no test coverage detected