Skip to content

Commit

Permalink
Merge pull request #1 from mwoz123/fetch-diff-version
Browse files Browse the repository at this point in the history
Fetch diff version
  • Loading branch information
mwoz123 committed Oct 29, 2023
2 parents e275a54 + f709a98 commit 2e80d5c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
81 changes: 80 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin, MarkdownView, Editor } from "obsidian";
import { Plugin, MarkdownView, Editor, App, Modal, Setting } from "obsidian";

export default class SwissArmyKnifePlugin extends Plugin {
async onload() {
Expand All @@ -12,6 +12,11 @@ export default class SwissArmyKnifePlugin extends Plugin {
name: "Remove empty lines",
editorCallback: (editor: Editor, view: MarkdownView) => this.removeEmptyLines(editor),
});
this.addCommand({
id: "fetch-plugin-version",
name: "Fetch different plugin version",
editorCallback: () => this.fetchPluginPreviousRelease(this.app),
});
}

replaceDoubledEmptyLinesWithSingle(editor: Editor) {
Expand All @@ -24,12 +29,86 @@ export default class SwissArmyKnifePlugin extends Plugin {
return replaceRegexInFile(editor, emptyLinesWithOptionalWhitespacesRegex, '\n');
}

async fetchPluginPreviousRelease(app: App) {
new SwissModal(app,(url, version) => fetchPluginPrevRelease(url, app, version)).open();
}
}


function replaceRegexInFile(editor: Editor, pattern: RegExp | string, replacement: string) {
const currentText = editor.getValue();
const updatedText = currentText.replace(pattern, replacement)
editor.setValue(updatedText);
}


async function fetchPluginPrevRelease(ghRepoUrl:string, app: App, version = 'latest', ){

const urlForGivenVersion = ghRepoUrl + "/releases/" + version;
const { ok, url } = await fetch(urlForGivenVersion);
if (!ok)
throw new Error("Invalid url: "+ urlForGivenVersion) ;

const isValidRedirectUrl = url.includes('/releases/tag')
if(!isValidRedirectUrl)
throw new Error("Redirect url is not valid " + url);

const fetchUrl = url.replace('/releases/tag/', '/releases/download/');

const toBeFetched = ['main.js', 'manifest.json', 'styles.css']
const fetchedElements = await Promise.all(toBeFetched.map(async e=> ([e, await (await fetch(fetchUrl + '/' + e)).text()])));
const existingElements = fetchedElements.filter(([file, content]) => !content.includes("Not Found"))

const urlParts = url.split("/")
const pluginName = urlParts[4];
const pluginsPath = '.obsidian/plugins/'
const fullPluginPath = pluginsPath + pluginName
app.vault.createFolder(fullPluginPath);

existingElements.map(([filename, content ])=> {
app.vault.create(fullPluginPath + "/"+ filename, content)
})
}


export class SwissModal extends Modal {
result: string;
version: string;
onSubmit: (result: string, version: string) => void;

constructor(app: App, onSubmit: (result: string, version: string) => void) {
super(app);
this.onSubmit = onSubmit;
}

onOpen() {
const { contentEl } = this;
contentEl.createEl("h1", { text: "GH repo url" });
new Setting(contentEl)
.setName("url")
.addText((text) =>
text.onChange((value) => {
this.result = value
}));
new Setting(contentEl)
.setName("version")
.addText((text) => {
text.setValue('latest')
text.onChange((ver) => {
this.version = ver
})});
new Setting(contentEl)
.addButton((btn) =>
btn.setButtonText("Process")
.setCta()
.onClick(() => {
this.close();
this.onSubmit(this.result, this.version);
}));
}

onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "swiss-army-knife",
"name": "Swiss army knife",
"version": "0.1.0",
"version": "0.5.0",
"minAppVersion": "0.15.0",
"description": "Collection of various utilities (e.g. duplicate empty line remover)",
"author": "mwoz123",
Expand Down

0 comments on commit 2e80d5c

Please sign in to comment.