1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | var path = require('path');
|
15 | var glob = require('globby');
|
16 | var async = require('async');
|
17 | var extend = require('extend-shallow');
|
18 | var tutil = require('template-utils');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | var requires = {};
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | module.exports = apidocs;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function 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 |
|
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 |
|
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 |
|
70 | apidocs.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 |