All files document.js

19.35% Statements 6/31
0% Branches 0/12
0% Functions 0/10
19.35% Lines 6/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                                1x   1x           1x               1x                                 1x                         1x                                            
// Documentation Creator
// =====================
//
// This code is what actually *creates* the documentation by running the
// parser/formatter against the source files and writing their output to files.
 
import fs from "fs-extra";
import path from "path";
import {promisify} from "util";
 
import configure from "./configure";
import parse from "./parse";
import format from "./format";
import write from "./write";
 
// Simple Promisified version of the Node fs.readFile. Useful for async batching.
const readFileAsync = promisify(fs.readFile);
 
const errorCallback = (error) => {
	if (error) {
		throw error;
	}
};
 
const copyAsset = function(file, output, callback) {
	if (!fs.existsSync(file)) {
		return callback();
	}
	return fs.copy(file, path.join(output, path.basename(file)), callback);
};
 
// Factory that returns a function that processes a source file.
const processSourcesFactory = function(config, callback) {
	return (source) => {
		// We first read the file
		return readFileAsync(source)
			.then((buffer) => {
				// then we parse the code, format it into sections, and finally write it to a file.
				const code = buffer.toString();
				const sections = format(source, parse(source, code, config), config);
				write(source, sections, config);
				config.writtenSources = (config.writtenSources || 0) + 1;
			})
			.catch(callback);
	};
};
 
// Simply Factory method that builds a function which, as long as there is no error,
// will copy the layout's files (if any) from its public folder to the output folder.
const copyLayoutFactory = function(config, callback) {
	return function(error) {
		if (error) {
			return callback(error);
		}
		if (fs.existsSync(config.public)) {
			return copyAsset(config.public, config.output, callback);
		}
		return callback();
	};
};
 
// Document is the function that actaully creates the documentation.
export const document = function(options = {}, callback = errorCallback) {
	// First we get a config object from the passed options.
	const config = configure(options);
	// Then we make the output directory (if we need to)
	global.console.log("docco: creating output directory (if required)");
	return fs.mkdirs(config.output, function() {
		global.console.log("docco: processing sources");
		// Once the output directory exists we can process the source files.
		const operations = config.sources.map(processSourcesFactory(config, callback));
		// We batch the Promises that are now in the operations constant
		// so we can trigger an event when they all are done (or fast-fail).
		return Promise.all(operations)
			.then(() => {
				// Once we're all done - copy the layouts css and other assets if required.
				global.console.log(`docco: processed and wrote ${config.writtenSources} files to directory: ${config.output}`);
				return copyAsset(config.css, config.output, copyLayoutFactory(config, callback));
			})
			.catch(callback);
	});
};
 
export default document;