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

85.71% Statements 12/14
100% Branches 4/4
100% Functions 1/1
85.71% Lines 12/14

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                    1x     6x   6x 1x 1x     5x     5x 5x           5x   5x   2x     3x      
import * as vscode from 'vscode';
import { compile } from 'triplate';
import { resolveQueryContext, getContextDocument } from '@src/utilities/query-context';
import { renderTemplateInteractively } from '@src/languages/triplate/triplate-prompt';
import { routeRenderedTriplate } from '@src/languages/triplate/route-rendered-triplate';
 
/**
 * Lets the user pick a declared example or enter parameter values for a triplate template,
 * renders it, and executes (SPARQL) or validates/opens (RDF) the result.
 */
export const executeTriplateTemplate = {
	id: 'mentor.command.executeTriplateTemplate',
	handler: async (documentUri?: vscode.Uri | string): Promise<void> => {
		const source = resolveQueryContext(documentUri);
 
		if (!source) {
			vscode.window.showWarningMessage('No template document found to execute.');
			return;
		}
 
		const text = getContextDocument(source).getText();
		let compiled: ReturnType<typeof compile>;
 
		try {
			compiled = compile(text);
		} catch (error) {
			vscode.window.showErrorMessage(`Failed to compile template: ${(error as Error).message}`);
			return;
		}
 
		const rendered = await renderTemplateInteractively(compiled);
 
		if (rendered === undefined) {
			// The user cancelled, or rendering failed (already reported).
			return;
		}
 
		await routeRenderedTriplate(source, rendered);
	}
};