Skip to content

Commit

Permalink
Add update option to Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiNtD committed Oct 18, 2024
1 parent e23175f commit 792404a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
Binary file modified bun.lockb
Binary file not shown.
45 changes: 45 additions & 0 deletions src/EnterPrompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
createPrompt,
useState,
useKeypress,
isEnterKey,
usePrefix,
makeTheme,
type Theme,
type Status,
} from "@inquirer/core";
import type { PartialDeep } from "@inquirer/type";
import chalk from "chalk";
import figures from "@inquirer/figures";

type ConfirmConfig = {
message?: string;
theme?: PartialDeep<Theme>;
};

export default createPrompt<boolean, ConfirmConfig>((config, done) => {
const [status, setStatus] = useState<Status>("idle");
const theme = makeTheme(
{
prefix: {
idle: chalk.blue(figures.info),
done: chalk.green(figures.tick),
},
},
config.theme,
);
const prefix = usePrefix({ status, theme });

useKeypress((key) => {
if (isEnterKey(key)) {
setStatus("done");
done(true);
}
});

const message = theme.style.message(
config.message ?? "Press Enter to continue...",
status,
);
return `${prefix} ${message}`;
});
61 changes: 57 additions & 4 deletions src/manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import chalk from "chalk";
import { StartupRun } from "startup-run";
import { select } from "@inquirer/prompts";
import { select, confirm } from "@inquirer/prompts";
import fs from "fs-extra";
import { $ } from "bun";
import EnterPrompt from "./EnterPrompt";

const run = StartupRun.create("Scrobble Rich Presence", {
command: "bun",
Expand All @@ -22,27 +25,77 @@ while (true) {
{
name: "Enable",
value: "enable",
description: "Enable run on startup and start the background program",
description: "Enable run on startup and start the background process",
},
{
name: "Disable",
value: "disable",
description: "Disable run on startup and stop the background program",
description: "Disable run on startup and stop the background process",
},
{
name: "Update",
value: "update",
description: "Updates the program and restarts it if running",
},
{ name: "Quit", value: "quit" },
],
});

console.clear();
switch (choice) {
case "enable":
await run.enable();
await run.start();
break;
case "disable":
await run.disable();
await run.stop();
await run.disable();
break;
case "update":
if (!Bun.which("git")) {
console.log(chalk.red("Git not found"));
await pause();
break;
}
if (!(await fs.exists(".git"))) {
console.log(chalk.red("Not in a Git repository"));
console.log(
"Please clone the GitHub repo instead of using Download ZIP",
);
await pause();
break;
}

if (!(await confirm({ message: "Are you sure you want to update?" })))
break;

await run.stop();

try {
console.log(chalk.gray("Updating..."));
await $`git pull`;
if (await fs.exists("node_modules")) {
console.log(chalk.gray("Installing dependencies..."));
await $`bun install`;
}
if (await run.isEnabled()) {
console.log(chalk.gray("Restarting process..."));
await run.start();
}
console.log(chalk.bold.green("Updated!"), "Manager will now close");
await pause();
process.exit();
} catch (e) {
console.log(chalk.bold.red("Failed to update"));
console.log(e);
await pause();
break;
}
case "quit":
process.exit();
}
}

async function pause() {
await EnterPrompt({});
}

0 comments on commit 792404a

Please sign in to comment.