fix patches

This commit is contained in:
SrGooglo 2024-03-25 08:11:46 +01:00
parent f371ffce6a
commit 70056e82a1

View File

@ -10,7 +10,29 @@ import {
getInstalledPackages,
} from "../local_db"
function findPatch(pkg, changes, mustBeInstalled) {
return pkg.patches
.filter((patch) => {
const patchID = patch.id
if (typeof changes.patches[patchID] === "undefined") {
return false
}
if (mustBeInstalled === true && !pkg.applied_patches.includes(patch.id) && changes.patches[patchID] === true) {
return true
}
if (mustBeInstalled === false && pkg.applied_patches.includes(patch.id) && changes.patches[patchID] === false) {
return true
}
return false
})
}
export default async function apply(pkg_id, changes) {
try {
let pkg = await getInstalledPackages(pkg_id)
if (!pkg) {
@ -22,18 +44,14 @@ export default async function apply(pkg_id, changes) {
console.log(`[${pkg_id}] apply() | Applying changes... >`, changes)
if (Array.isArray(changes.patches)) {
if (typeof changes.patches !== "undefined") {
if (!Array.isArray(pkg.applied_patches)) {
pkg.applied_patches = []
}
const disablePatches = pkg.patches.filter((p) => {
return !changes.patches[p.id]
})
const disablePatches = findPatch(pkg, changes, false)
const installPatches = pkg.patches.filter((p) => {
return changes.patches[p.id]
})
const installPatches = findPatch(pkg, changes, true)
for await (let patch of disablePatches) {
sendToRender(`pkg:update:status`, {
@ -74,6 +92,7 @@ export default async function apply(pkg_id, changes) {
for await (let patch of installPatches) {
if (pkg.applied_patches.includes(patch.id)) {
console.log(`[${pkg_id}] apply() | Patch [${patch.id}] already applied. Skipping...`)
continue
}
@ -86,7 +105,7 @@ export default async function apply(pkg_id, changes) {
console.log(`[${pkg_id}] apply() | Applying patch [${patch.id}]...`)
for await (let addition of patch.additions) {
console.log(addition)
console.log(`Processing addition [${addition.file}]`, addition)
// resolve patch file
addition.file = await parseStringVars(addition.file, pkg)
@ -130,10 +149,17 @@ export default async function apply(pkg_id, changes) {
sendToRender(`pkg:update:status`, {
...pkg,
statusText: "Changes applied!",
})
console.log(`[${pkg_id}] apply() | Changes applied`)
return true
} catch (error) {
console.log(error)
sendToRender(`new:notification`, {
type: "error",
message: "Failed to apply changes",
})
}
}