From 9a83a6a0123ad5282503e39c588c2f192e22fc86 Mon Sep 17 00:00:00 2001 From: nin0dev Date: Fri, 1 Nov 2024 13:10:56 -0400 Subject: [PATCH] cloning --- UserpluginInstallButton.tsx | 19 ++++++++++++++++ index.tsx | 43 +++++++++++++++++++++---------------- native.ts | 15 +++++++++++++ 3 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 UserpluginInstallButton.tsx create mode 100644 native.ts diff --git a/UserpluginInstallButton.tsx b/UserpluginInstallButton.tsx new file mode 100644 index 0000000..0bb50c4 --- /dev/null +++ b/UserpluginInstallButton.tsx @@ -0,0 +1,19 @@ +import { Button, ChannelStore } from "@webpack/common"; +import { Message } from "discord-types/general"; +import { CLONE_LINK_REGEX, clonePlugin } from "."; + +const WHITELISTED_SHARE_CHANNELS = ["1256395889354997771", "1032200195582197831", "1301947896601509900"]; + +export default function UserpluginInstallButton({ props }: any) { + const message: Message = props.message; + if (!WHITELISTED_SHARE_CHANNELS.includes(ChannelStore.getChannel(message.channel_id).parent_id) && !WHITELISTED_SHARE_CHANNELS.includes(message.channel_id)) + return; + const gitLink = (props.message.content as string).match(CLONE_LINK_REGEX); + if (!gitLink) return; + return ; +} diff --git a/index.tsx b/index.tsx index c15f8f8..701d409 100644 --- a/index.tsx +++ b/index.tsx @@ -1,27 +1,34 @@ import { Notices } from "@api/index"; import { addAccessory } from "@api/MessageAccessories"; import { Devs } from "@utils/constants"; -import definePlugin from "@utils/types"; -import { Button, ChannelStore, Forms } from "@webpack/common"; +import definePlugin, { PluginNative } from "@utils/types"; +import { Button, ChannelStore, Forms, Toasts } from "@webpack/common"; import { Message } from "discord-types/general"; +import { clone } from "lodash"; +import UserpluginInstallButton from "./UserpluginInstallButton"; -const WHITELISTED_SHARE_CHANNELS = ["1256395889354997771", "1032200195582197831"]; -const CLONE_LINK_REGEX = /(https:\/\/(?:git(?:hub|lab)\.com|git\.(?:[a-zA-Z0-9]|\.)+|codeberg\.org)\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+(?:\.git)?(?:\/)?)/; +export const CLONE_LINK_REGEX = /https:\/\/(?:git(?:hub|lab)\.com|git\.(?:[a-zA-Z0-9]|\.)+|codeberg\.org)\/(?!user-attachments)(?:[a-zA-Z0-9]|-)+\/((?:[a-zA-Z0-9]|-)+)(?:\.git)?(?:\/)?/; -function UserpluginInstallButton({ props }: any) { - const message: Message = props.message; - if (!WHITELISTED_SHARE_CHANNELS.includes(ChannelStore.getChannel(message.channel_id).parent_id) && !WHITELISTED_SHARE_CHANNELS.includes(message.channel_id)) - return; - const gitLink = (props.message.content as string).match(CLONE_LINK_REGEX); - if (!gitLink) return; - return ; +// @ts-ignore +const Native = VencordNative.pluginHelpers.UserpluginInstaller as PluginNative; + +export async function clonePlugin(gitLink: RegExpMatchArray) { + Toasts.show({ + message: "Cloning plugin...", + id: Toasts.genId(), + type: Toasts.Type.MESSAGE + }); + try { + const clonedRepo = await Native.cloneRepo(gitLink[0], `${VesktopNative.fileManager.getVencordDir().replace("\\", "/")}/../src/userplugins/${gitLink[1]}`); + } + catch (e) { + Toasts.pop(); + return Toasts.show({ + message: "Something bad has happened while cloning the plugin, try again later and make sure that the plugin link is valid.", + id: Toasts.genId(), + type: Toasts.Type.FAILURE + }); + } } export default definePlugin({ diff --git a/native.ts b/native.ts new file mode 100644 index 0000000..af6de52 --- /dev/null +++ b/native.ts @@ -0,0 +1,15 @@ +import { exec, spawn } from "child_process"; +import { IpcMainInvokeEvent } from "electron"; + +export async function cloneRepo(_: IpcMainInvokeEvent, repo: string, clonePath: string): Promise { + return new Promise((resolve, reject) => { + exec(`git clone ${repo} ${clonePath}`, (error, stdout, stderr) => { + if (error) { + return reject( + stderr + ); + } + resolve(null); + }); + }); +}