Skip to content

Commit

Permalink
Merge pull request #56 from cloud-atlas-ai:avoid-TFile-cast
Browse files Browse the repository at this point in the history
Per the [automated scan](obsidianmd/obsidian-releases#2938 (comment)) we should use a instanceof check to make sure that the return value of getAbstractFileByPath is actually a TFile
  • Loading branch information
muness authored Jan 19, 2024
2 parents d13a5ee + 97e20bc commit 3d80c72
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
isOtherText,
isWord,
joinStrings,
getFileByPath,
} from "./utils";
import {
CloudAtlasGlobalSettingsTab,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -277,7 +276,7 @@ export default class CloudAtlasPlugin extends Plugin {

flowConfigFromPath = async (filePath: string): Promise<FlowConfig> => {
const metadata = this.app.metadataCache.getFileCache(
this.app.vault.getAbstractFileByPath(filePath) as TFile
getFileByPath(filePath, this.app)
);

const llmOptions: LlmOptions = {};
Expand Down Expand Up @@ -364,7 +363,7 @@ export default class CloudAtlasPlugin extends Plugin {

readNote = async (filePath: string): Promise<string> => {
const content = await this.app.vault.read(
this.app.vault.getAbstractFileByPath(filePath) as TFile
getFileByPath(filePath, this.app)
);
return content;
};
Expand Down Expand Up @@ -400,7 +399,7 @@ export default class CloudAtlasPlugin extends Plugin {
excludePatterns: RegExp[]
): Promise<AdditionalContext> => {
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
Expand Down Expand Up @@ -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)
);
};
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 3d80c72

Please sign in to comment.