Skip to content

Commit

Permalink
recursive delete in dryRun
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jan 21, 2024
1 parent e27793e commit dac77f3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
43 changes: 33 additions & 10 deletions src/GitHub/delete.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 };
Expand All @@ -334,31 +337,51 @@ 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(
(f) => {
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;
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
6 changes: 3 additions & 3 deletions src/GitHub/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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}`);
Expand Down
18 changes: 11 additions & 7 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,23 @@ export async function purgeNotesRemote(
monoRepo: MonoRepoProperties,
): Promise<void|boolean> {
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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/utils/data_validation_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export async function checkRepositoryValidityWithRepoFrontmatter(
numberOfFile: number=1
): Promise<boolean> {
const settings = PublisherManager.settings;
if (settings.github.dryRun.enable) return true;
try {
/**
* verify for each repoFrontmatter if verifiedRepo is true
Expand Down

0 comments on commit dac77f3

Please sign in to comment.