2022-10-21 23:17:06 +00:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-28 12:58:14 +00:00
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
|
|
|
import { ErrorCard } from "@components/ErrorCard";
|
|
|
|
import { Flex } from "@components/Flex";
|
|
|
|
import { handleComponentFailed } from "@components/handleComponentFailed";
|
|
|
|
import { Link } from "@components/Link";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { classes, useAwaiter } from "@utils/misc";
|
|
|
|
import { changes, checkForUpdates, getRepo, isNewer, rebuild, update, updateError, UpdateLogger } from "@utils/updater";
|
|
|
|
import { Alerts, Button, Card, Forms, Margins, Parser, React, Toasts } from "@webpack/common";
|
|
|
|
|
2022-10-23 21:23:52 +00:00
|
|
|
import gitHash from "~git-hash";
|
2022-10-22 16:18:41 +00:00
|
|
|
|
2022-09-30 22:42:50 +00:00
|
|
|
function withDispatcher(dispatcher: React.Dispatch<React.SetStateAction<boolean>>, action: () => any) {
|
|
|
|
return async () => {
|
|
|
|
dispatcher(true);
|
|
|
|
try {
|
|
|
|
await action();
|
|
|
|
} catch (e: any) {
|
|
|
|
UpdateLogger.error("Failed to update", e);
|
|
|
|
if (!e) {
|
|
|
|
var err = "An unknown error occurred (error is undefined).\nPlease try again.";
|
|
|
|
} else if (e.code && e.cmd) {
|
|
|
|
const { code, path, cmd, stderr } = e;
|
|
|
|
|
|
|
|
if (code === "ENOENT")
|
|
|
|
var err = `Command \`${path}\` not found.\nPlease install it and try again`;
|
|
|
|
else {
|
|
|
|
var err = `An error occured while running \`${cmd}\`:\n`;
|
|
|
|
err += stderr || `Code \`${code}\`. See the console for more info`;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
var err = "An unknown error occurred. See the console for more info.";
|
|
|
|
}
|
|
|
|
Alerts.show({
|
|
|
|
title: "Oops!",
|
2022-10-02 00:46:41 +00:00
|
|
|
body: (
|
|
|
|
<ErrorCard>
|
|
|
|
{err.split("\n").map(line => <div>{Parser.parse(line)}</div>)}
|
|
|
|
</ErrorCard>
|
|
|
|
)
|
2022-09-30 22:42:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
dispatcher(false);
|
|
|
|
}
|
|
|
|
};
|
2022-10-08 18:36:57 +00:00
|
|
|
}
|
2022-09-30 22:42:50 +00:00
|
|
|
|
2022-10-05 15:09:37 +00:00
|
|
|
interface CommonProps {
|
|
|
|
repo: string;
|
|
|
|
repoPending: boolean;
|
|
|
|
}
|
2022-10-01 20:09:20 +00:00
|
|
|
|
2022-10-05 15:09:37 +00:00
|
|
|
function Changes({ updates, repo, repoPending }: CommonProps & { updates: typeof changes; }) {
|
2022-09-30 22:42:50 +00:00
|
|
|
return (
|
2022-10-05 15:09:37 +00:00
|
|
|
<Card style={{ padding: ".5em" }}>
|
|
|
|
{updates.map(({ hash, author, message }) => (
|
|
|
|
<div>
|
|
|
|
<Link href={`${repo}/commit/${hash}`} disabled={repoPending}>
|
|
|
|
<code>{hash}</code>
|
|
|
|
</Link>
|
|
|
|
<span style={{
|
|
|
|
marginLeft: "0.5em",
|
|
|
|
color: "var(--text-normal)"
|
|
|
|
}}>{message} - {author}</span>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
2022-09-30 22:42:50 +00:00
|
|
|
|
2022-10-05 15:09:37 +00:00
|
|
|
function Updatable(props: CommonProps) {
|
|
|
|
const [updates, setUpdates] = React.useState(changes);
|
|
|
|
const [isChecking, setIsChecking] = React.useState(false);
|
|
|
|
const [isUpdating, setIsUpdating] = React.useState(false);
|
2022-10-01 20:09:20 +00:00
|
|
|
|
2022-10-18 07:57:32 +00:00
|
|
|
const isOutdated = (updates?.length ?? 0) > 0;
|
2022-10-01 20:09:20 +00:00
|
|
|
|
2022-10-05 15:09:37 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-10-02 00:46:41 +00:00
|
|
|
{!updates && updateError ? (
|
|
|
|
<>
|
|
|
|
<Forms.FormText>Failed to check updates. Check the console for more info</Forms.FormText>
|
|
|
|
<ErrorCard style={{ padding: "1em" }}>
|
|
|
|
<p>{updateError.stderr || updateError.stdout || "An unknown error occurred"}</p>
|
|
|
|
</ErrorCard>
|
|
|
|
</>
|
2022-10-05 15:09:37 +00:00
|
|
|
) : (
|
|
|
|
<Forms.FormText className={Margins.marginBottom8}>
|
|
|
|
{isOutdated ? `There are ${updates.length} Updates` : "Up to Date!"}
|
|
|
|
</Forms.FormText>
|
2022-10-01 20:09:20 +00:00
|
|
|
)}
|
2022-09-30 22:42:50 +00:00
|
|
|
|
2022-10-05 15:09:37 +00:00
|
|
|
{isOutdated && <Changes updates={updates} {...props} />}
|
|
|
|
|
|
|
|
<Flex className={classes(Margins.marginBottom8, Margins.marginTop8)}>
|
2022-10-01 20:09:20 +00:00
|
|
|
{isOutdated && <Button
|
2022-09-30 22:42:50 +00:00
|
|
|
size={Button.Sizes.SMALL}
|
|
|
|
disabled={isUpdating || isChecking}
|
|
|
|
onClick={withDispatcher(setIsUpdating, async () => {
|
|
|
|
if (await update()) {
|
2022-10-01 20:14:57 +00:00
|
|
|
setUpdates([]);
|
2022-09-30 22:42:50 +00:00
|
|
|
const needFullRestart = await rebuild();
|
|
|
|
await new Promise<void>(r => {
|
|
|
|
Alerts.show({
|
|
|
|
title: "Update Success!",
|
|
|
|
body: "Successfully updated. Restart now to apply the changes?",
|
|
|
|
confirmText: "Restart",
|
|
|
|
cancelText: "Not now!",
|
|
|
|
onConfirm() {
|
|
|
|
if (needFullRestart)
|
|
|
|
window.DiscordNative.app.relaunch();
|
|
|
|
else
|
|
|
|
location.reload();
|
|
|
|
r();
|
|
|
|
},
|
|
|
|
onCancel: r
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
>
|
2022-10-01 20:09:20 +00:00
|
|
|
Update Now
|
|
|
|
</Button>}
|
2022-09-30 22:42:50 +00:00
|
|
|
<Button
|
|
|
|
size={Button.Sizes.SMALL}
|
|
|
|
disabled={isUpdating || isChecking}
|
|
|
|
onClick={withDispatcher(setIsChecking, async () => {
|
2022-10-01 20:09:20 +00:00
|
|
|
const outdated = await checkForUpdates();
|
|
|
|
if (outdated) {
|
2022-09-30 22:42:50 +00:00
|
|
|
setUpdates(changes);
|
|
|
|
} else {
|
2022-10-01 20:14:57 +00:00
|
|
|
setUpdates([]);
|
2022-10-01 20:09:20 +00:00
|
|
|
Toasts.show({
|
|
|
|
message: "No updates found!",
|
|
|
|
id: Toasts.genId(),
|
|
|
|
type: Toasts.Type.MESSAGE,
|
|
|
|
options: {
|
|
|
|
position: Toasts.Position.BOTTOM
|
|
|
|
}
|
|
|
|
});
|
2022-09-30 22:42:50 +00:00
|
|
|
}
|
|
|
|
})}
|
|
|
|
>
|
2022-10-01 20:09:20 +00:00
|
|
|
Check for Updates
|
2022-09-30 22:42:50 +00:00
|
|
|
</Button>
|
|
|
|
</Flex>
|
2022-10-05 15:09:37 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Newer(props: CommonProps) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Forms.FormText className={Margins.marginBottom8}>
|
|
|
|
Your local copy has more recent commits. Please stash or reset them.
|
|
|
|
</Forms.FormText>
|
|
|
|
<Changes {...props} updates={changes} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-16 15:15:15 +00:00
|
|
|
function Updater() {
|
2022-10-05 15:09:37 +00:00
|
|
|
const [repo, err, repoPending] = useAwaiter(getRepo, "Loading...");
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (err)
|
|
|
|
UpdateLogger.error("Failed to retrieve repo", err);
|
|
|
|
}, [err]);
|
|
|
|
|
|
|
|
const commonProps: CommonProps = {
|
|
|
|
repo,
|
|
|
|
repoPending
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-11-25 22:38:55 +00:00
|
|
|
<Forms.FormSection>
|
2022-10-05 15:09:37 +00:00
|
|
|
<Forms.FormTitle tag="h5">Repo</Forms.FormTitle>
|
|
|
|
|
|
|
|
<Forms.FormText>{repoPending ? repo : err ? "Failed to retrieve - check console" : (
|
|
|
|
<Link href={repo}>
|
|
|
|
{repo.split("/").slice(-2).join("/")}
|
|
|
|
</Link>
|
|
|
|
)} ({gitHash})</Forms.FormText>
|
|
|
|
|
|
|
|
<Forms.FormDivider />
|
|
|
|
|
|
|
|
<Forms.FormTitle tag="h5">Updates</Forms.FormTitle>
|
|
|
|
|
|
|
|
{isNewer ? <Newer {...commonProps} /> : <Updatable {...commonProps} />}
|
2022-10-02 00:46:41 +00:00
|
|
|
</Forms.FormSection >
|
2022-09-30 22:42:50 +00:00
|
|
|
);
|
2022-10-16 15:15:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 16:29:31 +00:00
|
|
|
export default IS_WEB ? null : ErrorBoundary.wrap(Updater, {
|
|
|
|
message: "Failed to render the Updater. If this persists, try using the installer to reinstall!",
|
|
|
|
onError: handleComponentFailed,
|
|
|
|
});
|