|
1 | 1 | import { type API } from './API'; |
2 | 2 | import { type EngineExecutionParams } from '../engine/Engine'; |
3 | | -import { type JsExecution } from '../engine/JsExecution'; |
4 | | -import { type Component, TFile } from 'obsidian'; |
| 3 | +import { type JsExecution, type JsExecutionContext } from '../engine/JsExecution'; |
| 4 | +import { Component, TFile } from 'obsidian'; |
5 | 5 | import { ResultRenderer } from '../engine/ResultRenderer'; |
6 | 6 |
|
7 | 7 | /** |
@@ -49,4 +49,37 @@ export class InternalAPI { |
49 | 49 | fullParams.code = await this.apiInstance.app.vault.read(file); |
50 | 50 | return await this.execute(fullParams); |
51 | 51 | } |
| 52 | + |
| 53 | + /** |
| 54 | + * Lead and execute the given file. |
| 55 | + * This method also handles the lifetime of the execution. |
| 56 | + * The component for the execution is created and destroyed automatically. |
| 57 | + * |
| 58 | + * @param path |
| 59 | + * @param params |
| 60 | + */ |
| 61 | + public async executeFileSimple(path: string, params?: Omit<EngineExecutionParams, 'code' | 'component'>): Promise<JsExecution> { |
| 62 | + const component = new Component(); |
| 63 | + component.load(); |
| 64 | + try { |
| 65 | + return await this.executeFile(path, { component: component, ...params }); |
| 66 | + } finally { |
| 67 | + component.unload(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public async getContextForFile(path: string): Promise<JsExecutionContext> { |
| 72 | + const file = this.apiInstance.app.vault.getAbstractFileByPath(path); |
| 73 | + if (!file || !(file instanceof TFile)) { |
| 74 | + throw new Error(`File ${path} not found.`); |
| 75 | + } |
| 76 | + |
| 77 | + const metadata = this.apiInstance.app.metadataCache.getFileCache(file); |
| 78 | + |
| 79 | + return { |
| 80 | + file: file, |
| 81 | + metadata: metadata, |
| 82 | + block: undefined, |
| 83 | + }; |
| 84 | + } |
52 | 85 | } |
0 commit comments