Skip to content

Commit

Permalink
fix: Error creating the autosave file when autosave timer is invoked …
Browse files Browse the repository at this point in the history
…for the first time and the autosave folder doesn't exist yet, Autosave file isn't removed when a patch is saved, delay show dialog function call to 7 secs

Signed-off-by: prakharagarwal1 <[email protected]>
  • Loading branch information
Prakhar-Agarwal-byte committed Aug 30, 2023
1 parent 43b932a commit 3f9412c
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions pd/nw/pdgui.js
Original file line number Diff line number Diff line change
Expand Up @@ -1969,7 +1969,7 @@ function gui_engine_ready() {
if (!isAutosaveDataEmpty()) {
setTimeout(() => {
showDialog();
}, 4);
}, 7);
}
}

Expand Down Expand Up @@ -2020,7 +2020,6 @@ function deleteDirectoryRecursive(directoryPath) {
fs.unlinkSync(filePath); // Delete files
}
});
fs.rmdirSync(directoryPath); // Finally, delete the empty directory
}
}

Expand Down Expand Up @@ -2079,6 +2078,16 @@ function autosave(autosave_value) {
clearInterval(autosave_timer);
// post("autosave timer value " + autosave_value);
if (autosave_value === 0) return;

// Check if the folder exists
if (!fs.existsSync(autosave_folder)) {
// If the folder doesn't exist, create it
fs.mkdirSync(autosave_folder);
// console.log('Autosave folder created.');
} else {
// console.log('Autosave folder already exists.');
}

autosave_timer = setInterval(function() {
pdsend("pd autosave", autosave_folder);
}, autosave_value * 1000);
Expand Down Expand Up @@ -7283,20 +7292,28 @@ function gui_canvas_saved(oldDirPath, oldFileName, newDirPath, newFileName) {
// post("gui_canvas_saved: " + oldDirPath + " " + oldFileName + " " + newDirPath + " " + newFileName);
const jsonFilePath = autosave_folder + "/" + "autosave.json";
const origCanvasFullPath = oldDirPath + "/" + oldFileName;
const newCanvasFullPath = newDirPath + "/" + newFileName;

//Check if "autosave.json" file exists
if (!fs.existsSync(autosave_folder)) return;
if (!fs.existsSync(jsonFilePath)) return;

const jsonData = fs.readFileSync(jsonFilePath, 'utf-8');
const data = JSON.parse(jsonData);

const oldAutosaveCanvasFullPath = data[origCanvasFullPath];

//Replace
const oldAutosaveCanvasFullPath = data[origCanvasFullPath];
delete data[origCanvasFullPath];
data[newCanvasFullPath] = oldAutosaveCanvasFullPath;

try {
if (fs.existsSync(oldAutosaveCanvasFullPath)) {
fs.unlinkSync(oldAutosaveCanvasFullPath);
delete data[origCanvasFullPath];
// console.log('File deleted successfully.');
} else {
// console.log('File does not exist.');
}
} catch (err) {
// console.error('Error occurred while deleting the file:', err);
}
// Write data to the JSON file
fs.writeFileSync(jsonFilePath, JSON.stringify(data, null, 2), 'utf-8');
}
Expand Down

0 comments on commit 3f9412c

Please sign in to comment.