All files / tasks typedoc.ts

100% Statements 38/38
73.68% Branches 14/19
100% Functions 5/5
100% Lines 38/38
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  42x 42x 42x 42x 42x               3x 3x 21x   18x 18x   18x 12x       3x     42x 42x   3x   3x 3x       3x     3x 3x 3x     3x   3x 2x 2x 2x   2x 2x 2x 2x       2x 2x 2x     2x 2x 1x     1x            
import ITask = grunt.task.ITask;
import { config, touch, cp, rm } from 'shelljs';
import { exec } from './util/process';
import Publisher from './util/Publisher';
import { join } from 'path';
import { existsSync } from 'fs';
 
/**
 * Build command line arguments for typedoc from grunt options
 * @param options grunt options
 * @return {string[]} command line arguments array
 */
function typedocOptions(options: any) {
	const args: string[] = [];
	Object.keys(options).filter(key => {
		return key !== 'publishOptions';
	}).forEach(key => {
		Eif (options[key]) {
			args.push(`--${key}`);
 
			if (typeof options[key] !== 'boolean') {
				args.push(`"${options[key]}"`);
			}
		}
	});
	return args;
}
 
export = function (grunt: IGrunt) {
	grunt.registerTask('typedoc', function (this: ITask) {
		// Throw when any shelljs command fails
		config.fatal = true;
 
		const options: any = this.options({});
		const publishOptions = Object.assign({
			log: grunt.log,
			subDirectory: ''
		}, options.publishOptions || {});
		options.out = grunt.option<string>('doc-dir') || options.out || grunt.config.get<string>('apiDocDirectory');
 
		// Use project-local typedoc
		const typedoc = require.resolve('typedoc/bin/typedoc');
		grunt.log.writeln(`Building API Docs to "${ options.out }"`);
		exec(`node "${ typedoc }" ${ typedocOptions(options).join(' ') }`);
 
		// Publish
		const publishMode = (typeof publishOptions.publishMode === 'function') ? publishOptions.publishMode() :
			publishOptions.publishMode;
		if (publishMode) {
			const cloneDir = grunt.config.get<string>('apiPubDirectory');
			const publisher = new Publisher(cloneDir, publishOptions);
			publisher.init();
 
			const apiDocTarget = join(cloneDir, publishOptions.subDirectory);
			grunt.log.writeln(`copying ${ options.out } to ${ apiDocTarget }`);
			rm('-rf', apiDocTarget);
			cp('-r', options.out, apiDocTarget);
 
			// Add a .nojekyll file to prevent GitHub pages from trying to parse files starting with an underscore
			// @see https://github.com/blog/572-bypassing-jekyll-on-github-pages
			const nojekyll = join(cloneDir, '.nojekyll');
			Eif (!existsSync(nojekyll)) {
				touch(nojekyll);
			}
 
			Eif (publisher.commit()) {
				if (publishMode === 'publish') {
					publisher.publish();
				}
				else {
					grunt.log.writeln('Only committing -- skipping push to repo');
				}
			}
		}
	});
};