Skip to content

Commit

Permalink
Redo the use of .bind for exported functions -- simplify grunt tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
robogeek committed May 19, 2015
1 parent 4790b5a commit 75ae079
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 424 deletions.
339 changes: 151 additions & 188 deletions index.js

Large diffs are not rendered by default.

52 changes: 27 additions & 25 deletions lib/fileCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ module.exports.config = function(_akasha, _config) {
akasha = _akasha;
logger = akasha.getLogger("fileCache");

module.exports.readDocument = readDocument.bind(_config);
module.exports.createInMemoryDocument = createInMemoryDocument.bind(_config);
module.exports.createDocument = createDocument.bind(_config);
module.exports.updateDocumentData = updateDocumentData.bind(_config);
module.exports.deleteDocumentForUrlpath = deleteDocumentForUrlpath.bind(_config);
module.exports.eachDocument = eachDocument.bind(_config);
module.exports.eachDocumentAsync = eachDocumentAsync.bind(_config);
module.exports.documentForUrlpath = documentForUrlpath.bind(_config);
module.exports.findMatchingDocuments = findMatchingDocuments.bind(_config);
module.exports.readTemplate = readTemplate.bind(_config);
module.exports.readPartial = readPartial.bind(_config);
module.exports.indexChain = indexChain.bind(_config);
module.exports.readDocument = _readDocument.bind(null, _akasha, _config);
module.exports.createInMemoryDocument = _createInMemoryDocument.bind(null, _akasha, _config);
module.exports.createDocument = _createDocument.bind(null, _akasha, _config);
module.exports.updateDocumentData = _updateDocumentData.bind(null, _config);
module.exports.deleteDocumentForUrlpath = _deleteDocumentForUrlpath.bind(null, _config);
module.exports.eachDocument = _eachDocument.bind(null, _config);
module.exports.eachDocumentAsync = _eachDocumentAsync.bind(null, _config);
module.exports.documentForUrlpath = _documentForUrlpath.bind(null, _config);
module.exports.findMatchingDocuments = _findMatchingDocuments.bind(null, _config);
module.exports.readTemplate = _readTemplate.bind(null, _akasha, _config);
module.exports.readPartial = _readPartial.bind(null, _akasha, _config);
module.exports.indexChain = _indexChain.bind(null, _akasha, _config);
};

