Skip to content

Instantly share code, notes, and snippets.

@jimmiehansson
Created August 14, 2013 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmiehansson/6235613 to your computer and use it in GitHub Desktop.
Save jimmiehansson/6235613 to your computer and use it in GitHub Desktop.
This is a gist made for Stackoverflow question: Returning values between different scopes in javascript
/**
* Generates restful API
* @desc Library handles all directory/file streams and IO
* for the APIEngine structure. Asynchronous folder iteration
* with proper cleanup.
* @Deps: fs, async, underscore
* @Formalia: Free to use for whatever you like, do anything you want to it.
* @param null
* @return func
*/
var fs = require('fs')
, async = require('async')
, _ = require('underscore');
ApiFS = function(stgs){
errLog = [];
errLog.fsEmpty = "No existing directories or empty, cannot continue.";
this.resultset = [''];
}
/**
* Simple error handling for IO
* @desc response for post read of directories and files
* @param String
* @return Object
*/
function ioResponse(err){
if(err){ throw new err; }
}
/**
* @desc Validate that directories and files exist for the model path
* and perform necessary steps to load them into the Mongodb model
* @param function
* @return object
**/
ApiFS.prototype.scan = function(){
var self = this;
var b;
fs.readdir(stgs.fsPath, function(err,folders){
if(folders.length<1) { console.log(errLog.fsEmpty); return; }
rslt = self.walk(folders);
rslt.forEach(function(itm){
if(!self.exclusion(rslt)){ b=self.loader(stgs.fsPath,itm); }
});
}, ioResponse);
}
/**
* Walk the folders and load, check for exclusions
* @desc non-blocking IO, read and do exclusions, finally loader().
* @param String
* @return Object
*/
ApiFS.prototype.walk = function(folders, callback){
var fldrmap = [];
async.each(folders, function(folder, callback){ fldrmap.push(folder); callback();
}, function(err){
if(err) { throw new err; return;}
});
return fldrmap;
}
/**
* Exclusion control for walk()
* @desc control stgs.exclude for exclusions
* @param Object
* @return Object
*/
ApiFS.prototype.exclusion = function(folder){
var filt;
if(stgs.exclude.length>0){ return filt= (!_.contains(stgs.exclude, folder)); }
}
/**
* Read directories and cache output
* @desc Just cache as necessary to minimize IO
* @param function
* @return Object
*/
ApiFS.prototype.loader = function(dir,obj){
var self = this;
var itr = [];
fs.readdir(dir+'/'+obj, function(err,cf){
cf.forEach(function(fi){ tgt = dir+'/'+obj+'/'+cf; itr.push(self.modelMapper(obj,tgt)); });
console.log(itr);
}, ioResponse);
};
/**
* Add targets to the map
* @desc Nothing fancy
* @param String
* @return Object
*/
ApiFS.prototype.modelMapper = function(obj,tgt){
var modelMap=[];
modelMap.push(obj,tgt); //console.log(modelMap);
return modelMap;
}
exports = module.exports = ApiFS;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment