Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
refactor: Separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Apr 15, 2022
1 parent 7b4319d commit 2f88808
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 84 deletions.
88 changes: 88 additions & 0 deletions Snippet-downloader/addSnippets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {snippetDownloaderSettings, snippetRepo} from "./settings";
import {Notice, Vault} from "obsidian";
import {
checkLastUpdate,
downloadSnippet, grabLastCommitDate,
listSnippetfromRepo
} from "./downloader";
import {basename, searchExcluded} from "./utils";
import {snippetUpdate} from "./modals/updateSnippets";
import {removeSnippetFromExcluded} from "./removeSnippet"

export async function addSnippet(repoPath: string, settings: snippetDownloaderSettings, vault: Vault) {
const snippetList = settings.snippetList;
let excludedSnippet = settings.errorSnippet;
const catchErrors: string[] = [];
if (!snippetList.some(snippet => snippet.repo === repoPath)) {
const newSnippetList = await listSnippetfromRepo(repoPath, settings);
if (newSnippetList.length === 0) {
new Notice('Error 😿, snippet or repository not found')
return snippetList;
}
snippetList.push({
repo: repoPath,
snippetsContents: await listSnippetfromRepo(repoPath, settings)
})
const snippet = snippetList.find(snippet => snippet.repo === repoPath)
for (const snippetContents of snippet.snippetsContents) {
const Success=await downloadSnippet(repoPath, snippetContents.name, vault)
if (!Success) {
excludedSnippet += snippetContents.name.replace('.css', '') + ', ';
catchErrors.push(snippetContents.name.replace('.css', ''));
}
}
let messageNotice = `Successfully added ${repoPath}. ${newSnippetList.length} snippets added. 🎉`
if (catchErrors.length > 0) {
messageNotice += `\n${catchErrors.length} snippets not added😿: ${catchErrors.join(', ')}`
}
new Notice(messageNotice);
return [snippetList, excludedSnippet]
}
new Notice ('Error : this repo is already in the list 😿');
return [snippetList, excludedSnippet]
}

export async function updateRepo(repoPath: string, snippetList: snippetRepo[], vault: Vault, excludedSnippets: string, errorSnippets: string) {
const snippet = snippetList.find(snippet => snippet.repo === repoPath);
if (snippet) {
for (const snippetContent of snippet.snippetsContents) {
if (await checkLastUpdate(snippetContent, repoPath) && (snippetContent.name !== 'obsidian.css') && (!searchExcluded(excludedSnippets, snippetContent.name)) && (!searchExcluded(errorSnippets, snippetContent.name)))
{
const successDownloaded=await downloadSnippet(repoPath, snippetContent.name, vault)
if (successDownloaded) {
snippetContent.lastUpdate = await grabLastCommitDate(repoPath, snippetContent.name);
} else {
errorSnippets += snippetContent.name.replace('.css', '') + ', ';
}
}
}
}
snippetList = removeSnippetFromExcluded(repoPath,snippetList, errorSnippets, excludedSnippets);
new Notice(`${repoPath} successfully updated 🎉`);
return [errorSnippets, snippetList];
}

export async function updateSpecificSnippet(item: snippetUpdate, settings: snippetDownloaderSettings) {
let listSnippet = settings.snippetList
let excludedSnippet = settings.errorSnippet
const snippet = listSnippet.find(snippet => snippet.repo === item.repo);
const snippetsRep = snippet.snippetsContents.find(snippet => snippet.name === item.snippetPath);
if (await checkLastUpdate(snippetsRep, item.repo)) {
const successDownload = await downloadSnippet(item.repo, snippetsRep.name, this.app.vault);
if (successDownload) {
snippetsRep.lastUpdate = await grabLastCommitDate(item.repo, snippetsRep.name);
new Notice(`${basename(item.snippetPath)} has been updated 🎉`);
return [listSnippet,
excludedSnippet];
} else {
excludedSnippet =excludedSnippet + item.snippetPath.replace('.css', '') + ', ';
listSnippet = removeSnippetFromExcluded(item.repo,listSnippet, excludedSnippet, settings.excludedSnippet);
return [listSnippet,
excludedSnippet];
}
}
listSnippet = removeSnippetFromExcluded(item.repo,listSnippet, excludedSnippet, settings.excludedSnippet);
new Notice (`${basename(item.snippetPath)} is already up to date 💡`);
return [listSnippet,
excludedSnippet];
}
83 changes: 2 additions & 81 deletions Snippet-downloader/downloader.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {Octokit} from "@octokit/core";
import { Base64 } from "js-base64";

