All files configure.js

83.33% Statements 25/30
59.09% Branches 13/22
75% Functions 3/4
83.33% Lines 25/30

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 86 87 88 89                            1x 3x 24x 3x   24x           1x     2x               2x     1x 1x       1x     1x 1x   1x 1x     1x   1x   2x       2x 2x   1x 1x     2x             2x               2x        
// Configuration
// =============
// For any particular run of Docco we might use a passed-in external template,
// or one of the built-in **layouts**. Plus we have to set up a few internal options.
 
import template from "lodash.template";
import fs from "fs-extra";
import path from "path";
 
import defaults from "./defaults";
import {getLanguage} from "./languages";
 
// This is a simple way of picking which keys from an object you want returned.
// eg: `pick({foo: "bar", a: "b", c: "d"}, "foo", "c") === {foo: "bar", c: "d"}`
export const pick = (obj, ...keys) => {
	return keys.reduce((acc, key) => {
		if (obj.hasOwnProperty(key)) {
			acc[key] = obj[key];
		}
		return acc;
	}, {});
};
 
 
// This function builds the main configuration object.
export const configure = function(options) {
	// First we set up the base config object.
	// We use the defaults and then any keys from defaults that are in the options object.
	const config = {
		...defaults,
		...pick(options, ...Object.keys(defaults))
	};
	// The user is able to override the layout file used with the `--template` parameter.
	// In this case, it is also neccessary to explicitly specify a stylesheet file.
	// These custom templates are compiled exactly like the predefined ones, but the `public` folder
	// is only copied for the latter.
	if (options.template) {
		// If a custom template was provided we will use it but usually
		// we expect a custom css file too. However there is no *requirement* for this.
		Eif (!options.css) {
			global.console.warn("docco: have a template but no stylesheet file specified.");
		}
		// Templates take precidence, so if we're using a template there is no layout.
		// Lets make sure of that.
		config.layout = null;
	} else {
		// We're using a layout. So lets save the layout name before moving on.
		config.layoutname = config.layout;
		const dir = config.layout = path.join(__dirname, "../resources", config.layout);
		// If the layout has a public directory (for resources like css, images, etc) lets set that.
		Eif (fs.existsSync(path.join(dir, "public"))) {
			config.public = path.join(dir, "public");
		}
		// Templates are always named the same thing.
		config.template = path.join(dir, "docco.jst");
		// Note that a custom css file can be provided to override the layouts own.
		config.css = options.css || path.join(dir, "docco.css");
	}
	Iif (config.zip) {
 
	}
	// Now we try to load everything we set up above.
	try {
		config.template = template(fs.readFileSync(config.template).toString());
	} catch (err) {
		global.console.warn(`docco: could not load layout template "${config.layoutname || config.template}"`);
		config.template = false;
	}
	// If we're using custom markdown options we load those from a file now.
	Iif (options.marked) {
		config.marked = JSON.parse(fs.readFileSync(options.marked));
	}
	// We only support what we can read.
	// If docco is called with `docco *.*` we don't want to try to read `.gif` files or something.
	// So lets filter out those langauges we don't support.
	// Want to add a language? [Submit a PR](https://github.com/abritinthebay/docco-es)!
	config.sources = Array.isArray(options.args) ? options.args.filter(function(source) {
		const lang = getLanguage(source, config);
		if (!lang && config.verbose) {
			global.console.warn(`docco: skipped unknown type (${path.basename(source)})`);
		}
		return lang;
	}).sort() : [];
	// We're done! Return the completed configuration object.
	return config;
};
 
export default configure;