From 32bc0a30fc768528904aff9a4cf04a8afd057fa8 Mon Sep 17 00:00:00 2001 From: Dennis Kehrig Date: Fri, 30 Aug 2013 14:07:06 -0700 Subject: [PATCH 1/2] When saving a file at a different location, don't delete an existing assets folder there, but try to delete it. If 1000 attempts to do so are not enough, give up. --- main.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index 1f032d2a..50eb2afb 100644 --- a/main.js +++ b/main.js @@ -21,6 +21,8 @@ * */ +/*jshint unused: false */ + (function () { "use strict"; @@ -56,6 +58,7 @@ FILES_TO_IGNORE = [".ds_store", "desktop.ini"], DELAY_TO_WAIT_UNTIL_USER_DONE = 300, MAX_SIMULTANEOUS_UPDATES = 50, + MAX_DIR_RENAME_ATTEMPTS = 1000, TIMEOUT_ERROR_MESSAGE = "timeout"; // must be in sync with Generator's timeout error // TODO: Once we get the layer change management/updating right, we should add a @@ -793,12 +796,28 @@ return resolvedPromise(); } else { - // Delete the assets of a previous file - // Photoshop will have asked the user to confirm overwriting the PSD file at this point, - // so "overwriting" its assets is fine, too + // Rename the assets folder of another document at this location + // Try foo-assets-old, then foo-assets-old-2, etc. + // Give up after MAX_DIR_RENAME_ATTEMPTS many unsuccessful attempts if (fs.existsSync(newStorageDir)) { - console.log("Deleting existing storage directory %j", newStorageDir); - deleteDirectoryRecursively(newStorageDir); + var attempts = 0, + renamedNewStorageDir; + do { + attempts++; + renamedNewStorageDir = newStorageDir + "-old"; + if (attempts > 1) { + renamedNewStorageDir += "-" + attempts; + } + } while (fs.existsSync(renamedNewStorageDir) && attempts < MAX_DIR_RENAME_ATTEMPTS); + + // If the suggested path exists despite our efforts to find one that doesn't, give up + if (fs.existsSync(renamedNewStorageDir)) { + throw new Error("At least " + MAX_DIR_RENAME_ATTEMPTS + " other backups of " + + newStorageDir + " already exist. Giving up."); + } + + console.log("Renaming existing storage directory %j to %j", newStorageDir, renamedNewStorageDir); + fs.renameSync(newStorageDir, renamedNewStorageDir); } // Move generated assets to the new directory and delete the old one if empty From c61f19adeb39b4bedab47338df5313dec3b8d65d Mon Sep 17 00:00:00 2001 From: Dennis Kehrig Date: Fri, 30 Aug 2013 16:06:56 -0700 Subject: [PATCH 2/2] First check if we know the file at all before figuring out whether the changes are complex --- main.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/main.js b/main.js index 50eb2afb..23f0a41e 100644 --- a/main.js +++ b/main.js @@ -418,8 +418,20 @@ } } - var documentContext = _contextPerDocument[document.id], - unknownChange = false, + var documentContext = _contextPerDocument[document.id]; + + // Possible reasons for an undefined context: + // - User created a new image + // - User opened an image + // - User switched to an image that was created/opened before Generator started + if (!documentContext) { + console.log("Unknown document, so getting all information"); + requestEntireDocument(document.id); + return; + } + + // We have seen this document before: information about the changes are enough + var unknownChange = false, layersMoved = false; traverseLayers(document, function (obj, isLayer) { @@ -473,18 +485,6 @@ return; } - // Possible reasons for an undefined context: - // - User created a new image - // - User opened an image - // - User switched to an image that was created/opened before Generator started - if (!documentContext) { - console.log("Unknown document, so getting all information"); - requestEntireDocument(document.id); - return; - } - - // We have seen this document before: information about the changes are enough - // Resize event: regenerate everything if (!document.layers && document.bounds) { requestEntireDocument(document.id);