diff --git a/src/TemplateArchiveProcessor.ts b/src/TemplateArchiveProcessor.ts index 5708205..b17b0a7 100644 --- a/src/TemplateArchiveProcessor.ts +++ b/src/TemplateArchiveProcessor.ts @@ -18,11 +18,9 @@ import { Template } from '@accordproject/cicero-core'; import { TemplateMarkInterpreter } from './TemplateMarkInterpreter'; import { TemplateMarkTransformer } from '@accordproject/markdown-template'; import { transform } from '@accordproject/markdown-transform'; -import { TypeScriptToJavaScriptCompiler } from './TypeScriptToJavaScriptCompiler'; import Script from '@accordproject/cicero-core/types/src/script'; -import { TwoSlashReturn } from '@typescript/twoslash'; import { JavaScriptEvaluator } from './JavaScriptEvaluator'; -import { SMART_LEGAL_CONTRACT_BASE64 } from './runtime/declarations'; +import { compileUserLogic, CompiledUserLogic } from './UserLogic'; export type State = object; export type Response = object; @@ -66,9 +64,14 @@ export class TemplateArchiveProcessor { const metadata = this.template.getMetadata(); const templateKind = metadata.getTemplateType() !== 0 ? 'clause' : 'contract'; + // Compile the template's logic/logic.ts (when present) so that + // inline formulas {{% expr %}} can call helpers like + // monthlyPaymentFormula(...) declared in user code (issue #147). + const userLogic = await this.compileLogic(); + // Get the data const modelManager = this.template.getModelManager(); - const engine = new TemplateMarkInterpreter(modelManager, {}); + const engine = new TemplateMarkInterpreter(modelManager, {}, undefined, userLogic); const templateMarkTransformer = new TemplateMarkTransformer(); const templateMarkDom = templateMarkTransformer.fromMarkdownTemplate( { content: this.template.getTemplate() }, modelManager, templateKind, {options}); @@ -82,6 +85,31 @@ export class TemplateArchiveProcessor { } + /** + * Compiles the template's `logic/logic.ts` using the same TypeScript + * compilation pipeline that {@link trigger} and {@link init} use, but + * returns the result via {@link CompiledUserLogic} so it can also be + * consumed by {@link draft} (so inline formulas can call helpers from + * logic.ts — see issue #147). Returns `undefined` for non-TypeScript + * runtimes or when the template ships no logic. + */ + private async compileLogic(): Promise { + const logicManager = this.template.getLogicManager(); + if (logicManager.getLanguage() !== 'typescript') { + return undefined; + } + const tsFiles: Array