From 97e20bc8dce05cccb7221c35b3aae74f00a45e36 Mon Sep 17 00:00:00 2001 From: Muness Castle <931+muness@users.noreply.github.com> Date: Fri, 19 Jan 2024 11:22:52 -0500 Subject: [PATCH] Per the [automated scan](https://github.com/obsidianmd/obsidian-releases/pull/2938#issuecomment-1899560221) we should use a instanceof check to make sure that the return value of getAbstractFileByPath is actually a TFile --- src/main.ts | 21 ++++++++++----------- src/utils.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main.ts b/src/main.ts index f5494e5..8a8f43a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -46,6 +46,7 @@ import { isOtherText, isWord, joinStrings, + getFileByPath, } from "./utils"; import { CloudAtlasGlobalSettingsTab, @@ -178,9 +179,7 @@ export default class CloudAtlasPlugin extends Plugin { try { const flowConfig = await this.flowConfigFromPath(filePath); - const flowFile = this.app.vault.getAbstractFileByPath( - filePath - ) as TFile; + const flowFile = getFileByPath(filePath, this.app); // Inherit booleans unless specifically defined. if (previousConfig) { @@ -277,7 +276,7 @@ export default class CloudAtlasPlugin extends Plugin { flowConfigFromPath = async (filePath: string): Promise => { const metadata = this.app.metadataCache.getFileCache( - this.app.vault.getAbstractFileByPath(filePath) as TFile + getFileByPath(filePath, this.app) ); const llmOptions: LlmOptions = {}; @@ -364,7 +363,7 @@ export default class CloudAtlasPlugin extends Plugin { readNote = async (filePath: string): Promise => { const content = await this.app.vault.read( - this.app.vault.getAbstractFileByPath(filePath) as TFile + getFileByPath(filePath, this.app) ); return content; }; @@ -400,7 +399,7 @@ export default class CloudAtlasPlugin extends Plugin { excludePatterns: RegExp[] ): Promise => { const additionalContext: AdditionalContext = {}; - const file = this.app.vault.getAbstractFileByPath(filePath) as TFile; + const file = getFileByPath(filePath, this.app); const activeBacklinks = // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -562,7 +561,7 @@ export default class CloudAtlasPlugin extends Plugin { createFlow = async (flow: string) => { await this.create( - `CloudAtlas/${flow}.canvas`, + `CloudAtlas/${flow}.flow.canvas`, JSON.stringify(CANVAS_CONTENT) ); }; @@ -916,14 +915,14 @@ export default class CloudAtlasPlugin extends Plugin { const canvasContent = payloadToGraph(payload); - const canvasFilePath = `CloudAtlas/${flow}.canvas`; - const canvasFile = await this.app.vault.getAbstractFileByPath( - canvasFilePath + const canvasFilePath = `CloudAtlas/${flow}.flow.canvas`; + const canvasFile = await getFileByPath( + canvasFilePath, this.app ); if (!canvasFile) { this.app.vault.create( - `CloudAtlas/${flow}.canvas`, + `CloudAtlas/${flow}.flow.canvas`, JSON.stringify(canvasContent) ); } else { diff --git a/src/utils.ts b/src/utils.ts index ed87c0b..0433c78 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,17 @@ import { readFileSync } from "fs"; import WordExtractor from "word-extractor"; import { AdditionalContext, Payload, User } from "./interfaces"; +import { App, TAbstractFile, TFile } from "obsidian"; + +// Utility function to safely get a TFile by path +export function getFileByPath(filePath: string, app: App): TFile { + const file: TAbstractFile | null = app.vault.getAbstractFileByPath(filePath); + if (file instanceof TFile) { + return file; + } else { + throw new Error(`The path ${filePath} does not refer to a valid file.`); + } +} export function combinePayloads( base: Payload | null,