Added main.js file detecting

This commit is contained in:
Henry Hiles 2022-04-28 12:48:17 -04:00
parent e8a3c7fc41
commit cc53a210ae
2 changed files with 32 additions and 15 deletions

View file

@ -1,6 +1,7 @@
const { contextBridge } = require("electron") const { contextBridge } = require("electron")
const { resolve, join } = require("path") const { resolve, join } = require("path")
const { exec } = require("child_process") const { exec } = require("child_process")
const { promises, constants } = require("fs")
const execWithPromise = async (command, options) => const execWithPromise = async (command, options) =>
new Promise(async (resolve, reject) => new Promise(async (resolve, reject) =>
@ -11,16 +12,23 @@ const execWithPromise = async (command, options) =>
contextBridge.exposeInMainWorld("installPackage", async (link) => { contextBridge.exposeInMainWorld("installPackage", async (link) => {
const packagesPath = resolve(__dirname, "..") const packagesPath = resolve(__dirname, "..")
const packagePath = join(packagesPath, link[7])
try { try {
await execWithPromise(`git clone ${link[0]}`, { await execWithPromise(`git clone ${link[0]}`, {
cwd: packagesPath, cwd: packagesPath,
}) })
await execWithPromise("pnpm i --production", { await execWithPromise("pnpm i --production", {
cwd: join(packagesPath, link[7]), cwd: packagePath,
}) })
try {
await promises.access(join(packagePath, "main.js"), constants.F_OK)
return "Main.js file detected: Please quit Discord from the system tray"
} catch (error) { } catch (error) {
console.error(error) return "Please reload discord with Ctrl+R"
return true }
} catch (error) {
return console.error(error)
} }
}) })

View file

@ -60,10 +60,29 @@ export default new (class PackageDownloader {
disabled: disabled, disabled: disabled,
onClick: async () => { onClick: async () => {
setDisabled(true) setDisabled(true)
const failed =
const reloadMessage =
await window.installPackage(gitURL) await window.installPackage(gitURL)
if (failed) { if (reloadMessage) {
Toasts.showToast(
Toasts.createToast(
`Successfully installed package! ${
reloadMessage
? `${reloadMessage}.`
: ""
}`,
Toasts.ToastType.SUCCESS
)
)
pluginLog(
`Successfully installed package! ${
reloadMessage
? `${reloadMessage}.`
: ""
}`
)
} else {
Toasts.showToast( Toasts.showToast(
Toasts.createToast( Toasts.createToast(
"Failed to install package", "Failed to install package",
@ -75,16 +94,6 @@ export default new (class PackageDownloader {
console.error console.error
) )
setDisabled(false) setDisabled(false)
} else {
Toasts.showToast(
Toasts.createToast(
"Successfully installed package! Please reload discord with Ctrl+R.",
Toasts.ToastType.SUCCESS
)
)
pluginLog(
"Successfully installed package! Please reload discord with Ctrl+R."
)
} }
}, },
}, },