UNPKG

savvy-js

Version:

Savvy - Style sheet documentation tool

147 lines (121 loc) 4.84 kB
(function () { 'use strict'; var fs = require('fs'), ncp = require('ncp').ncp, rimraf = require('rimraf'), path = require('path'), COMPILED_CSS_FOLDER = 'compiledStyles', DATA_FILE_NAME = 'data.js'; function copyFile(source, target, callback) { fs.readFile(source, 'utf8', function (err, fileContent) { if (err) { callback(err); } else { fs.writeFile(target, fileContent, callback); } }); } function createTargetFolder(targetFolder, callback) { if (fs.existsSync(targetFolder)) { rimraf(targetFolder, function(err){ if(err){ callback(err); } else{ fs.mkdir(targetFolder, callback); } }); } else{ fs.mkdir(targetFolder, callback); } } function copyThemeFiles(sourceFolder, targetFolder, callback) { ncp(sourceFolder, targetFolder, callback); } function writeDataFile(data, targetFolder, callback) { var filename = targetFolder + '/' + DATA_FILE_NAME; fs.writeFile(filename, 'var data = ' + JSON.stringify(data) + ';', null, callback); } function copyStylesheetFiles(styleFiles, targetFolder, callback) { var targetStyleFolder = path.join(targetFolder, COMPILED_CSS_FOLDER), processQueue = styleFiles.length; fs.mkdir(targetStyleFolder, function () { if (!styleFiles || !styleFiles.length) { callback(); } styleFiles.forEach(function (styleFilePath) { var filename = path.basename(styleFilePath), targetStyleFilePath = path.join(targetStyleFolder, filename); copyFile(styleFilePath, targetStyleFilePath, function () { processQueue = processQueue - 1; if (processQueue <= 0) { callback(); } }); }); }); } function modifyIndexFile(targetFolder, styleFiles, callback) { var indexFilePath = path.join(targetFolder, 'index.html'), links = ''; function generateLink(filePath) { var fileName = path.basename(filePath), link = '<link rel="stylesheet" href="'; fileName = path.join(COMPILED_CSS_FOLDER, fileName); link += fileName; link += '" />'; return link; } fs.readFile(indexFilePath, function (err, fileContent) { if (err) { callback(err); } else { if (!styleFiles || !styleFiles.length) { links = ''; } else { styleFiles.forEach(function (styleFilePath) { links += generateLink(styleFilePath); }); } fileContent = fileContent.toString(); fileContent = fileContent.replace('{links}', links); fs.writeFile(indexFilePath, fileContent, callback); } }); } /** * Render the documentation site and put it in the target folder. * * @method render * @param {object} data - the tree that contains the parsed comments, after running the assembler. * @param {string} sourceFolder - path to the theme folder * @param {string} targetFolder - path to the output folder * @param {array} styleFiles - array of paths to the compiled css files. These will be copied to the targetFolder * and will be used to show live examples of the css inside the documentation. * @param {function} callback - this will be invoked once the rendering is done. */ function render(data, sourceFolder, targetFolder, styleFiles, callback) { // This is the number of times the processQueue function is called inside this function. var queue = 3; function processQueue() { queue = queue - 1; if (queue <= 0) { callback(); } } createTargetFolder(targetFolder, function(){ copyThemeFiles(sourceFolder, targetFolder, function () { modifyIndexFile(targetFolder, styleFiles, processQueue); }); writeDataFile(data, targetFolder, processQueue); copyStylesheetFiles(styleFiles, targetFolder, processQueue); }); } module.exports = { render: render }; }());