Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up conversion / stub folders whenever the app is closed #388

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { app, BrowserWindow, dialog, shell } from 'electron';
import { electronApp, optimizer, is } from '@electron-toolkit/utils'

import paths from '../../paths.config.json'

import main from '@electron/remote/main';
main.initialize()

Expand Down Expand Up @@ -393,8 +395,37 @@ app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) initialize()
})


const homeDirectory = app.getPath("home");
const appDirectory = path.join(homeDirectory, paths.root)
const guidedProgressFilePath = path.join(appDirectory, ...paths.subfolders.progress);
const guidedConversionFolderPath = path.join(appDirectory, ...paths.subfolders.conversions);
const guidedStubFolderPath = path.join(appDirectory, ...paths.subfolders.stubs);

function getEntries(path, type = 'isDirectory') {
return fs.readdirSync(path, { withFileTypes: true })
.filter(dirent => dirent[type]())
.map(dirent => dirent.name)
}


// This function removes all folders that don't have a corresponding pipeline (specifically DANDI temp folders—but also any folders left after a pipeline is deleted)
function deleteFoldersWithoutPipelines() {
const conversionDirectories = getEntries(guidedConversionFolderPath)
const stubDirectories = getEntries(guidedStubFolderPath)

const allowedDirectories = getEntries(guidedProgressFilePath, 'isFile').map(name => name.slice(0, -'.json'.length))
const conversionsToRemove = conversionDirectories.filter(name => !allowedDirectories.includes(name))
const stubsToRemove = stubDirectories.filter(name => !allowedDirectories.includes(name))

conversionsToRemove.forEach(name => fs.rmSync(path.join(guidedConversionFolderPath, name), { recursive: true }))
stubsToRemove.forEach(name => fs.rmSync(path.join(guidedStubFolderPath, name), { recursive: true }))
}


app.on('will-quit', async () => {
try {
deleteFoldersWithoutPipelines()
await exitPyProc()
if (globals.mainWindow) {
globals.mainWindow.close();
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async function pythonServerClosed(message?: string) {
allowEscapeKey: false,
});

if (isElectron) app.exit();
if (isElectron) app.quit();
else location.reload()

Swal.close();
Expand Down