Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow installation via git #574

Open
wants to merge 7 commits into
base: v4.8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"eslint:fix": "eslint ./src ./scripts ./bin --fix",
"lint": "pnpm run prettier:check && pnpm run eslint:check && pnpm run cspell:check && pnpm run typescript:check",
"lint:fix": "pnpm run prettier:fix && pnpm run eslint:fix && pnpm run cspell:check && pnpm run typescript:check",
"build:types": "rm -rf dist; tsc --declaration --emitDeclarationOnly --noEmit false -p tsconfig.json --outDir dist; rm -rf dist/scripts; mv dist/src/* dist; rm -rf dist/src; cp src/*.d.ts dist; tsx scripts/create-import-wrappers.mts",
"postinstall": "tsx scripts/build-bin.mts",
"prepublishOnly": "rm -rf dist; tsc --declaration --emitDeclarationOnly --noEmit false -p tsconfig.json --outDir dist; rm -rf dist/scripts; mv dist/src/* dist; rm -rf dist/src; cp src/*.d.ts dist; tsx scripts/create-import-wrappers.mts",
"prepare": "[ ! -e src ] && exit 0 || pnpm run build:types",
"postpublish": "rm -rf *.d.ts; rm -rf dist; npm run build",
"docs": "typedoc src/renderer/replugged.ts --excludeExternals",
"docs:watch": "pnpm run docs --watch"
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/badges/badge.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Messages } from "@common/i18n";
import React from "@common/react";
import React, { _ } from "@common/react";
import { Clickable, Tooltip } from "@components";
import { getByProps } from "../../modules/webpack";
import { goToOrJoinServer, openExternal } from "../../util";
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/badges/badges/Booster.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "@common/react";
import React, { _ } from "@common/react";

