From dac77f3da93aa08f90cae079b8a6b6a9a16b0f8a Mon Sep 17 00:00:00 2001 From: Mara Date: Sun, 21 Jan 2024 17:59:28 +0100 Subject: [PATCH] recursive delete in dryRun --- src/GitHub/delete.ts | 43 ++++++++++++++++++++++++------- src/GitHub/files.ts | 2 +- src/GitHub/upload.ts | 6 ++--- src/commands/index.ts | 18 ++++++++----- src/utils/data_validation_test.ts | 1 + 5 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/GitHub/delete.ts b/src/GitHub/delete.ts index 79f0961e..15ea44c9 100644 --- a/src/GitHub/delete.ts +++ b/src/GitHub/delete.ts @@ -1,7 +1,7 @@ import { Octokit } from "@octokit/core"; import i18next from "i18next"; import { Base64 } from "js-base64"; -import { MetadataCache, normalizePath, Notice, parseYaml, TFile, TFolder } from "obsidian"; +import { MetadataCache, normalizePath, Notice, parseYaml, TAbstractFile, TFile, TFolder, Vault } from "obsidian"; import { Deleted, @@ -318,12 +318,15 @@ function cleanDryRun( const {vault, settings} = filesManagement; const app = filesManagement.plugin.app; const repo = repoProperties.frontmatter; - const dryRunFolderPath = normalizePath(repo.dryRun.folderName.replace("{{owner}}", repo.owner).replace("{{repo}}", repo.repo).replace("{{branch}}", repo.branch)); + const dryRunFolderPath = normalizePath(repo.dryRun.folderName + .replace("{{owner}}", repo.owner) + .replace("{{repo}}", repo.repo) + .replace("{{branch}}", repo.branch)); const dryRunFolder = vault.getAbstractFileByPath(dryRunFolderPath); if (!dryRunFolder || dryRunFolder instanceof TFile) return {success: false, deleted: [], undeleted: []}; - const dryRunFolderChildren = (dryRunFolder as TFolder).children; - const dryRunFiles = dryRunFolderChildren.filter((file) => { - return file instanceof TFile; + const dryRunFiles:TFile[] = []; + Vault.recurseChildren(dryRunFolder as TFolder, (file: TAbstractFile) => { + if (!excludedFileFromDelete(normalizePath(file.path.replace(dryRunFolderPath, "")), settings) && (isAttachment(file.path) || file.path.match("md$")) && file instanceof TFile) dryRunFiles.push(file); }); const allSharedFiles = filesManagement.getAllFileWithPath(repoProperties.repo).map((file) => { return { converted: file.converted, repo: file.repoFrontmatter }; @@ -334,9 +337,11 @@ function cleanDryRun( undeleted: [], success: false, }; + const deletedFolder: TAbstractFile[] = []; for (const file of dryRunFiles) { + const convertedPath = normalizePath(file.path.replace(dryRunFolderPath, "")); const isInObsidian = allSharedFiles.some( - (f) => f.converted === file.path + (f) => f.converted === convertedPath ); const isMarkdownForAnotherRepo = file.path.trim().endsWith(".md") ? !allSharedFiles.some( @@ -344,21 +349,39 @@ function cleanDryRun( let repoFrontmatter = f.repo; if (Array.isArray(repoFrontmatter)) { repoFrontmatter = repoFrontmatter.find((r) => JSON.stringify(r.repo) === JSON.stringify(repo.repo)); - } return f.converted === file.path && repoFrontmatter; + } return f.converted === convertedPath && repoFrontmatter; }) : false; const isNeedToBeDeleted = isInObsidian ? isMarkdownForAnotherRepo : true; if (isNeedToBeDeleted) { - const indexFile = (file.path.contains(settings.upload.folderNote.rename)) ? indexFileDryRun(file as TFile, app.metadataCache) : false; + const indexFile = (convertedPath.contains(settings.upload.folderNote.rename)) ? indexFileDryRun(file as TFile, app.metadataCache) : false; if (!indexFile) { - notif({settings}, `[DRYRUN] trying to delete file : ${file.path} from ${repo.owner}/${repo.repo}`); + notif({settings}, `[DRYRUN] trying to delete file : ${file.path} from ${dryRunFolderPath}`); vault.trash(file, false); deletedSuccess++; + deletedFolder.push(file); } } + } + //recursive delete empty folder in dryRunFolder + //empty folder are folder with children.length === 0 + const dryRunFolders:TFolder[] = []; + Vault.recurseChildren(vault.getAbstractFileByPath(dryRunFolderPath) as TFolder, (file: TAbstractFile) => { + if (file instanceof TFolder) { + dryRunFolders.push(file); + } + }); + for (const folder of dryRunFolders.reverse()) { + const children = folder.children.filter((child) => !deletedFolder.includes(child)); + if (children.length === 0) { + deletedFolder.push(folder); + vault.trash(folder, false); + deletedSuccess++; + } } - const successMsg = i18next.t("deletion.noFile") ; + + const successMsg = deletedSuccess > 0 ? (i18next.t("deletion.success", {nb: deletedSuccess.toString()})) : i18next.t("deletion.noFile"); if (!silent) new Notice(successMsg); result.success = deletedSuccess === 0; diff --git a/src/GitHub/files.ts b/src/GitHub/files.ts index c2feff05..6de7fddb 100644 --- a/src/GitHub/files.ts +++ b/src/GitHub/files.ts @@ -89,7 +89,7 @@ export class FilesManagement extends Publisher { */ getAllFileWithPath(repo: Repository | null): ConvertedLink[] { - const files = this.vault.getFiles(); + const files = this.vault.getFiles().filter((x) => !x.path.startsWith(this.settings.github.dryRun.folderName)); const allFileWithPath: ConvertedLink[] = []; for (const file of files) { if (isAttachment(file.name)) { diff --git a/src/GitHub/upload.ts b/src/GitHub/upload.ts index 8f9b94bb..467a65e3 100644 --- a/src/GitHub/upload.ts +++ b/src/GitHub/upload.ts @@ -332,7 +332,7 @@ export default class Publisher { const embeddedUploaded = embeded.uploaded; embeddedUploaded.push(uploaded); - if (autoclean && repo.autoclean && !this.settings.github.dryRun) { + if ((autoclean && repo.autoclean) || repo.dryRun.autoclean) { deleted = await deleteFromGithub( true, this.branchName, @@ -452,7 +452,7 @@ export default class Publisher { if (this.settings.github.dryRun.enable) { const folderName = this.settings.github.dryRun.folderName .replace("{{repo}}", repoFrontmatter.repo) - .replace("{{branch}}", this.branchName) + .replace("{{branch}}", repoFrontmatter.branch) .replace("{{owner}}", repoFrontmatter.owner); const dryRunPath = normalizePath(`${folderName}/${path}`); const isAlreadyExist = this.vault.getAbstractFileByPath(dryRunPath); @@ -499,7 +499,7 @@ export default class Publisher { //create a new file in the vault const folderName = this.settings.github.dryRun.folderName .replace("{{repo}}", repoFrontmatter.repo) - .replace("{{branch}}", this.branchName) + .replace("{{branch}}", repoFrontmatter.branch) .replace("{{owner}}", repoFrontmatter.owner); const newPath = normalizePath(`${folderName}/${path}`); diff --git a/src/commands/index.ts b/src/commands/index.ts index 2f97379f..fb1ada6c 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -134,19 +134,23 @@ export async function purgeNotesRemote( monoRepo: MonoRepoProperties, ): Promise { try { + const noticeFragment = document.createDocumentFragment(); + noticeFragment.createSpan({ cls: ["obsidian-publisher", "notification"] }).innerHTML = i18next.t("informations.startingClean", { repo: monoRepo.frontmatter }); new Notice( - i18next.t("informations.startingClean", {repo: monoRepo.frontmatter}) + noticeFragment ); - const isValid = checkRepositoryValidityWithRepoFrontmatter(PublisherManager, monoRepo.frontmatter); + const isValid = await checkRepositoryValidityWithRepoFrontmatter(PublisherManager, monoRepo.frontmatter); if (!isValid) return false; - await PublisherManager.newBranch(monoRepo.frontmatter); + if (!PublisherManager.settings.github.dryRun.enable) + await PublisherManager.newBranch(monoRepo.frontmatter); const deleted = await deleteFromGithub( false, branchName, PublisherManager, monoRepo ); - await PublisherManager.updateRepository(monoRepo.frontmatter); + if (!PublisherManager.settings.github.dryRun.enable) + await PublisherManager.updateRepository(monoRepo.frontmatter); if (PublisherManager.settings.plugin.displayModalRepoEditing) new ListChangedFiles(PublisherManager.plugin.app, deleted).open(); } catch (e) { notif({settings: PublisherManager.settings, e: true}, e); @@ -276,7 +280,7 @@ export async function shareNewNote( ); const statusBarElement = plugin.addStatusBarItem(); - const isValid = checkRepositoryValidityWithRepoFrontmatter(PublisherManager, monoRepo.frontmatter, newlySharedNotes.length); + const isValid = await checkRepositoryValidityWithRepoFrontmatter(PublisherManager, monoRepo.frontmatter, newlySharedNotes.length); if (!isValid) return false; await PublisherManager.newBranch(monoRepo.frontmatter); await shareAllMarkedNotes( @@ -326,7 +330,7 @@ export async function shareAllEditedNotes( ); const statusBarElement = plugin.addStatusBarItem(); - const isValid = checkRepositoryValidityWithRepoFrontmatter(PublisherManager, monoRepo.frontmatter, newlySharedNotes.length); + const isValid = await checkRepositoryValidityWithRepoFrontmatter(PublisherManager, monoRepo.frontmatter, newlySharedNotes.length); if (!isValid) return false; await PublisherManager.newBranch(monoRepo.frontmatter); await shareAllMarkedNotes( @@ -372,7 +376,7 @@ export async function shareOnlyEdited( (i18next.t("informations.foundNoteToSend", {nbNotes: newlySharedNotes.length})) ); const statusBarElement = PublisherManager.plugin.addStatusBarItem(); - const isValid = checkRepositoryValidityWithRepoFrontmatter(PublisherManager, repoFrontmatter, newlySharedNotes.length); + const isValid = await checkRepositoryValidityWithRepoFrontmatter(PublisherManager, repoFrontmatter, newlySharedNotes.length); if (!isValid) return false; await PublisherManager.newBranch(repoFrontmatter); await shareAllMarkedNotes( diff --git a/src/utils/data_validation_test.ts b/src/utils/data_validation_test.ts index afaa3561..4100bd8d 100644 --- a/src/utils/data_validation_test.ts +++ b/src/utils/data_validation_test.ts @@ -318,6 +318,7 @@ export async function checkRepositoryValidityWithRepoFrontmatter( numberOfFile: number=1 ): Promise { const settings = PublisherManager.settings; + if (settings.github.dryRun.enable) return true; try { /** * verify for each repoFrontmatter if verifiedRepo is true