Skip to content

Commit

Permalink
Merge pull request #388 from NeurodataWithoutBorders/remove-tmp-folders
Browse files Browse the repository at this point in the history
Clean up conversion / stub folders whenever the app is closed
  • Loading branch information
CodyCBakerPhD authored Sep 25, 2023
2 parents f775bab + b24c9f2 commit 2749c4a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit 2749c4a

Please sign in to comment.