UNPKG

2.11 kBJavaScriptView Raw
1/*!
2 * helper-apidocs <https://github.com/ * Module dependencies/helper-apidocs>
3 *
4 * Copyright (c) 2014 Jon Schlinkert, contributors.
5 * Licensed under the MIT license.
6 */
7
8'use strict';
9
10/**
11 * Module dependencies
12 */
13
14var path = require('path');
15var glob = require('globby');
16var async = require('async');
17var extend = require('extend-shallow');
18var tutil = require('template-utils');
19
20/**
21 * Requires cache
22 */
23
24var requires = {};
25
26/**
27 * Expose `apidocs` helper
28 */
29
30module.exports = apidocs;
31
32/**
33 * `apidocs` helper.
34 */
35
36function apidocs(patterns, options, cb) {
37 if (typeof options === 'function') {
38 cb = options;
39 options = {};
40 }
41
42 var opts = extend({sep: '\n', dest: 'README.md'}, options);
43 var appOpts = this && this.app && this.app.options;
44 var ctx = extend({}, opts, appOpts, this && this.context);
45
46 // assemble compatibility
47 if (ctx.dest && typeof ctx.dest === 'object') {
48 ctx.dest = ctx.dest.dirname || ctx.dest.path;
49 }
50
51 var dest = ctx.dest ? path.resolve(ctx.dest) : '.';
52
53 opts.cwd = ctx.filepath
54 ? path.dirname(ctx.filepath)
55 : process.cwd();
56
57 // we can't pass the `ctx` object to glob because it bugs out
58 glob(patterns, opts, function(err, files) {
59 async.mapSeries(files, function(fp, next) {
60 var jscomments = requires.jscomments || (requires.jscomments = require('js-comments'));
61 var res = jscomments(fp, dest, opts);
62 next(null, tutil.headings(res));
63 }, function (err, arr) {
64 if (err) return cb(err);
65 cb(null, arr.join('\n'));
66 });
67 });
68}
69
70apidocs.sync = function(patterns, options) {
71 var appOpts = this && this.app && this.app.options;
72 var opts = extend({sep: '\n', dest: 'README.md'}, appOpts, options);
73 var ctx = extend({}, this && this.context);
74 var dest = path.resolve(ctx.dest || opts.dest);
75
76 opts.cwd = ctx.filepath
77 ? path.dirname(ctx.filepath)
78 : process.cwd();
79
80 return glob.sync(patterns, opts).map(function (fp) {
81 var jscomments = requires.jscomments || (requires.jscomments = require('js-comments'));
82 return tutil.headings(jscomments(fp, dest, opts));
83 }).join('\n');
84};
\No newline at end of file