Skip to content

Commit

Permalink
feat: Delete unused partitions on app start to free up space
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Nov 2, 2021
1 parent f385e80 commit 2586812
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const AutoLaunch = require("auto-launch");
const importSettings = require("./ipcHandlers/main/importSettings");
const exportSettings = require("./ipcHandlers/main/exportSettings");
const promptCloseTab = require("./ipcHandlers/main/promptCloseTab");
const pruneUnusedPartitions = require("./util/pruneUnusedPartitions");
const flushSessionData = require("./ipcHandlers/main/flushSessionData");
const zoom = require("./ipcHandlers/main/zoom");
const contextMenu = require("electron-context-menu");
Expand All @@ -32,6 +33,10 @@ let settings = new Store({
name: "settings",
});

const tabStore = new Store({
name: "tabs",
});

let windowState = new Store({
name: "windowState",
defaults: {
Expand Down Expand Up @@ -215,6 +220,8 @@ if (!singleInstanceLock) {
altusAutoLauncher.disable();
}

pruneUnusedPartitions(tabStore.get("tabs"), tabStore.get("previouslyClosedTab"), app.getPath("userData"));

mainWindow.on("blur", () => mainWindow.send("window-blurred"));

mainWindow.on("focus", () => mainWindow.send("window-focused"));
Expand Down
31 changes: 31 additions & 0 deletions src/util/pruneUnusedPartitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require("fs");
const path = require("path");

/** @typedef {import("../types").TabType} TabType */

/**
* Deletes unused tab partitions to free up space
* @param {TabType[]} tabs
* @param {TabType} previouslyClosedTab
* @param {string} userDataPath
*/
const pruneUnusedPartitions = (tabs, previouslyClosedTab, userDataPath) => {
const partitionsDirPath = path.join(userDataPath, "Partitions");
const partitions = fs.readdirSync(partitionsDirPath);
const tabIds = [];
tabs.forEach((tab) => {
tabIds.push(tab.id);
});
if (previouslyClosedTab) {
tabIds.push(previouslyClosedTab.id);
}
partitions
.filter((id) => !tabIds.includes(id))
.forEach((partition) => {
fs.rmSync(path.join(partitionsDirPath, partition), {
recursive: true,
});
});
};

module.exports = pruneUnusedPartitions;

0 comments on commit 2586812

Please sign in to comment.