This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters