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