Skip to content

Commit

Permalink
Merge pull request #153 from adobe-photoshop/jrb/delete-folder-fix
Browse files Browse the repository at this point in the history
Jrb/delete folder fix
  • Loading branch information
joelrbrandt committed Aug 31, 2013
2 parents 801c5db + c61f19a commit 3407c4d
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*
*/

/*jshint unused: false */

(function () {
"use strict";

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -415,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) {
Expand Down Expand Up @@ -470,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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3407c4d

Please sign in to comment.