Skip to content

Commit

Permalink
refactor: rename class & functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jun 17, 2022
1 parent a08c157 commit 839f549
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
8 changes: 4 additions & 4 deletions mkdocsPublisher/githubInteraction/delete.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Octokit } from "@octokit/core";
import { Notice } from "obsidian";
import { MkdocsPublicationSettings } from "../settings/interface";
import { GetFiles } from "./getFiles";
import { FilesManagement } from "./filesManagement";
import {Base64} from "js-base64";

export async function deleteFromGithub(silent = false, settings: MkdocsPublicationSettings, octokit: Octokit, branchName='main', GetFiles: GetFiles) {
const getAllFile = await GetFiles.getAllFileFromRepo(branchName, octokit, settings);
export async function deleteFromGithub(silent = false, settings: MkdocsPublicationSettings, octokit: Octokit, branchName='main', filesManagement: FilesManagement) {
const getAllFile = await filesManagement.getAllFileFromRepo(branchName, octokit, settings);
const filesInRepo = await filterGithubFile(getAllFile,
settings
);
Expand All @@ -30,7 +30,7 @@ export async function deleteFromGithub(silent = false, settings: MkdocsPublicati
}
return false;
}
const allSharedFiles = GetFiles.getAllFileWithPath();
const allSharedFiles = filesManagement.getAllFileWithPath();
const allSharedConverted = allSharedFiles.map((file) => { return file.converted; });
let deletedSuccess = 0;
let deletedFailed = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {MkdocsPublicationSettings} from "../settings/interface";
import {Octokit} from "@octokit/core";
import {getImageLinkOptions, getReceiptFolder} from "../utils/filePathConvertor";

export class GetFiles {
export class FilesManagement {
vault: Vault;
metadataCache: MetadataCache;
settings: MkdocsPublicationSettings;
Expand Down Expand Up @@ -219,8 +219,7 @@ export class GetFiles {
return newFiles;
}

async getAllplusNewEditedFiles(allFileWithPath:{converted: string, real: string}[] , githubSharedFiles: { file: string, sha: string }[], vault: Vault): Promise<TFile[]>{
const newFiles = this.getNewFiles(allFileWithPath, githubSharedFiles, vault); //new file : present in allFileswithPath but not in githubSharedFiles
async getEditedFiles(allFileWithPath:{converted: string, real: string}[] , githubSharedFiles: { file: string, sha: string }[], vault: Vault, newFiles: TFile[]): Promise<TFile[]>{
for (const file of allFileWithPath) {
if (githubSharedFiles.some((x) => x.file === file.converted.trim())) {
const githubSharedFile = githubSharedFiles.find((x) => x.file === file.converted.trim());
Expand Down
4 changes: 2 additions & 2 deletions mkdocsPublisher/githubInteraction/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Vault
} from "obsidian";
import { MkdocsPublicationSettings } from "../settings/interface";
import { GetFiles } from "./getFiles";
import { FilesManagement } from "./filesManagement";
import { Octokit } from "@octokit/core";
import { Base64 } from "js-base64";
import {deleteFromGithub} from "./delete"
Expand Down Expand Up @@ -39,7 +39,7 @@ export default class MkdocsPublish {
}

async publish(file: TFile, one_file = false, ref = "main") {
const shareFiles = new GetFiles(this.vault, this.metadataCache, this.settings, this.octokit);
const shareFiles = new FilesManagement(this.vault, this.metadataCache, this.settings, this.octokit);
const sharedKey = this.settings.shareKey;
const frontmatter = this.metadataCache.getFileCache(file).frontmatter;
if (
Expand Down
34 changes: 21 additions & 13 deletions mkdocsPublisher/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
import MkdocsPublish from "./githubInteraction/upload";
import { disablePublish } from "./utils/utils";
import {MkdocsPublicationSettings, DEFAULT_SETTINGS} from './settings/interface'
import { GetFiles } from "./githubInteraction/getFiles";
import { FilesManagement } from "./githubInteraction/filesManagement";
import {GithubBranch} from "./githubInteraction/branch";
import { Octokit } from "@octokit/core";
import {
deleteUnsharedDeletedNotes,
shareAllEditedNotes,
shareAllMarkedNotes,
shareNewNote,
shareOneNote
shareOneNote, shareOnlyEdited
} from "./utils/commands";


Expand All @@ -32,7 +32,7 @@ export default class MkdocsPublication extends Plugin {
octokit
);
const githubBranch = new GithubBranch(this.settings, octokit);
const shareFiles = new GetFiles(this.app.vault, this.app.metadataCache, this.settings, octokit);
const filesManagement = new FilesManagement(this.app.vault, this.app.metadataCache, this.settings, octokit);
const branchName = app.vault.getName() + "-" + new Date().toLocaleDateString('en-US').replace(/\//g, '-');


Expand Down Expand Up @@ -83,8 +83,8 @@ export default class MkdocsPublication extends Plugin {
);

this.addCommand({
id: "obs2mk-one",
name: "Share active file with Mkdocs Publisher",
id: "publisher-one",
name: "Share active file",
hotkeys: [],
checkCallback: (checking) => {
if (
Expand All @@ -104,13 +104,13 @@ export default class MkdocsPublication extends Plugin {
});

this.addCommand({
id: "Obs2MK-delete-clean",
name: "Delete from repository",
id: "publisher-delete-clean",
name: "Remove unshared and deleted file in repository",
hotkeys: [],
checkCallback: (checking) => {
if (this.settings.autoCleanUp) {
if (!checking) {
deleteUnsharedDeletedNotes(githubBranch, this.settings, octokit, shareFiles, branchName);
deleteUnsharedDeletedNotes(githubBranch, this.settings, octokit, filesManagement, branchName);
}
return true;
}
Expand All @@ -119,12 +119,20 @@ export default class MkdocsPublication extends Plugin {
});

this.addCommand({
id: "obs2mk-publish-all",
name: "Share all marked notes",
id: "publisher-publish-all",
name: "Upload all shared notes",
callback: async () => {
const statusBarElement = this.addStatusBarItem()
const sharedFiles = shareFiles.getSharedFiles();
await shareAllMarkedNotes(publish, this.settings, octokit, shareFiles, githubBranch, statusBarElement, branchName, sharedFiles);
const sharedFiles = filesManagement.getSharedFiles();
const statusBarItems = this.addStatusBarItem()
await shareAllMarkedNotes(publish, this.settings, octokit, filesManagement, githubBranch, statusBarItems, branchName, sharedFiles, true);
}
});

this.addCommand({
id: "publisher-upload-new",
name: "Upload new shared notes",
callback: async () => {
await shareNewNote(githubBranch, publish, this.settings, octokit, filesManagement, branchName, this.app.vault);
}
});

Expand Down
20 changes: 10 additions & 10 deletions mkdocsPublisher/utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import MkdocsPublish from "../githubInteraction/upload";
import { noticeMessage } from "./utils";
import {MkdocsPublicationSettings} from '../settings/interface'
import { deleteFromGithub } from '../githubInteraction/delete'
import { GetFiles } from "../githubInteraction/getFiles";
import { FilesManagement } from "../githubInteraction/filesManagement";
import {GithubBranch} from "../githubInteraction/branch";
import { Octokit } from "@octokit/core";
import {Notice, TFile, Vault} from "obsidian";


export async function shareAllMarkedNotes(publish: MkdocsPublish, settings: MkdocsPublicationSettings, octokit: Octokit, shareFiles: GetFiles, githubBranch: GithubBranch, statusBarItems: HTMLElement, branchName: string, sharedFiles: TFile[], createGithubBranch=true) {
export async function shareAllMarkedNotes(publish: MkdocsPublish, settings: MkdocsPublicationSettings, octokit: Octokit, filesManagement: FilesManagement, githubBranch: GithubBranch, statusBarItems: HTMLElement, branchName: string, sharedFiles: TFile[], createGithubBranch=true) {
try {
const statusBar = new ShareStatusBar(
statusBarItems,
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function shareAllMarkedNotes(publish: MkdocsPublish, settings: Mkdo
}
statusBar.finish(8000);
const noticeValue = `${publishedFiles.length - errorCount} notes`
await deleteFromGithub(true, settings, octokit, branchName, shareFiles);
await deleteFromGithub(true, settings, octokit, branchName, filesManagement);
const update = await githubBranch.updateRepository(branchName);
if (update) {
await noticeMessage(publish, noticeValue, settings)
Expand All @@ -55,11 +55,11 @@ export async function shareAllMarkedNotes(publish: MkdocsPublish, settings: Mkdo
}
}

export async function deleteUnsharedDeletedNotes(githubBranch: GithubBranch, settings: MkdocsPublicationSettings, octokit: Octokit, shareFiles: GetFiles, branchName: string) {
export async function deleteUnsharedDeletedNotes(githubBranch: GithubBranch, settings: MkdocsPublicationSettings, octokit: Octokit, filesManagement: FilesManagement, branchName: string) {
try {
new Notice(`Starting cleaning ${settings.githubRepo} `)
await githubBranch.newBranch(branchName);
await deleteFromGithub(false, settings,octokit, branchName, shareFiles);
await deleteFromGithub(false, settings,octokit, branchName, filesManagement);
await githubBranch.updateRepository(branchName);
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -87,15 +87,15 @@ export async function shareOneNote(branchName: string, githubBranch: GithubBranc
}
}

export async function shareNewNote(githubBranch: GithubBranch, publish:MkdocsPublish ,settings: MkdocsPublicationSettings, octokit: Octokit, shareFiles: GetFiles, branchName: string, vault: Vault) {
export async function shareNewNote(githubBranch: GithubBranch, publish:MkdocsPublish , settings: MkdocsPublicationSettings, octokit: Octokit, filesManagement: FilesManagement, branchName: string, vault: Vault) {
const branchMaster = await githubBranch.getMasterBranch();
const sharedFilesWithPaths = shareFiles.getAllFileWithPath();
const githubSharedNotes = await shareFiles.getAllFileFromRepo(branchMaster, octokit, settings);
const newlySharedNotes = shareFiles.getNewFiles(sharedFilesWithPaths, githubSharedNotes, vault);
const sharedFilesWithPaths = filesManagement.getAllFileWithPath();
const githubSharedNotes = await filesManagement.getAllFileFromRepo(branchMaster, octokit, settings);
const newlySharedNotes = filesManagement.getNewFiles(sharedFilesWithPaths, githubSharedNotes, vault);
if (newlySharedNotes.length > 0) {
const statusBarElement = this.addStatusBarItem();
await githubBranch.newBranch(branchName);
await shareAllMarkedNotes(publish, this.settings, octokit, shareFiles, githubBranch, statusBarElement, branchName, newlySharedNotes);
await shareAllMarkedNotes(publish, this.settings, octokit, filesManagement, githubBranch, statusBarElement, branchName, newlySharedNotes);
} else {
new Notice("No new notes to share.");
}
Expand Down

0 comments on commit 839f549

Please sign in to comment.