PromucFlow_constructor/app/client/cypress/apply-patches.js
Satish Gandham 83538ad74d
feat: Bundle optimization and first load improvements (#21667)
Co-authored-by: Ivan Akulov <mail@iamakulov.com>
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Ivan Akulov <iamakulov@outlook.com>
Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
Co-authored-by: Shrikant Sharat Kandula <shrikant@appsmith.com>
Co-authored-by: somangshu <somangshu.goswami1508@gmail.com>
2023-05-11 10:56:03 +05:30

53 lines
1.6 KiB
JavaScript

const { getVersionDir } = require("cypress/lib/tasks/state");
const chalk = require("chalk");
const Diff = require("diff");
const fs = require("fs/promises");
const path = require("path");
async function applyPatches() {
const patchesDir = path.join(__dirname, "patches");
const patches = await fs.readdir(patchesDir);
const installDir = getVersionDir();
console.log(`\n> Applying patches on to ${chalk.cyan(installDir)}\n`);
for (const filename of patches) {
if (!filename.endsWith(".patch")) {
continue;
}
const fullpath = path.join(patchesDir, filename);
const enc = "utf8";
const patch = await fs.readFile(fullpath, enc);
const relativeFilename = path.relative(__dirname, fullpath);
console.log(`>> Applying patch ${chalk.cyan(relativeFilename)}`);
await Diff.applyPatches(patch, {
loadFile: (index, callback) => {
console.debug(`>>> Loading old file: ${chalk.red(index.oldFileName)}`);
fs.readFile(path.join(installDir, index.oldFileName), enc)
.then((contents) => {
callback(null, contents);
})
.catch(callback);
},
patched: (index, content, callback) => {
console.debug(
`>>> Patched new file: ${chalk.green(index.newFileName)}`,
);
fs.writeFile(path.join(installDir, index.newFileName), content, enc)
.then(callback)
.catch(callback);
},
complete: () => {
console.log(
`>> Successfully applied patch ${chalk.cyan(relativeFilename)}`,
);
},
});
}
}
applyPatches().catch((e) => {
console.error(e);
process.exit(42);
});