All files / commands/triplate execute-triplate-example.ts

66.66% Statements 12/18
75% Branches 3/4
100% Functions 2/2
64.7% Lines 11/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                  1x     2x   2x             2x 2x           2x 1x 1x         1x 1x           1x      
import * as vscode from 'vscode';
import { compile } from 'triplate';
import { resolveQueryContext, getContextDocument } from '@src/utilities/query-context';
import { routeRenderedTriplate } from '@src/languages/triplate/route-rendered-triplate';
 
/**
 * Renders a triplate template using the declared values of a named example block
 * and executes (SPARQL) or validates/opens (RDF) the result. No prompting.
 */
export const executeTriplateExample = {
	id: 'mentor.command.executeTriplateExample',
	handler: async (documentUri: vscode.Uri | string, exampleId: string): Promise<void> => {
		const source = resolveQueryContext(documentUri);
 
		Iif (!source) {
			vscode.window.showWarningMessage('No template document found to execute.');
			return;
		}
 
		let compiled: ReturnType<typeof compile>;
 
		try {
			compiled = compile(getContextDocument(source).getText());
		} catch (error) {
			vscode.window.showErrorMessage(`Failed to compile template: ${(error as Error).message}`);
			return;
		}
 
		if (!compiled.examples.some(e => e.id === exampleId)) {
			vscode.window.showErrorMessage(`No example "${exampleId}" found in this template.`);
			return;
		}
 
		let rendered: string;
 
		try {
			rendered = compiled.previewExample(exampleId);
		} catch (error) {
			vscode.window.showErrorMessage(`Failed to render example "${exampleId}": ${(error as Error).message}`);
			return;
		}
 
		await routeRenderedTriplate(source, rendered);
	}
};