MCPcopy Create free account
hub / github.com/chhoumann/quickadd / overwriteFileWithTemplate

Function overwriteFileWithTemplate

src/engine/TemplateEngine.ts:659–706  ·  view source on GitHub ↗
(
		file: TFile,
		resolvedTemplatePath: string
	)

Source from the content-addressed store, hash-verified

657
658
659 protected async overwriteFileWithTemplate(
660 file: TFile,
661 resolvedTemplatePath: string
662 ) {
663 try {
664 const templateContent: string = await this.getTemplateContent(
665 resolvedTemplatePath
666 );
667
668 // Use the existing file's basename as the title
669 const fileBasename = file.basename;
670 this.formatter.setTitle(fileBasename);
671 // {{FOLDER}} reflects the existing file's folder, which can differ
672 // from the choice's configured folder.
673 this.formatter.setTargetFolderPath(parentFolderPath(file.path));
674
675 const formattedTemplateContent: string =
676 await this.formatter.withTemplatePropertyCollection(() =>
677 this.formatter.formatFileContent(templateContent),
678 );
679
680 // Get template variables before modifying the file
681 const templateVars = this.formatter.getAndClearTemplatePropertyVars();
682
683 log.logMessage(`TemplateEngine.overwriteFileWithTemplate: Collected ${templateVars.size} template property variables for ${file.path}`);
684 if (templateVars.size > 0) {
685 log.logMessage(`Variables: ${Array.from(templateVars.keys()).join(', ')}`);
686 }
687
688 await this.app.vault.modify(file, formattedTemplateContent);
689
690 // Post-process front matter for template property types BEFORE Templater
691 if (shouldPostProcessFrontMatter(file, templateVars)) {
692 await postProcessFrontMatter(this.app, file, templateVars);
693 }
694
695 // Process Templater commands
696 await overwriteTemplaterOnce(this.app, file);
697
698 return file;
699 } catch (err) {
700 if (isMacroAbortError(err)) {
701 throw err;
702 }
703 reportError(err, "Could not overwrite file with template");
704 return null;
705 }
706 }
707
708 protected async appendToFileWithTemplate(
709 file: TFile,

Callers

nothing calls this directly

Calls 13

parentFolderPathFunction · 0.90
postProcessFrontMatterFunction · 0.90
reportErrorFunction · 0.90
overwriteTemplaterOnceFunction · 0.85
isMacroAbortErrorFunction · 0.85
logMessageMethod · 0.65
getTemplateContentMethod · 0.45
setTitleMethod · 0.45
setTargetFolderPathMethod · 0.45
formatFileContentMethod · 0.45

Tested by

no test coverage detected