import { ResponseHeaders } from "@octokit/types";
import {normalizePath, Notice, Vault, request} from "obsidian";
import {normalizePath, Vault, request} from "obsidian";
import {
snippetDownloaderSettings,
snippetInformation,
snippetRepo
} from "./settings";
import {searchExcluded, basename} from "./utils";

Expand Down Expand Up @@ -48,8 +46,7 @@ export async function grabLastCommitDate(repoPath:string, filepath:string) {
}
}


async function listSnippetfromRepo(repoPath: string, settings: snippetDownloaderSettings): Promise<snippetInformation[]> {
export async function listSnippetfromRepo(repoPath: string, settings: snippetDownloaderSettings): Promise<snippetInformation[]> {
const octokit = new Octokit();
const repo = repoPath.replace('https://github.com/', '')
const owner = repo.split('/')[0]
Expand Down Expand Up @@ -81,88 +78,12 @@ async function listSnippetfromRepo(repoPath: string, settings: snippetDownloader
}
}


export async function addSnippet(repoPath: string, settings: snippetDownloaderSettings, vault: Vault) {
const snippetList = settings.snippetList;
let excludedSnippet = settings.errorSnippet;
const catchErrors: string[] = [];
if (!snippetList.some(snippet => snippet.repo === repoPath)) {
const newSnippetList = await listSnippetfromRepo(repoPath, settings);
if (newSnippetList.length === 0) {
new Notice('Error 😿, snippet or repository not found')
return snippetList;
}
snippetList.push({
repo: repoPath,
snippetsContents: await listSnippetfromRepo(repoPath, settings)
})
const snippet = snippetList.find(snippet => snippet.repo === repoPath)
for (const snippetContents of snippet.snippetsContents) {
const Success=await downloadSnippet(repoPath, snippetContents.name, vault)
if (!Success) {
excludedSnippet += snippetContents.name.replace('.css', '') + ', ';
catchErrors.push(snippetContents.name.replace('.css', ''));
}
}
let messageNotice = `Successfully added ${repoPath}. ${newSnippetList.length} snippets added. 🎉`
if (catchErrors.length > 0) {
messageNotice += `\n${catchErrors.length} snippets not added😿: ${catchErrors.join(', ')}`
}
new Notice(messageNotice);
return [snippetList, excludedSnippet]
}
new Notice ('Error : this repo is already in the list 😿');
return [snippetList, excludedSnippet]
}

function removeErrorSnippet(repoPath: string, errorSnippet: string, snippetList: snippetRepo[]){
const snippet = snippetList.find(snippet => snippet.repo === repoPath)
let errorSnippetList = errorSnippet.split(', ');
for (const snippetContents of snippet.snippetsContents) {
errorSnippetList=errorSnippetList.filter(v=>v!=snippetContents.name.replace('.css', '').trim());
}
console.log(errorSnippetList)
return errorSnippetList.join(', ');
}

export function removeSnippet(repoPath: string, snippetList: snippetRepo[], errorSnippet: string) {
if (snippetList.some(snippet => snippet.repo === repoPath)) {
errorSnippet = removeErrorSnippet(repoPath, errorSnippet, snippetList)
snippetList.splice(snippetList.findIndex(snippet => snippet.repo === repoPath), 1)
new Notice('Repository successfully removed 🎉');
return [snippetList, errorSnippet]
}
new Notice ('Error : this repo is not in the list 😿');
return [snippetList, errorSnippet]

}

export async function checkLastUpdate(snippetName:snippetInformation, repoPath: string) {
const oldDate = new Date (snippetName.lastUpdate);
const newDate= new Date(await grabLastCommitDate(repoPath, snippetName.name));
return (oldDate < newDate)
}

export async function updateSnippet(repoPath: string, snippetList: snippetRepo[], vault: Vault, excludedSnippets: string, errorSnippets: string) {
const snippet = snippetList.find(snippet => snippet.repo === repoPath);

if (snippet) {
for (const snippetContent of snippet.snippetsContents) {
if (await checkLastUpdate(snippetContent, repoPath) && (snippetContent.name !== 'obsidian.css') && (!searchExcluded(excludedSnippets, snippetContent.name)) && (!searchExcluded(errorSnippets, snippetContent.name)))
{
const successDownloaded=await downloadSnippet(repoPath, snippetContent.name, vault)
if (successDownloaded) {
snippetContent.lastUpdate = await grabLastCommitDate(repoPath, snippetContent.name);
} else {
errorSnippets += snippetContent.name.replace('.css', '') + ', ';
}
}
}
}
new Notice(`${repoPath} successfully updated 🎉`);
return [errorSnippets, snippetList];
}

export async function downloadSnippet(repoPath: string, snippetName: string, vault:Vault) {
const repo = repoPath.replace('https://github.com/', '')
const owner = repo.split('/')[0]
Expand Down
4 changes: 2 additions & 2 deletions Snippet-downloader/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "./settings";
import {snippetDownloaderModals} from "./modals/simpleCommands";
import {repoDownloader, specificSnippetDownloader} from "./modals/updateSnippets";
import {addSnippet, updateSnippet} from "./downloader";
import {addSnippet, updateRepo} from "./addSnippets";

export default class snippetDownloader extends Plugin {
settings: snippetDownloaderSettings;
Expand Down Expand Up @@ -41,7 +41,7 @@ export default class snippetDownloader extends Plugin {
let updatedSettings = [errorSnippet, snippetList];
for (const repoName of snippetList) {
//@ts-ignore
updatedSettings= await updateSnippet(repoName.repo, snippetList, this.app.vault, excludedSnippet, errorSnippet);
updatedSettings= await updateRepo(repoName.repo, snippetList, this.app.vault, excludedSnippet, errorSnippet);
this.settings.snippetList = <snippetRepo[]>updatedSettings[1];
this.settings.errorSnippet = <string>updatedSettings[0];
}
Expand Down
30 changes: 30 additions & 0 deletions Snippet-downloader/removeSnippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {snippetRepo} from "./settings";
import {Notice} from "obsidian";
import {searchExcluded} from "./utils";

function removeErrorSnippet(repoPath: string, errorSnippet: string, snippetList: snippetRepo[]){
const snippet = snippetList.find(snippet => snippet.repo === repoPath)
let errorSnippetList = errorSnippet.split(', ');
for (const snippetContents of snippet.snippetsContents) {
errorSnippetList=errorSnippetList.filter(v=>v!=snippetContents.name.replace('.css', '').trim());
}
console.log(errorSnippetList)
return errorSnippetList.join(', ');
}

export function removeSnippet(repoPath: string, snippetList: snippetRepo[], errorSnippet: string) {
if (snippetList.some(snippet => snippet.repo === repoPath)) {
errorSnippet = removeErrorSnippet(repoPath, errorSnippet, snippetList)
snippetList.splice(snippetList.findIndex(snippet => snippet.repo === repoPath), 1)
new Notice('Repository successfully removed 🎉');
return [snippetList, errorSnippet]
}
new Notice ('Error : this repo is not in the list 😿');
return [snippetList, errorSnippet]
}

export function removeSnippetFromExcluded(repoPath: string, snippetList: snippetRepo[], errorSnippet: string, excludedSnippet: string) {
const repo = snippetList.find(snippet => snippet.repo === repoPath)
repo.snippetsContents=repo.snippetsContents.filter(snippet=>!searchExcluded(excludedSnippet, snippet.name) && !(searchExcluded(errorSnippet, snippet.name)))
return snippetList
}
2 changes: 1 addition & 1 deletion Snippet-downloader/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ButtonComponent
} from "obsidian";
import snippetDownloader from "./main";
import {removeSnippet} from "./downloader";
import {removeSnippet} from "./removeSnippet";

export interface snippetInformation {
name: string;
Expand Down
1 change: 1 addition & 0 deletions Snippet-downloader/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function searchExcluded(excludedSnippet: string, name: string): boolean {
return true;
}
}
return false;
}

export function basename(path: string) {
Expand Down

0 comments on commit 2f88808

Please sign in to comment.