var extractFrontmatter = module.exports.extractFrontmatter = function(text) {
Expand All @@ -60,7 +60,8 @@ var extractFrontmatter = module.exports.extractFrontmatter = function(text) {
};

var docCache = [];
function readDocument(docName, done) {
function _readDocument(akasha, config, docName, done) {
// logger.trace('START readDocument read '+ docName);
if (docName.charAt(0) === '/') {
docName = docName.substr(1);
}
Expand All @@ -85,11 +86,12 @@ function readDocument(docName, done) {
find.documentAsync(docName, function(err, docEntry) {
if (err) { logger.error(err); done(err); }
else {
logger.trace('readDocument read '+ docName);
logger.trace('readDocument read '+ docName); // +' '+ docEntry.fullpath);
fs.readFile(docEntry.fullpath, 'utf8', function(err, text) {
if (err) { logger.error(err); done(err); }
else {
var renderer = akasha.findRenderChain(docName);
// logger.trace(docName +' renderer: '+ util.inspect(renderer));
if (!renderer) {
renderer = rendererNull.match(docName);
renderer.renderSync = rendererNull.renderSync;
Expand Down Expand Up @@ -127,7 +129,7 @@ function readDocument(docName, done) {
}
}

function createInMemoryDocument(rootdir, docpath, text, done) {
function _createInMemoryDocument(akasha, config, rootdir, docpath, text, done) {
var renderChain = akasha.findRenderChain(docpath);
if (!renderChain) {
renderChain = rendererNull.match(docpath);
Expand Down Expand Up @@ -166,7 +168,7 @@ function createInMemoryDocument(rootdir, docpath, text, done) {
}
}

function createDocument(rootdir, docpath, metadata, content, done) {
function _createDocument(akasha, config, rootdir, docpath, metadata, content, done) {
var text =
'---\n'
+ metadata +'\n'
Expand Down Expand Up @@ -231,7 +233,7 @@ function createDocument(rootdir, docpath, metadata, content, done) {
}
}

function updateDocumentData(docEntry, metadata, content, cb) {
function _updateDocumentData(config, docEntry, metadata, content, cb) {
var text =
'---\n'
+ metadata +'\n'
Expand All @@ -257,7 +259,7 @@ function updateDocumentData(docEntry, metadata, content, cb) {
}
}

function deleteDocumentForUrlpath(urlpath, cb) {
function _deleteDocumentForUrlpath(config, urlpath, cb) {
logger.trace('deleteDocumentForUrlpath '+ urlpath);
if (docCache[urlpath]) {
// util.log('FOUND');
Expand All @@ -277,21 +279,21 @@ function deleteDocumentForUrlpath(urlpath, cb) {
}
}

function eachDocument(doccb) {
function _eachDocument(config, doccb) {
var keys = Object.keys(docCache);
for (var i = 0; i < keys.length; i++) {
doccb(docCache[keys[i]]);
}
}

function eachDocumentAsync(action, fini) {
function _eachDocumentAsync(config, action, fini) {
var docs = [];
module.exports.eachDocument(function(docEntry) { docs.push(docEntry); });
// util.log('eachDocumentAsync count='+ docs.length);
async.eachSeries(docs, action, fini);
}

function documentForUrlpath(_urlpath) {
function _documentForUrlpath(config, _urlpath) {
var docEntry, urlpath = _urlpath;
if (urlpath.charAt(0) === '/') {
urlpath = urlpath.substr(1);
Expand All @@ -307,7 +309,7 @@ function documentForUrlpath(_urlpath) {
return undefined;
}

function findMatchingDocuments(matchers) {
function _findMatchingDocuments(config, matchers) {
var matched = [];
var keys = Object.keys(docCache);
for (var i = 0; i < keys.length; i++) {
Expand Down Expand Up @@ -355,7 +357,7 @@ function findMatchingDocuments(matchers) {
}

var templCache = [];
function readTemplate(templName, done) {
function _readTemplate(akasha, config, templName, done) {
if (templCache.hasOwnProperty(templName)) {
var templEntry = templCache[templName];
fs.stat(templEntry.fullpath, function(err, stats) {
Expand Down Expand Up @@ -418,7 +420,7 @@ function readTemplate(templName, done) {

// This function doesn't seem to be used - do we need to keep it around?
var partialCache = [];
function readPartial(partialName, done) {
function _readPartial(akasha, config, partialName, done) {
if (partialCache.hasOwnProperty(partialName)) {
var partialEntry = partialCache[partialName];
fs.stat(partialEntry.fullpath, function(err, stats) {
Expand Down Expand Up @@ -479,7 +481,7 @@ function readPartial(partialName, done) {
}
};

function indexChain(docName) {
function _indexChain(akasha, config, docName) {
var docEntry = module.exports.documentForUrlpath(docName);
if (!docEntry) throw new Error('Could not find docEntry for '+ docName);
else {
Expand Down
70 changes: 35 additions & 35 deletions lib/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ var logger;
module.exports.config = function(akasha, config) {
logger = akasha.getLogger("find");

module.exports.theme = findTheme.bind(config);
module.exports.themeAsync = findThemeAsync.bind(config);
module.exports.template = findTemplate.bind(config);
module.exports.templateAsync = findTemplateAsync.bind(config);
module.exports.partial = findPartial.bind(config);
module.exports.partialAsync = findPartialAsync.bind(config);
module.exports.document = findDocument.bind(config);
module.exports.documentAsync = findDocumentAsync.bind(config);
module.exports.assetFile = findAssetFile.bind(config);
module.exports.theme = _findTheme.bind(null, config);
module.exports.themeAsync = _findThemeAsync.bind(null, config);
module.exports.template = _findTemplate.bind(null, config);
module.exports.templateAsync = _findTemplateAsync.bind(null, config);
module.exports.partial = _findPartial.bind(null, config);
module.exports.partialAsync = _findPartialAsync.bind(null, config);
module.exports.document = _findDocument.bind(null, config);
module.exports.documentAsync = _findDocumentAsync.bind(null, config);
module.exports.assetFile = _findAssetFile.bind(null, config);
};


function findTheme(themeName) {
function _findTheme(config, themeName) {
// Does the file match on its own?
var stat = fs.existsSync(themeName)
? fs.statSync(themeName)
: undefined;
if (stat) {
return themeName;
}
if (this.root_theme) {
for (var i = 0; i < this.root_theme.length; i++) {
var theme = this.root_theme[i];
if (config.root_theme) {
for (var i = 0; i < config.root_theme.length; i++) {
var theme = config.root_theme[i];
// Is it in a theme directory?
stat = fs.existsSync(path.join(theme, themeName))
? fs.statSync(path.join(theme, themeName))
Expand All @@ -63,10 +63,10 @@ function findTheme(themeName) {

}

function findThemeAsync(themeName, done) {
function _findThemeAsync(config, themeName, done) {
logger.trace('themeAsync '+ themeName);
var fnTheme, themeroot;
async.eachSeries(this.root_theme,
async.eachSeries(config.root_theme,
function(root, next) {
if (!fnTheme) {
var fntheme = path.join(root, themeName);
Expand Down Expand Up @@ -99,7 +99,7 @@ function findThemeAsync(themeName, done) {
/**
* Find a template file whether it's directly specified, or whether its in the layout directory
**/
function findTemplate(tmplName) {
function _findTemplate(config, tmplName) {
// Does the file match on its own?
logger.trace('find.template ' + util.inspect(tmplName));
// util.log(util.inspect(config));
Expand All @@ -114,9 +114,9 @@ function findTemplate(tmplName) {
};
}
// util.log(util.inspect(options.root_layouts));
if (this.root_layouts) {
for (var i = 0; i < this.root_layouts.length; i++) {
var root = this.root_layouts[i];
if (config.root_layouts) {
for (var i = 0; i < config.root_layouts.length; i++) {
var root = config.root_layouts[i];
// Is it in a layouts directory?
// util.log('root: '+ root);
// util.log('find.template ' + path.join(root, tmplName));
Expand All @@ -135,10 +135,10 @@ function findTemplate(tmplName) {
return undefined;
}

function findTemplateAsync(tmplName, done) {
function _findTemplateAsync(config, tmplName, done) {
logger.trace('templateAsync '+ tmplName);
var found;
async.eachSeries(this.root_layouts,
async.eachSeries(config.root_layouts,
function(root, next) {
if (!found) {
var fntemplate = path.join(root, tmplName);
Expand Down Expand Up @@ -171,7 +171,7 @@ function findTemplateAsync(tmplName, done) {
/**
* Find a partial/template file whether it's directly specified, or whether its in a partials directory
**/
function findPartial(tmplName) {
function _findPartial(config, tmplName) {
// Does the file match on its own?
logger.trace('find.partial '+ tmplName);
// util.log('find.partial '+ tmplName);
Expand All @@ -182,10 +182,10 @@ function findPartial(tmplName) {
return tmplName;
}
// util.log(util.inspect(options));
if (this.root_partials) {
if (config.root_partials) {
// util.log(util.inspect(options.root_partials));
for (var i = 0; i < this.root_partials.length; i++) {
var partial = this.root_partials[i];
for (var i = 0; i < config.root_partials.length; i++) {
var partial = config.root_partials[i];
// util.log('Looking for '+ tmplName +' in '+ util.inspect(partial));
// Is it in a partials directory?
stat = fs.existsSync(path.join(partial, tmplName))
Expand All @@ -200,11 +200,11 @@ function findPartial(tmplName) {
return undefined;
}

function findPartialAsync(tmplName, done) {
function _findPartialAsync(config, tmplName, done) {
logger.trace('partialAsync '+ tmplName);
// util.log('partialAsync '+ tmplName);
var found;
async.eachSeries(this.root_partials,
async.eachSeries(config.root_partials,
function(root, next) {
if (!found) {
var fnpartial = path.join(root, tmplName);
Expand Down Expand Up @@ -235,7 +235,7 @@ function findPartialAsync(tmplName, done) {
/**
* Find a Document file whether it's directly specified, or whether it's in a document directory.
**/
function findDocument(docName) {
function _findDocument(config, docName) {
// Does the docName match on its own?
logger.trace('find.document '+ docName);
var stat = fs.existsSync(docName)
Expand All @@ -249,9 +249,9 @@ function findDocument(docName) {
};
}
// util.log(util.inspect(this));
if (this.root_docs) {
for (var i = 0; i < this.root_docs.length; i++) {
var docroot = this.root_docs[i];
if (config.root_docs) {
for (var i = 0; i < config.root_docs.length; i++) {
var docroot = config.root_docs[i];
// Is it in a partials directory?
stat = fs.existsSync(path.join(docroot, docName))
? fs.statSync(path.join(docroot, docName))
Expand All @@ -268,10 +268,10 @@ function findDocument(docName) {
return undefined;
}

function findDocumentAsync(docName, done) {
function _findDocumentAsync(config, docName, done) {
logger.trace('documentAsync '+ docName);
var found;
async.eachSeries(this.root_docs,
async.eachSeries(config.root_docs,
function(root, next) {
if (!found) {
// util.log('find.documentAsync '+ root +' '+ docName);
Expand Down Expand Up @@ -300,10 +300,10 @@ function findDocumentAsync(docName, done) {
});
}

function findAssetFile(assetFname, done) {
function _findAssetFile(config, assetFname, done) {
var found;

async.eachSeries([ this.root_docs, this.root_assets ],
async.eachSeries([ config.root_docs, config.root_assets ],
function(dirs, next) {
logger.trace('search for '+ assetFname +' in '+ util.inspect(dirs));
if (!found) async.eachSeries(dirs,
Expand Down
Loading

0 comments on commit 75ae079

Please sign in to comment.