export default React.memo((props) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/badges/badges/BugHunter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "@common/react";
import React, { _ } from "@common/react";

export default React.memo((props) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/badges/badges/Contributor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "@common/react";
import React, { _ } from "@common/react";

export default React.memo((props) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/badges/badges/Developer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "@common/react";
import React, { _ } from "@common/react";

export default React.memo((props) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/badges/badges/EarlyUser.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "@common/react";
import React, { _ } from "@common/react";

export default React.memo((props) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/badges/badges/Staff.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "@common/react";
import React, { _ } from "@common/react";

export default React.memo((props) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/badges/badges/Support.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "@common/react";
import React, { _ } from "@common/react";

export default React.memo((props) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/coremods/badges/badges/Translator.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "@common/react";
import React, { _ } from "@common/react";

export default React.memo((props) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
Expand Down
1 change: 1 addition & 0 deletions src/renderer/coremods/badges/badges/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type _ from "react";
import Booster from "./Booster";
import BugHunter from "./BugHunter";
import Contributor from "./Contributor";
Expand Down
5 changes: 2 additions & 3 deletions src/renderer/coremods/installer/AddonEmbed.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Messages } from "@common/i18n";
import { React } from "@common";
import React, { _ } from "@common/react";
import { Button, Clickable, Text, Tooltip } from "@components";
import { Logger } from "@replugged";
import { getByProps } from "@webpack";
import { openExternal } from "src/renderer/util";
import { CheckResultSuccess } from "src/types";
import { InstallLinkProps } from ".";
import { getSourceLink } from "../settings/pages";
import { authorList, checkIsInstalled, getInfo, install } from "./util";
import { InstallLinkProps, authorList, checkIsInstalled, getInfo, install } from "./util";

import "./addonEmbed.css";

Expand Down
83 changes: 83 additions & 0 deletions src/renderer/coremods/installer/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Injector } from "@replugged";
import { ApplicationCommandOptionType } from "src/types";
import { INSTALLER_SOURCES, InstallerSource, installFlow, parseInstallLink } from "./util";
import { Messages } from "@common/i18n";

/**
* A map of display names for installer sources.
*/
const sourceDisplayNames: Record<InstallerSource, string> = {
github: "GitHub",
store: Messages.REPLUGGED_STORE,
};

export function loadCommands(injector: Injector): void {
injector.utils.registerSlashCommand({
name: "install",
displayName: Messages.REPLUGGED_COMMAND_INSTALL_NAME,
description: Messages.REPLUGGED_COMMAND_INSTALL_DESC,
options: [
{
name: "addon",
displayName: Messages.REPLUGGED_COMMAND_INSTALL_OPTION_ADDON_NAME,
description: Messages.REPLUGGED_COMMAND_INSTALL_OPTION_ADDON_DESC,
type: ApplicationCommandOptionType.String,
required: true,
},
{
name: "source",
displayName: Messages.REPLUGGED_COMMAND_INSTALL_OPTION_SOURCE_NAME,
description: Messages.REPLUGGED_COMMAND_INSTALL_OPTION_SOURCE_DESC,
type: ApplicationCommandOptionType.String,
required: false,
choices: INSTALLER_SOURCES.map((v) => ({
name: v,
displayName: sourceDisplayNames[v],
value: v,
})),
},
{
name: "id",
displayName: Messages.REPLUGGED_COMMAND_INSTALL_OPTION_ID_NAME,
description: Messages.REPLUGGED_COMMAND_INSTALL_OPTION_ID_DESC,
type: ApplicationCommandOptionType.String,
required: false,
},
],

async executor(i) {
let addon = i.getValue("addon");
let source = i.getValue("source");
let id = i.getValue("id");

const linkParsed = parseInstallLink(addon);

if (linkParsed) {
({ identifier: addon, source, id } = linkParsed);
}

const resp = await installFlow(addon, source, id, false);

switch (resp.kind) {
case "FAILED":
return {
result: Messages.REPLUGGED_TOAST_INSTALLER_ADDON_FETCH_INFO_FAILED,
};
case "ALREADY_INSTALLED":
return {
result: Messages.REPLUGGED_ERROR_ALREADY_INSTALLED.format({ name: resp.manifest.name }),
};
case "CANCELLED":
return {
result: Messages.REPLUGGED_TOAST_INSTALLER_ADDON_CANCELED_INSTALL,
};
case "SUCCESS":
return {
result: Messages.REPLUGGED_TOAST_INSTALLER_ADDON_INSTALL_SUCCESS.format({
name: resp.manifest.name,
}),
};
}
},
});
}
55 changes: 9 additions & 46 deletions src/renderer/coremods/installer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { Injector, Logger } from "@replugged";
import { filters, getFunctionKeyBySource, waitForModule } from "src/renderer/modules/webpack";
import { ObjectExports } from "src/types";
import { registerRPCCommand } from "../rpc";
import { InstallResponse, InstallerSource, installFlow, isValidSource } from "./util";
import {
InstallLinkProps,
InstallResponse,
InstallerSource,
installFlow,
parseInstallLink,
} from "./util";
import { plugins } from "src/renderer/managers/plugins";
import { themes } from "src/renderer/managers/themes";
import AddonEmbed from "./AddonEmbed";
import { generalSettings } from "../settings/pages";
import type { Capture, DefaultInRule } from "simple-markdown";
import { parser } from "@common";
import { loadCommands } from "./commands";

const injector = new Injector();
const logger = Logger.coremod("Installer");
Expand All @@ -18,51 +25,6 @@ interface AnchorProps extends React.ComponentPropsWithoutRef<"a"> {
focusProps?: Record<string, unknown>;
}

export interface InstallLinkProps {
/** Identifier for the addon in the source */
identifier: string;
/** Updater source type */
source?: InstallerSource;
/** ID for the addon in that source. Useful for GitHub repositories that have multiple addons. */
id?: string;
}

function parseInstallLink(href: string): InstallLinkProps | null {
try {
const url = new URL(href);
const repluggedHostname = new URL(generalSettings.get("apiUrl")).hostname;
if (url.hostname !== repluggedHostname) return null;

if (url.pathname === "/install") {
const params = url.searchParams;
const identifier = params.get("identifier");
const source = params.get("source") ?? "store";
const id = params.get("id") ?? undefined;
if (!identifier) return null;
if (!isValidSource(source)) return null;
return {
identifier,
source,
id,
};
}

const storeMatch = url.pathname.match(/^\/store\/([^/]+)$/);
if (storeMatch) {
const identifier = storeMatch[1];
if (["plugins", "themes"].includes(identifier.toLowerCase())) return null;
return {
identifier,
source: "store",
};
}

return null;
} catch {
return null;
}
}

let uninjectFns: Array<() => void> = [];

const modalFlows = new Map<string, Promise<InstallResponse>>();
Expand Down Expand Up @@ -202,6 +164,7 @@ async function injectLinks(): Promise<void> {
export async function start(): Promise<void> {
await injectLinks();
injectRpc();
loadCommands(injector);
}

export function stop(): void {
Expand Down
49 changes: 47 additions & 2 deletions src/renderer/coremods/installer/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { openExternal } from "src/renderer/util";
import type { AnyAddonManifest, CheckResultSuccess } from "src/types";
import * as pluginManager from "../../managers/plugins";
import * as themeManager from "../../managers/themes";
import { getAddonType, getSourceLink, label } from "../settings/pages";
import { generalSettings, getAddonType, getSourceLink, label } from "../settings/pages";

const logger = Logger.coremod("Installer");

// First item is the default
const INSTALLER_SOURCES = ["store", "github"] as const;
export const INSTALLER_SOURCES = ["store", "github"] as const;
export type InstallerSource = (typeof INSTALLER_SOURCES)[number];
const DEFAULT_INSTALLER_SOURCE: InstallerSource = "store";

Expand All @@ -25,6 +25,51 @@ export function isValidSource(type: string): type is InstallerSource {
return INSTALLER_SOURCES.includes(type);
}

export interface InstallLinkProps {
/** Identifier for the addon in the source */
identifier: string;
/** Updater source type */
source?: InstallerSource;
/** ID for the addon in that source. Useful for GitHub repositories that have multiple addons. */
id?: string;
}

export function parseInstallLink(href: string): InstallLinkProps | null {
try {
const url = new URL(href);
const repluggedHostname = new URL(generalSettings.get("apiUrl")).hostname;
if (url.hostname !== repluggedHostname) return null;

if (url.pathname === "/install") {
const params = url.searchParams;
const identifier = params.get("identifier");
const source = params.get("source") ?? "store";
const id = params.get("id") ?? undefined;
if (!identifier) return null;
if (!isValidSource(source)) return null;
return {
identifier,
source,
id,
};
}

const storeMatch = url.pathname.match(/^\/store\/([^/]+)$/);
if (storeMatch) {
const identifier = storeMatch[1];
if (["plugins", "themes"].includes(identifier.toLowerCase())) return null;
return {
identifier,
source: "store",
};
}

return null;
} catch {
return null;
}
}

export async function getInfo(
identifier: string,
source?: InstallerSource,
Expand Down
1 change: 1 addition & 0 deletions src/renderer/coremods/settings/icons/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from "react";
import Discord from "./Discord";
import GitHub from "./GitHub";
import Link from "./Link";
Expand Down
1 change: 1 addition & 0 deletions src/renderer/modules/common/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import type React from "react";
import { waitForProps } from "../webpack";

export default await waitForProps<typeof React>("createElement", "useState");
export type { React as _ };
Loading