diff --git a/src/main/index.js b/src/main/index.js index f19b492..d6ec826 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -12,6 +12,7 @@ import pkg from "../../package.json" import setup from "./setup" import PkgManager from "./pkgManager" +import { readManifest } from "./pkgManager" class ElectronApp { constructor() { @@ -26,6 +27,9 @@ class ElectronApp { "get:installations": async () => { return await this.pkgManager.getInstallations() }, + "bundle:read": async (event, manifest_url) => { + return JSON.stringify(await readManifest(manifest_url)) + }, "bundle:update": (event, manifest_id) => { this.pkgManager.update(manifest_id) }, @@ -98,6 +102,27 @@ class ElectronApp { await app.whenReady() + // Set app user model id for windows + electronApp.setAppUserModelId("com.electron") + + app.on("browser-window-created", (_, window) => { + optimizer.watchWindowShortcuts(window) + }) + + this.createWindow() + + app.on("activate", () => { + if (BrowserWindow.getAllWindows().length === 0) { + this.createWindow() + } + }) + + app.on("window-all-closed", () => { + if (process.platform !== "darwin") { + app.quit() + } + }) + autoUpdater.on("update-available", (ev, info) => { console.log(info) @@ -132,27 +157,6 @@ class ElectronApp { }) autoUpdater.checkForUpdates() - - // Set app user model id for windows - electronApp.setAppUserModelId("com.electron") - - app.on("browser-window-created", (_, window) => { - optimizer.watchWindowShortcuts(window) - }) - - this.createWindow() - - app.on("activate", () => { - if (BrowserWindow.getAllWindows().length === 0) { - this.createWindow() - } - }) - - app.on("window-all-closed", () => { - if (process.platform !== "darwin") { - app.quit() - } - }) } } diff --git a/src/main/pkgManager.js b/src/main/pkgManager.js index 504a943..d31939a 100644 --- a/src/main/pkgManager.js +++ b/src/main/pkgManager.js @@ -47,7 +47,7 @@ function sendToRenderer(event, data) { global.win.webContents.send(event, serializeIpc(data)) } -async function fetchAndCreateModule(manifest) { +export async function fetchAndCreateModule(manifest) { console.log(`Fetching ${manifest}...`) try { @@ -63,7 +63,7 @@ async function fetchAndCreateModule(manifest) { } } -async function readManifest(manifest, { just_read = false } = {}) { +export async function readManifest(manifest, { just_read = false } = {}) { // check if manifest is a directory or a url const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi @@ -177,25 +177,16 @@ export default class PkgManager { } } - async install(manifest) { - let pendingTasks = [] - - manifest = await readManifest(manifest).catch((error) => { - sendToRenderer("runtime:error", "Cannot fetch this manifest") - - return false - }) - - if (!manifest) { - return false - } - + async initManifest(manifest = {}) { const packPath = path.resolve(INSTALLERS_PATH, manifest.id) + const osString = `${os.platform()}-${os.arch()}` + if (typeof manifest.init === "function") { const init_result = await manifest.init({ pack_dir: packPath, - tmp_dir: TMP_PATH + tmp_dir: TMP_PATH, + os_string: osString, }) manifest = { @@ -206,18 +197,44 @@ export default class PkgManager { delete manifest.init } - manifest.status = "installing" - - console.log(`Starting to install ${manifest.pack_name}...`) - console.log(`Installing at >`, packPath) - - sendToRenderer("new:installation", manifest) - - fs.mkdirSync(packPath, { recursive: true }) - - await this.appendInstallation(manifest) + return manifest + } + async install(manifest) { try { + let pendingTasks = [] + + manifest = await readManifest(manifest).catch((error) => { + sendToRenderer("runtime:error", "Cannot fetch this manifest") + + return false + }) + + if (!manifest) { + return false + } + + manifest = await this.initManifest(manifest) + + manifest.status = "installing" + + console.log(`Starting to install ${manifest.pack_name}...`) + console.log(`Installing at >`, packPath) + + sendToRenderer("new:installation", manifest) + + fs.mkdirSync(packPath, { recursive: true }) + + await this.appendInstallation(manifest) + + if (typeof manifest.on_install === "function") { + await manifest.on_install({ + manifest: manifest, + pack_dir: packPath, + tmp_dir: TMP_PATH, + }) + } + if (typeof manifest.git_clones_steps !== "undefined" && Array.isArray(manifest.git_clones_steps)) { for await (const step of manifest.git_clones_steps) { const _path = path.resolve(packPath, step.path) @@ -430,19 +447,7 @@ export default class PkgManager { manifest.status = "updating" - if (typeof manifest.init === "function") { - const init_result = await manifest.init({ - pack_dir: packPath, - tmp_dir: TMP_PATH - }) - - manifest = { - ...manifest, - ...init_result, - } - - delete manifest.init - } + manifest = await this.initManifest(manifest) if (typeof manifest.update === "function") { sendToRenderer(`installation:status`, { @@ -614,24 +619,12 @@ export default class PkgManager { } const packPath = path.resolve(INSTALLERS_PATH, manifest.id) - + if (manifest.remote_url) { manifest = await readManifest(manifest.remote_url, { just_read: true }) } - if (typeof manifest.init === "function") { - const init_result = await manifest.init({ - pack_dir: packPath, - tmp_dir: TMP_PATH - }) - - manifest = { - ...manifest, - ...init_result, - } - - delete manifest.init - } + manifest = await this.initManifest(manifest) if (typeof manifest.execute !== "function") { sendToRenderer("installation:status", {