fix patches

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

View File

@ -10,130 +10,156 @@ 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) {
let pkg = await getInstalledPackages(pkg_id)
try {
let pkg = await getInstalledPackages(pkg_id)
if (!pkg) {
sendToRender("runtime:error", "Package not found")
return false
}
pkg = await initManifest(pkg)
console.log(`[${pkg_id}] apply() | Applying changes... >`, changes)
if (Array.isArray(changes.patches)) {
if (!Array.isArray(pkg.applied_patches)) {
pkg.applied_patches = []
if (!pkg) {
sendToRender("runtime:error", "Package not found")
return false
}
const disablePatches = pkg.patches.filter((p) => {
return !changes.patches[p.id]
})
pkg = await initManifest(pkg)
const installPatches = pkg.patches.filter((p) => {
return changes.patches[p.id]
})
console.log(`[${pkg_id}] apply() | Applying changes... >`, changes)
for await (let patch of disablePatches) {
sendToRender(`pkg:update:status`, {
id: pkg_id,
status: "loading",
statusText: `Removing patch [${patch.id}]...`,
})
if (typeof changes.patches !== "undefined") {
if (!Array.isArray(pkg.applied_patches)) {
pkg.applied_patches = []
}
console.log(`[${pkg_id}] apply() | Removing patch [${patch.id}]...`)
const disablePatches = findPatch(pkg, changes, false)
// remove patch additions
for await (let addition of patch.additions) {
// resolve patch file
addition.file = await parseStringVars(addition.file, pkg)
const installPatches = findPatch(pkg, changes, true)
console.log(`[${pkg_id}] apply() | Removing addition [${addition.file}]...`)
for await (let patch of disablePatches) {
sendToRender(`pkg:update:status`, {
id: pkg_id,
status: "loading",
statusText: `Removing patch [${patch.id}]...`,
})
if (!fs.existsSync(addition.file)) {
console.log(`[${pkg_id}] apply() | Removing patch [${patch.id}]...`)
// remove patch additions
for await (let addition of patch.additions) {
// resolve patch file
addition.file = await parseStringVars(addition.file, pkg)
console.log(`[${pkg_id}] apply() | Removing addition [${addition.file}]...`)
if (!fs.existsSync(addition.file)) {
continue
}
// remove addition
await fs.promises.unlink(addition.file, { force: true, recursive: true })
}
// TODO: remove file patch overrides with original file
// remove from applied patches
pkg.applied_patches = pkg.applied_patches.filter((p) => {
return p !== patch.id
})
sendToRender(`pkg:update:status`, {
id: pkg_id,
status: "done",
statusText: `Patch [${patch.id}] removed!`,
})
}
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
}
// remove addition
await fs.promises.unlink(addition.file, { force: true, recursive: true })
}
sendToRender(`pkg:update:status`, {
id: pkg_id,
status: "loading",
statusText: `Applying patch [${patch.id}]...`,
})
// TODO: remove file patch overrides with original file
// remove from applied patches
pkg.applied_patches = pkg.applied_patches.filter((p) => {
return p !== patch.id
})
console.log(`[${pkg_id}] apply() | Applying patch [${patch.id}]...`)
sendToRender(`pkg:update:status`, {
id: pkg_id,
status: "done",
statusText: `Patch [${patch.id}] removed!`,
})
}
for await (let addition of patch.additions) {
console.log(`Processing addition [${addition.file}]`, addition)
for await (let patch of installPatches) {
if (pkg.applied_patches.includes(patch.id)) {
continue
}
// resolve patch file
addition.file = await parseStringVars(addition.file, pkg)
sendToRender(`pkg:update:status`, {
id: pkg_id,
status: "loading",
statusText: `Applying patch [${patch.id}]...`,
})
if (fs.existsSync(addition.file)) {
continue
}
console.log(`[${pkg_id}] apply() | Applying patch [${patch.id}]...`)
for await (let addition of patch.additions) {
console.log(addition)
// resolve patch file
addition.file = await parseStringVars(addition.file, pkg)
if (fs.existsSync(addition.file)) {
continue
await processGenericSteps(pkg, addition.steps)
}
await processGenericSteps(pkg, addition.steps)
// add to applied patches
pkg.applied_patches.push(patch.id)
sendToRender(`pkg:update:status`, {
id: pkg_id,
status: "done",
statusText: `Patch [${patch.id}] applied!`,
})
}
}
if (changes.configs) {
if (!pkg.storaged_configs) {
pkg.storaged_configs = {}
}
// add to applied patches
pkg.applied_patches.push(patch.id)
sendToRender(`pkg:update:status`, {
id: pkg_id,
status: "done",
statusText: `Patch [${patch.id}] applied!`,
})
if (Object.keys(changes.configs).length !== 0) {
Object.entries(changes.configs).forEach(([key, value]) => {
pkg.storaged_configs[key] = value
})
}
}
await updateInstalledPackage(pkg)
sendToRender(`new:notification`, {
type: "success",
message: "Changes applied!",
})
sendToRender(`pkg:update:status`, {
...pkg,
})
console.log(`[${pkg_id}] apply() | Changes applied`)
return true
} catch (error) {
console.log(error)
sendToRender(`new:notification`, {
type: "error",
message: "Failed to apply changes",
})
}
if (changes.configs) {
if (!pkg.storaged_configs) {
pkg.storaged_configs = {}
}
if (Object.keys(changes.configs).length !== 0) {
Object.entries(changes.configs).forEach(([key, value]) => {
pkg.storaged_configs[key] = value
})
}
}
await updateInstalledPackage(pkg)
sendToRender(`new:notification`, {
type: "success",
message: "Changes applied!",
})
sendToRender(`pkg:update:status`, {
...pkg,
statusText: "Changes applied!",
})
console.log(`[${pkg_id}] apply() | Changes applied`)
return true
}