1 | ;
|
2 |
|
3 | const fs = require('fs');
|
4 |
|
5 | const globby = require('globby');
|
6 |
|
7 | const loaders = require('./loaders');
|
8 |
|
9 | const formatPathsArrayToIgnore = require('./utils').formatPathsArrayToIgnore;
|
10 | const setConfigDefaults = require('./utils').setConfigDefaults;
|
11 |
|
12 | const DEFAULT_IGNORE_PATHS = [
|
13 | '!./{node_modules,bower_components,test,tests}/**',
|
14 | '!./Gruntfile.js',
|
15 | '!./Gulpfile.js'
|
16 | ];
|
17 |
|
18 | const REPLACE_FILENAME_REGEXP = new RegExp(`^(${process.cwd()}/|./)`, 'u');
|
19 |
|
20 | /**
|
21 | * Parse a file with custom parser.
|
22 | *
|
23 | * parseFile('src/main.js', {'parser': 'dox'}).then(files => {});
|
24 | *
|
25 | * @param {String} input File to parse.
|
26 | * @param {Object} config Configuration object.
|
27 | * @param {String} config.parser String representing the parser to be used.
|
28 | * @return {Object} Promise
|
29 | * @public
|
30 | */
|
31 |
|
32 | const parseFile = (input, config) =>
|
33 | loaders.loadParser(setConfigDefaults(config)).then(parser =>
|
34 | new Promise((resolve, reject) => {
|
35 |
|
36 | fs.readFile(input, 'utf8', (err, data) => {
|
37 |
|
38 | if (err) {
|
39 |
|
40 | return reject(err);
|
41 |
|
42 | }
|
43 |
|
44 | const filename = input.replace(REPLACE_FILENAME_REGEXP, '');
|
45 |
|
46 | return resolve({
|
47 | 'methods': parser(data, filename),
|
48 | 'name': filename
|
49 | });
|
50 |
|
51 | });
|
52 |
|
53 | }));
|
54 |
|
55 | /**
|
56 | * Parse array of files, and then render the parsed data through the defined layout plugin.
|
57 | *
|
58 | * parseFiles(['src/main.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
|
59 | *
|
60 | * @param {Array} inputs Array of directory globs and/or files.
|
61 | * @param {Object} config Configuration object.
|
62 | * @param {String} config.ignore Array of paths to ignore.
|
63 | * @param {String} config.parser String representing the parser to be used.
|
64 | * @param {String} config.layout String representing the layout plugin to be used.
|
65 | * @return {Object} Promise
|
66 | * @public
|
67 | */
|
68 |
|
69 | const parseFiles = (files, config) =>
|
70 | loaders.loadPlugin(setConfigDefaults(config)).then(plugin =>
|
71 | Promise.all(files.map(input => parseFile(input, config))).then(files =>
|
72 | plugin({
|
73 | 'files': files.filter(file => file.methods.length),
|
74 | ...setConfigDefaults(config)
|
75 | })));
|
76 |
|
77 | /**
|
78 | * Parse array of directory globs and/or files, and then render the parsed data through the defined layout plugin.
|
79 | *
|
80 | * parseInputs(['src/*.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
|
81 | *
|
82 | * @param {Array} inputs Array of directory globs and/or files.
|
83 | * @param {Object} config Configuration object.
|
84 | * @param {String} config.ignore Array of paths to ignore.
|
85 | * @param {String} config.parser String representing the parser to be used.
|
86 | * @param {String} config.layout String representing the layout plugin to be used.
|
87 | * @return {Object} Promise
|
88 | * @public
|
89 | */
|
90 |
|
91 | const parseInputs = (inputs, config) =>
|
92 | globby(inputs.concat(
|
93 | DEFAULT_IGNORE_PATHS,
|
94 | formatPathsArrayToIgnore(setConfigDefaults(config).ignore)
|
95 | )).then(files => parseFiles(files, config));
|
96 |
|
97 | module.exports = {
|
98 | parseFile,
|
99 | parseFiles,
|
100 | parseInputs
|
101 | };
|