From 6199d4a2ed74bad3e83c329facc90af8a3260ffa Mon Sep 17 00:00:00 2001 From: "Hascoet Kevin (Neolex)" Date: Sun, 3 Dec 2023 12:03:02 +0100 Subject: [PATCH] stop modifying file if no change --- main.ts | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/main.ts b/main.ts index f5c0933..c6a97d5 100644 --- a/main.ts +++ b/main.ts @@ -162,27 +162,6 @@ export default class H1ObsidianPlugin extends Plugin { } - async overwriteFiles(reportNotes: Array) { - try { - const folderPath = normalizePath(`${this.settings.directory}/Bugs`); - let existingReportFiles = this.app.vault.getMarkdownFiles() - existingReportFiles = existingReportFiles.filter(file => file.path.startsWith(folderPath)); - for (const reportNote of reportNotes) { - const foundExistingReport = existingReportFiles.find((reportFile: TFile) => reportFile.basename.split("-").pop() === reportNote.id); - if(foundExistingReport){ - await this.app.vault.modify(foundExistingReport, reportNote["content"]); - - }else{ - console.log("report "+reportNote["id"]+" not found create "+reportNote["filename"]) - await this.app.vault.create(reportNote["filename"],reportNote["content"]) - } - } - } catch (err) { - new Notice('Error: Unable to overwrite the file:'+err); - console.log('Error overwriting file:', err); - } - } - async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); } @@ -249,7 +228,7 @@ export default class H1ObsidianPlugin extends Plugin { const newReportNote: ReportNote = { id: item.id, content: noteContent, - filename: fileName + filename: fileName, }; reportNotes.push(newReportNote); @@ -258,6 +237,30 @@ export default class H1ObsidianPlugin extends Plugin { } + async overwriteFiles(reportNotes: Array) { + try { + const folderPath = normalizePath(`${this.settings.directory}/Bugs`); + let existingReportFiles = this.app.vault.getMarkdownFiles() + existingReportFiles = existingReportFiles.filter(file => file.path.startsWith(folderPath)); + for (const reportNote of reportNotes) { + const foundExistingReport = existingReportFiles.find((reportFile: TFile) => reportFile.basename.split("-").pop() === reportNote.id); + if(foundExistingReport){ + let currentContent = await this.app.vault.cachedRead(foundExistingReport); + if(currentContent!=reportNote["content"]){ + await this.app.vault.modify(foundExistingReport, reportNote["content"]); + } + + }else{ + console.log("report "+reportNote["id"]+" not found create "+reportNote["filename"]) + await this.app.vault.create(reportNote["filename"],reportNote["content"]) + } + } + } catch (err) { + new Notice('Error: Unable to overwrite the file:'+err); + console.log('Error overwriting file:', err); + } + } + async getBountyReport(reportId: number, earnings: any[]) { let ret = 0; for (const earning of earnings) {