Skip to content

Commit

Permalink
Trigger different events with different data for file vs. folder rena…
Browse files Browse the repository at this point in the history
…mes (adobe#558)

* Trigger different events with different data for file vs. folder renames

* Review fixes
  • Loading branch information
gideonthomas authored Jul 13, 2016
1 parent 67d5811 commit ba63dfc
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 16 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,14 @@ There are also high-level events for changes to files:
* `"fileChange"` - triggered whenever a file is created or updated within the project root. It includes the `filename` of the file that changed.
* `"fileDelete"` - triggered whenever a file is deleted within the project root. It includes the `filename` of the file that was deleted.
* `"fileRename"` - triggered whenever a file is renamed within the project root. It includes the `oldFilename` and the `newFilename` of the file that was renamed.
* `"folderRename"` - triggered whenever a folder is renamed within the project root. It includes an object that looks something like this:
```js
{
oldPath: "/path/before/rename",
newPath: "/path/after/rename",
// Paths to all files contained inside the folder being renamed
children: [ "relativeFilePath1", "relativeFilePath2", ... ]
}
```

NOTE: if you want to receive generic events for file system events, especially events across windows using the same file system, use [fs.watch()](https://github.com/filerjs/filer#watch) instead.
96 changes: 80 additions & 16 deletions src/bramble/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ define([
callback(err);
};
}
// Renames are special, since we care about both filenames
// File Renames are special, since we care about both filenames
function renameFileEventFn(type, oldFilename, newFilename, callback) {
return function(err) {
if(!err) {
Expand All @@ -514,6 +514,22 @@ define([
};
}

// Folder Renames are special since we care about the old name,
// the new name and all the child paths that were changed as a
// result of the rename
function renameFolderEventFn(type, oldFolderPath, newFolderPath, folderChildren, callback) {
return function(err) {
if(!err) {
self.trigger(type, [{
oldPath: oldFolderPath,
newPath: newFolderPath,
children: folderChildren
}]);
}
callback(err);
};
}

function deleteDirectoryRecursively(directory, callback) {
function deleteFile(path, next) {
// If the current path is a directory, do nothing
Expand Down Expand Up @@ -588,6 +604,30 @@ define([
});
}

function getChildrenOfFolder(folderPath, callback) {
var children = [];

function storeRelativePath(childPath, next) {
// We don't care about child folders, only files because
// the current user of Bramble viz. Thimble does not keep
// track of folder paths and hence including them would be
// redundant (possibly problematic)
if(!childPath.endsWith("/")) {
children.push(Path.relative(folderPath, childPath));
}

next();
}

shell.find(folderPath, { exec: storeRelativePath }, function(err) {
if(err) {
callback(err);
} else {
callback(null, children);
}
});
}

// Most fs methods can just get run normally, but we have to deal with
// ArrayBuffer vs. Filer.Buffer for readFile and writeFile, and persist
// watch callbacks. We also deal with the special case of `tutorial.html`.
Expand All @@ -607,22 +647,46 @@ define([
}));
break;
case "rename":
wrappedCallback = renameFileEventFn("fileRename", args[0], args[1], callback);
_fs.rename.apply(_fs, args.concat(function(err) {
if(!err) {
// If we rename tutorial.html to something else, we're removing it
if(args[0] === self.tutorialPath) {
wrappedCallback = genericFileEventFn("tutorialRemoved", args[0], wrappedCallback);
_tutorialExists = false;
}
// If we rename something to tutorial.html, we're adding a tutorial
else if(args[1] === self.tutorialPath) {
wrappedCallback = genericFileEventFn("tutorialAdded", args[1], wrappedCallback);
_tutorialExists = true;
}
// We need to check if the rename is happening on a file or a
// folder and accordingly trigger the right event and send the
// right data needed by the event listener
_fs.stat(args[0], function(err, stats) {
if(err) {
callback(err);
} else if(stats.isDirectory()) {
getChildrenOfFolder(args[0], function(err, children) {
if(err) {
callback(err);
} else {
_fs.rename.apply(
_fs,
args.concat(renameFolderEventFn(
"folderRename",
args[0], args[1], children,
callback
))
);
}
});
} else {
wrappedCallback = renameFileEventFn("fileRename", args[0], args[1], callback);
_fs.rename.apply(_fs, args.concat(function(err) {
if(!err) {
// If we rename tutorial.html to something else, we're removing it
if(args[0] === self.tutorialPath) {
wrappedCallback = genericFileEventFn("tutorialRemoved", args[0], wrappedCallback);
_tutorialExists = false;
}
// If we rename something to tutorial.html, we're adding a tutorial
else if(args[1] === self.tutorialPath) {
wrappedCallback = genericFileEventFn("tutorialAdded", args[1], wrappedCallback);
_tutorialExists = true;
}
}
wrappedCallback(err);
}));
}
wrappedCallback(err);
}));
});
break;
case "unlink":
path = args[0];
Expand Down

0 comments on commit ba63dfc

Please sign in to comment.