From 66ef112acf365bca2145e37de690bbdd8b35f3be Mon Sep 17 00:00:00 2001 From: Rithvik Nishad Date: Fri, 8 Nov 2024 20:11:58 +0530 Subject: [PATCH] Update setup-care-apps scripts to remove existing apps that are not configured in env if any (#9052) --- .gitignore | 1 + scripts/setup-care-apps.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/.gitignore b/.gitignore index 000a965c44a..3ae96d927fb 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ cypress/fixtures/token.json # Care Apps /apps/* src/pluginMap.ts +/apps_backup/* \ No newline at end of file diff --git a/scripts/setup-care-apps.js b/scripts/setup-care-apps.js index 870c73bd247..5f29678c1db 100644 --- a/scripts/setup-care-apps.js +++ b/scripts/setup-care-apps.js @@ -53,6 +53,36 @@ const installApp = (app) => { ); }; +const backupDir = path.join(__dirname, "..", "apps_backup"); + +// Create backup directory if needed +if (!fs.existsSync(backupDir)) { + fs.mkdirSync(backupDir); +} + +try { + fs.readdirSync(appsDir, { withFileTypes: true }) + .filter((dirent) => dirent.isDirectory()) + .map((dirent) => dirent.name) + .filter( + (dir) => + !appsConfig.map((app) => app.package.split("/")[1]).includes(dir), + ) + .forEach((unusedApp) => { + const appPath = path.join(appsDir, unusedApp); + const backupPath = path.join(backupDir, `${unusedApp}_${Date.now()}`); + console.log(`Backing up '${unusedApp}' to ${backupPath}`); + fs.cpSync(appPath, backupPath, { recursive: true }); + console.log( + `Removing existing app '${unusedApp}' as it is not configured.`, + ); + fs.rmSync(appPath, { recursive: true, force: true }); + }); +} catch (error) { + console.error("Error during cleanup:", error); + process.exit(1); +} + // Clone or pull care apps appsConfig.forEach((app) => { const appDir = path.join(appsDir, app.package.split("/")[1]);