From b626740b8aa51615c9da569f4f019d05799da361 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Sun, 28 Jan 2024 20:09:58 +0100 Subject: [PATCH] improve install scripts --- src/main/pkg_mng/commands/install.js | 12 +++-- src/main/pkg_mng/commands/uninstall.js | 6 +++ src/main/pkg_mng/commands/update.js | 16 +++--- .../installs_steps_methods/git_clone.js | 50 ++++++++++++++++--- .../installs_steps_methods/git_pull.js | 6 ++- .../installs_steps_methods/git_reset.js | 7 ++- .../pkg_mng/installs_steps_methods/http.js | 4 +- .../pkg_mng/installs_steps_methods/index.js | 15 +++++- 8 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/main/pkg_mng/commands/install.js b/src/main/pkg_mng/commands/install.js index d740ca9..f66512e 100644 --- a/src/main/pkg_mng/commands/install.js +++ b/src/main/pkg_mng/commands/install.js @@ -39,7 +39,11 @@ export default async function install(manifest) { try { pkg = await initManifest(pkg) - fs.mkdirSync(pkg.install_path, { recursive: true }) + if (fs.existsSync(pkg.install_path)) { + await fs.rmSync(pkg.install_path, { recursive: true }) + } + + await fs.mkdirSync(pkg.install_path, { recursive: true }) // append to db await updateInstalledPackage(pkg) @@ -47,7 +51,7 @@ export default async function install(manifest) { if (typeof pkg.before_install === "function") { sendToRender(`pkg:update:status`, { id: pkg_id, - status: "loading", + status: "installing", statusText: `Performing before_install hook...`, }) @@ -59,7 +63,7 @@ export default async function install(manifest) { sendToRender(`pkg:update:status`, { id: pkg_id, - status: "loading", + status: "installing", statusText: `Performing install steps...`, }) @@ -69,7 +73,7 @@ export default async function install(manifest) { if (typeof pkg.after_install === "function") { sendToRender(`pkg:update:status`, { id: pkg_id, - status: "loading", + status: "installing", statusText: `Performing after_install hook...`, }) diff --git a/src/main/pkg_mng/commands/uninstall.js b/src/main/pkg_mng/commands/uninstall.js index 2f5213f..e092ce1 100644 --- a/src/main/pkg_mng/commands/uninstall.js +++ b/src/main/pkg_mng/commands/uninstall.js @@ -41,6 +41,12 @@ export default async function uninstall(pkg_id) { await rimraf(pkg.install_path) + sendToRender("pkg:update:status", { + id: pkg_id, + status: "uninstalling", + statusText: null, + }) + sendToRender("pkg:remove", { id: pkg_id }) diff --git a/src/main/pkg_mng/commands/update.js b/src/main/pkg_mng/commands/update.js index 3ba0a97..51f757e 100644 --- a/src/main/pkg_mng/commands/update.js +++ b/src/main/pkg_mng/commands/update.js @@ -10,15 +10,15 @@ import sendToRender from "../../utils/sendToRender" import processGenericSteps from "../installs_steps_methods" export default async function update(pkg_id) { + // find package manifest + let pkg = await getInstalledPackages(pkg_id) + + if (!pkg) { + sendToRender("runtime:error", "Package not found") + return false + } + try { - // find package manifest - let pkg = await getInstalledPackages(pkg_id) - - if (!pkg) { - sendToRender("runtime:error", "Package not found") - return false - } - // output to logs console.log(`[${pkg_id}] update() | Updating to latest version...`) diff --git a/src/main/pkg_mng/installs_steps_methods/git_clone.js b/src/main/pkg_mng/installs_steps_methods/git_clone.js index 038319b..994ea54 100644 --- a/src/main/pkg_mng/installs_steps_methods/git_clone.js +++ b/src/main/pkg_mng/installs_steps_methods/git_clone.js @@ -1,11 +1,25 @@ import path from "node:path" import fs from "node:fs" +import os from "node:os" import ChildProcess from "node:child_process" import sendToRender from "../../utils/sendToRender" +import Vars from "../../vars" + +const gitCMD = fs.existsSync(Vars.git_path) ? `${Vars.git_path}` : "git" + export default async (manifest, step) => { - const _path = path.resolve(manifest.install_path, step.path) + const tmp_path = path.resolve(os.tmpdir(), `rsb_${manifest.id}_clone_${new Date().getTime()}`) + const final_path = path.resolve(manifest.install_path, step.path) + + if (fs.existsSync(tmp_path)) { + fs.rmdirSync(tmp_path, { recursive: true }) + } + + if (!fs.existsSync(final_path)) { + fs.mkdirSync(final_path, { recursive: true }) + } sendToRender(`pkg:update:status`, { id: manifest.id, @@ -14,14 +28,34 @@ export default async (manifest, step) => { console.log(`[${manifest.id}] steps.git_clone() | Cloning ${step.url}...`) - fs.mkdirSync(_path, { recursive: true }) + const command = `${gitCMD} clone --recurse-submodules --remote-submodules ${step.url} ${tmp_path}` + + fs.mkdirSync(final_path, { recursive: true }) await new Promise((resolve, reject) => { - const process = ChildProcess.exec(`${global.GIT_PATH ?? "git"} clone --recurse-submodules --remote-submodules ${step.url} ${_path}`, { - shell: true, - }) - - process.on("exit", resolve) - process.on("error", reject) + ChildProcess.exec( + command, + { + shell: true, + }, + (error, out) => { + if (error) { + console.error(error) + reject(error) + } else { + console.log(out) + resolve() + } + } + ) }) + + // move tmp_path to final_path + await fs.promises.rename(tmp_path, final_path) + + if (fs.existsSync(tmp_path)) { + fs.rmdirSync(tmp_path, { recursive: true }) + } + + return manifest } \ No newline at end of file diff --git a/src/main/pkg_mng/installs_steps_methods/git_pull.js b/src/main/pkg_mng/installs_steps_methods/git_pull.js index 5d9e373..eb406f4 100644 --- a/src/main/pkg_mng/installs_steps_methods/git_pull.js +++ b/src/main/pkg_mng/installs_steps_methods/git_pull.js @@ -4,6 +4,10 @@ import ChildProcess from "node:child_process" import sendToRender from "../../utils/sendToRender" +import Vars from "../../vars" + +const gitCMD = fs.existsSync(Vars.git_path) ? `${Vars.git_path}` : "git" + export default async (manifest, step) => { const _path = path.resolve(manifest.install_path, step.path) @@ -17,7 +21,7 @@ export default async (manifest, step) => { fs.mkdirSync(_path, { recursive: true }) await new Promise((resolve, reject) => { - const process = ChildProcess.exec(`${global.GIT_PATH ?? "git"} pull`, { + const process = ChildProcess.exec(`${gitCMD} pull`, { cwd: _path, shell: true, }) diff --git a/src/main/pkg_mng/installs_steps_methods/git_reset.js b/src/main/pkg_mng/installs_steps_methods/git_reset.js index 39d6b4c..d73fcb3 100644 --- a/src/main/pkg_mng/installs_steps_methods/git_reset.js +++ b/src/main/pkg_mng/installs_steps_methods/git_reset.js @@ -1,10 +1,13 @@ - import path from "node:path" import fs from "node:fs" import ChildProcess from "node:child_process" import sendToRender from "../../utils/sendToRender" +import Vars from "../../vars" + +const gitCMD = fs.existsSync(Vars.git_path) ? `${Vars.git_path}` : "git" + export default async (manifest, step) => { const _path = path.resolve(manifest.install_path, step.path) const from = step.from ?? "origin/main" @@ -19,7 +22,7 @@ export default async (manifest, step) => { fs.mkdirSync(_path, { recursive: true }) await new Promise((resolve, reject) => { - const process = ChildProcess.exec(`${global.GIT_PATH ?? "git"} reset --hard ${from}`, { + const process = ChildProcess.exec(`${gitCMD} reset --hard ${from}`, { cwd: _path, shell: true, }) diff --git a/src/main/pkg_mng/installs_steps_methods/http.js b/src/main/pkg_mng/installs_steps_methods/http.js index 7c5cfb2..475be91 100644 --- a/src/main/pkg_mng/installs_steps_methods/http.js +++ b/src/main/pkg_mng/installs_steps_methods/http.js @@ -1,5 +1,7 @@ import path from "node:path" import fs from "node:fs" +import os from "node:os" + import { pipeline as streamPipeline } from "node:stream/promises" import humanFormat from "human-format" @@ -26,7 +28,7 @@ export default async (manifest, step) => { console.log(`[${manifest.id}] steps.http() | Downloading ${step.url} to ${_path}`) if (step.tmp) { - _path = path.resolve(TMP_PATH, String(new Date().getTime())) + _path = path.resolve(os.tmpdir(), String(new Date().getTime()), path.basename(step.url)) } fs.mkdirSync(path.resolve(_path, ".."), { recursive: true }) diff --git a/src/main/pkg_mng/installs_steps_methods/index.js b/src/main/pkg_mng/installs_steps_methods/index.js index 77264bd..ffc6334 100644 --- a/src/main/pkg_mng/installs_steps_methods/index.js +++ b/src/main/pkg_mng/installs_steps_methods/index.js @@ -12,10 +12,23 @@ const InstallationStepsMethods = { git_reset: ISM_GIT_RESET, } +const StepsOrders = [ + "git_clones", + "git_clones_steps", + "git_pulls", + "git_update", + "git_pulls_steps", + "git_reset", + "drive_downloads", + "http_downloads", +] + export default async function processGenericSteps(pkg, steps) { console.log(`[${pkg.id}] steps() | Processing steps...`, steps) - for await (const [stepKey, stepValue] of Object.entries(steps)) { + const stepsEntries = Object.entries(steps) + + for await (const [stepKey, stepValue] of stepsEntries) { switch (stepKey) { case "drive_downloads": { for await (const dl_step of stepValue) {