savvy-js
Version:
Savvy - Style sheet documentation tool
125 lines (103 loc) • 4.33 kB
JavaScript
(function(){
'use strict';
var fileReader = require('./fileReader.js'),
parser = require('./parser.js'),
assembler = require('./assembler.js'),
renderer = require('./renderer'),
Minimatch = require('minimatch').Minimatch;
function handleError(err){
throw err;
}
function getDefaultConfig(config){
config = config || {};
if(config.hasOwnProperty('themeFolder') === false){
config.themeFolder = '../theme';
}
return config;
}
function getParsedComments(styleFolder, config, callback){
var parsedComments = [],
processedFiles = 0;
fileReader.getFiles(styleFolder, function(err, files){
if(err){
handleError(err);
}
else{
files.forEach(function(file){
if(config.debug){
console.log('processing ' + file);
}
fileReader.readFile(file, function(err, fileContent) {
var comments,
parsedComment;
processedFiles = processedFiles + 1;
if (err) {
handleError(err);
}
else {
comments = parser.extractComments(fileContent);
if(config.debug){
console.log('extracted ' + comments.length + ' comments');
}
comments.forEach(function (comment) {
parsedComment = parser.parseComment(comment);
parsedComment.filePath = file;
parsedComments.push(parsedComment);
});
}
if (processedFiles === files.length) {
callback(parsedComments);
}
});
});
}
}, config.pattern, config.patternOptions);
}
/**
* Run Savvy to generate style documentation site.
*
* @method run
* @param {string} sourceFolder - path to the documented style folder
* @param {string} targetFolder - path were you want the documentation site to be in
* @param {array} styleFiles - array of paths to the compiled css files. These will be copied to the documentation
* site as-is.
* @param {object} [config] - pass configuration to tell savvy how to render
* @param {string} [config.pattern] - A pattern that matches the files you wish to process.
* @param {object} [config.patternOptions] - Pattern matching options. See more details at https://github.com/isaacs/minimatch
* @param {string} [config.themeFolder] - path to the theme folder, if you wish to use a different theme
*/
function run(sourceFolder, targetFolder, styleFiles, config, callback) {
config = getDefaultConfig(config);
if(config.debug){
console.log('Savvy started');
}
getParsedComments(sourceFolder, config, function(parsedComments){
var tree;
if(config.debug){
console.log('Finished processing comments.');
console.log('Starting to assemble.');
}
tree = assembler.assemble(parsedComments);
if(config.debug){
console.log('Finished assembling.');
console.log('Converting object tree to arrays.');
}
tree = assembler.treeToArray(tree);
if(config.debug){
console.log('Finished converting.');
console.log('Starting rendering.');
}
renderer.render(tree, config.themeFolder, targetFolder, styleFiles, function(err){
if(err){
handleError(err);
}
else {
callback();
}
});
});
}
module.exports = {
run: run
};
}());