MCPcopy
hub / github.com/callumalpass/tasknotes / createNewFile

Method createNewFile

src/modals/FileSelectorModal.ts:188–231  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

186 }
187
188 private async createNewFile(): Promise<void> {
189 if (!this.currentQuery) {
190 new Notice("Please enter a file name");
191 return;
192 }
193
194 try {
195 let fileName = this.currentQuery;
196 // Remove .md extension if user typed it (we'll add it back)
197 if (fileName.toLowerCase().endsWith(".md")) {
198 fileName = fileName.slice(0, -3);
199 }
200
201 // Determine the folder
202 const folderPath = this.options.newFileFolder || "";
203 const filePath = folderPath ? `${folderPath}/${fileName}.md` : `${fileName}.md`;
204
205 // Check if file already exists
206 const existingFile = this.app.vault.getAbstractFileByPath(filePath);
207 if (existingFile) {
208 new Notice(`File "${filePath}" already exists`);
209 return;
210 }
211
212 // Ensure folder exists (check on-disk via adapter, not in-memory cache)
213 if (folderPath && !(await this.app.vault.adapter.exists(folderPath))) {
214 await createVaultFolder(this.app, folderPath);
215 }
216
217 // Create the file
218 const newFile = await createVaultFile(this.app, filePath, "");
219
220 this.resultHandled = true;
221 this.close();
222 this.options.onResult({ type: "created", file: newFile });
223 } catch (error) {
224 tasknotesLogger.error("Error creating file:", {
225 category: "persistence",
226 operation: "creating-file",
227 error: error,
228 });
229 new Notice("Failed to create file");
230 }
231 }
232
233 getSuggestions(query: string): TAbstractFile[] {
234 this.currentQuery = query.trim();

Callers 1

onOpenMethod · 0.95

Calls 6

createVaultFolderFunction · 0.90
createVaultFileFunction · 0.90
getAbstractFileByPathMethod · 0.80
existsMethod · 0.80
errorMethod · 0.80
closeMethod · 0.65

Tested by

no test coverage detected