define([ "./buildControl", "./fileUtils", "./fs" ], function(bc, fileUtils, fs){ // find all files as given by files, dirs, trees, and packages var dirsProcessed = // a set of the directory names that have been inspected {}, treesDirsFiles = ["trees", "dirs", "files"], srcDirs = {}, destDirs = {}, getFilepath = fileUtils.getFilepath, catPath = fileUtils.catPath, compactPath = fileUtils.compactPath, start = function(resource, tagResource){ if(!resource.tag){ resource.tag = {}; } if(tagResource){ tagResource(resource); } bc.start(resource); srcDirs[getFilepath(resource.src)] = 1; destDirs[getFilepath(resource.dest)] = 1; }, getResourceTagFunction = function(resourceTags){ //resource tags is a map from tag to a function or a regular expression var getFilterFunction = function(item){ return typeof item=="function" ? item : function(filename){ return item.test(filename); }; }, tag = {}, gotOne = false; for(var p in resourceTags){ tag[p] = getFilterFunction(resourceTags[p]); gotOne = true; } if(!gotOne){ return 0; } return function(resource){ for(var p in tag){ if(tag[p](resource.src, resource.mid, resource)){ resource.tag[p] = 1; } } }; }, neverExclude = function(){ return 0; }, getExcludes = function(excludes){ // excludes is falsy, a function, or a regulare expression if(!excludes){ return neverExclude; }else if(typeof excludes=="function"){ return excludes; }else{ return function(filename){ return excludes.test(filename); }; } }, readSingleDir = function(srcBase, destBase, current, excludes, advise, traverse){ // Read a directory and advise of each child contained therein if the child is // not excluded. // // If traverse is truthy, then traverse the directory tree. When traversing, // current gives the current position in the traversal relative to srcBase. // The first call when traversing (or only call when not traversing) must // have current set to falsy. // // Notice that only the complete child path relative to srcBase is submitted // to excludes. This simplifies constructing exclude functions since srcBase // will never be part of the input to those functions. var dir = srcBase + (current ? "/" + current : ""), fullPrefix = dir + "/", currentPrefix = current ? current + "/" : "", subdirs = []; // inspect each directory once per build if(dirsProcessed[dir]){ return; } dirsProcessed[dir] = 1; fs.readdirSync(dir).forEach(function(filename){ var current = currentPrefix + filename; if(!excludes || !excludes(current)){ var fullFilename = fullPrefix + filename, stats = fs.statSync(fullFilename); if(stats.isDirectory()){ subdirs.push(current); }else{ advise(fullFilename, destBase + "/" + current); } } }); if(traverse && subdirs.length){ subdirs.forEach(function(current){ readSingleDir(srcBase, destBase, current, excludes, advise, 1); }); } }, readFile = function(item, advise){ advise(item[0], item[1]); }, srcPathExists = function(srcPath){ if(!fileUtils.dirExists(srcPath)){ bc.log("missingDirDuringDiscovery", ["directory", srcPath]); return 0; } return 1; }, readDir = function(item, advise){ if(srcPathExists(item[0])){ readSingleDir(item[0], item[1], 0, getExcludes(item[2]), advise, 0, 0); } }, readTree = function(item, advise){ if(srcPathExists(item[0])){ readSingleDir(item[0], item[1], 0, getExcludes(item[2]), advise, 1); } }, discover = { files:readFile, dirs:readDir, trees:readTree }, processPackage = function(pack, destPack){ // treeItem is the package location tree; it may give explicit exclude instructions var treeItem; for(var trees = pack.trees || [], i = 0; i