2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/model/runPatches.js

41 lines
952 B
JavaScript

const frappe = require('frappejs');
module.exports = async function runPatches(allPatches, patchOrder) {
let executedPatchRuns = [];
try {
executedPatchRuns = (
await frappe.db.getAll({ doctype: 'PatchRun', fields: ['name'] })
).map((d) => d.name);
} catch (error) {}
let patchRunOrder = patchOrder
.map((text) => {
let [patch] = text.split(' ');
if (text && patch) {
return {
fileName: text,
method: allPatches[patch],
};
}
})
.filter(Boolean);
for (let patch of patchRunOrder) {
if (!executedPatchRuns.includes(patch.fileName)) {
await runPatch(patch);
}
}
};
async function runPatch(patch) {
try {
await patch.method();
let patchRun = frappe.getNewDoc('PatchRun');
patchRun.name = patch.fileName;
await patchRun.insert();
} catch (error) {
console.error(error);
console.log('Could not run patch', patch);
}
}