Split out file and directory callbacks

This commit is contained in:
Kevin Sawicki
2012-10-09 00:28:28 -07:00
parent 71c161d527
commit 8b61e6a9df
5 changed files with 47 additions and 20 deletions

View File

@@ -16,7 +16,7 @@ var $native = {};
native function list(path, recursive);
$native.list = list;
native function traverseTree(path, callback);
native function traverseTree(path, onFile, onDirectory);
$native.traverseTree = traverseTree;
native function isFile(path);

View File

@@ -108,22 +108,30 @@ bool Native::Execute(const CefString& name,
if (tree == NULL)
return true;
CefRefPtr<CefV8Value> function = arguments[1];
CefRefPtr<CefV8Value> onFile = arguments[1];
CefRefPtr<CefV8Value> onDirectory = arguments[2];
CefV8ValueList args;
FTSENT *entry;
while ((entry = fts_read(tree)) != NULL) {
if (entry->fts_level == 0)
continue;
if ((entry->fts_info & FTS_D) != 0 || (entry->fts_info & FTS_F) != 0) {
if((entry->fts_info & FTS_F) != 0) {
int pathLength = entry->fts_pathlen - rootPathLength;
char relative[pathLength + 1];
relative[pathLength] = '\0';
strncpy(relative, entry->fts_path + rootPathLength, pathLength);
args.clear();
args.push_back(CefV8Value::CreateString(relative));
args.push_back(CefV8Value::CreateString(entry->fts_name));
args.push_back(CefV8Value::CreateBool((entry->fts_info & FTS_F) != 0));
if (!function->ExecuteFunction(function, args)->GetBoolValue())
onFile->ExecuteFunction(onFile, args);
}
else if ((entry->fts_info & FTS_D) != 0) {
int pathLength = entry->fts_pathlen - rootPathLength;
char relative[pathLength + 1];
relative[pathLength] = '\0';
strncpy(relative, entry->fts_path + rootPathLength, pathLength);
args.clear();
args.push_back(CefV8Value::CreateString(relative));
if (!onDirectory->ExecuteFunction(onDirectory, args)->GetBoolValue())
fts_set(tree, entry, FTS_SKIP);
}
}

View File

@@ -111,7 +111,8 @@ describe "Project", ->
describe ".getFilePaths()", ->
it "ignores files that return true from atom.ignorePath(path)", ->
spyOn(project, 'ignorePath').andCallFake (path) -> fs.base(path).match /a$/
spyOn(project, 'ignoreDirectory').andCallFake (path) -> fs.base(path).match /a$/
spyOn(project, 'ignoreFile').andCallFake (path) -> fs.base(path).match /a$/
project.getFilePaths().done (paths) ->
expect(paths).not.toContain('a')

View File

@@ -55,22 +55,40 @@ class Project
filePaths = []
fs.traverseTree @getPath(), (path, name, isFile) =>
return false if @ignorePath(path, name, isFile)
filePaths.push path if isFile
return not isFile
onFile = (path) =>
filePaths.push(path) unless @ignoreFile(path)
onDirectory = (path) =>
return not @ignoreDirectory(path)
fs.traverseTree @getPath(), onFile, onDirectory
deferred.resolve filePaths
deferred
ignorePath: (path, name, isFile) ->
if isFile
for ignored in @ignoredFileNames
return true if name is ignored
ignoreDirectory: (path) ->
lastSlash = path.lastIndexOf('/')
if lastSlash isnt -1
name = path.substring(lastSlash + 1)
else
for ignored in @ignoredFolderNames
return true if name is ignored
name = path
for ignored in @ignoredFolderNames
return true if name is ignored
for regex in @ignoredPathRegexes
return true if path.match(regex)
return false
ignoreFile: (path) ->
lastSlash = path.lastIndexOf('/')
if lastSlash isnt -1
name = path.substring(lastSlash + 1)
else
name = path
for ignored in @ignoredFileNames
return true if name is ignored
for regex in @ignoredPathRegexes
return true if path.match(regex)

View File

@@ -102,8 +102,8 @@ module.exports =
@makeTree(@directory(path))
@makeDirectory(path)
traverseTree: (rootPath, fn) ->
$native.traverseTree(rootPath, fn)
traverseTree: (rootPath, onFile, onDirectory) ->
$native.traverseTree(rootPath, onFile, onDirectory)
lastModified: (path) ->
$native.lastModified(path)