Skip to content

Commit 13cd493

Browse files
committed
Changed parseInput to parseFile.
Added parseFiles method. Added test.
1 parent 03cd890 commit 13cd493

File tree

4 files changed

+117
-26
lines changed

4 files changed

+117
-26
lines changed

DOCUMENTATION.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
### lib/doxdox.js
77

88

9-
#### parseInput(input, config)
9+
#### parseFile(input, config)
1010

11-
Parse an input file with parser.
11+
Parse a file with custom parser.
1212

13-
parseInput('src/main.js', {'parser': 'dox'}).then(files => {});
13+
parseFile('src/main.js', {'parser': 'dox'}).then(files => {});
1414

1515

1616

@@ -24,6 +24,33 @@ Parse an input file with parser.
2424

2525

2626

27+
##### Returns
28+
29+
30+
- `Object` Promise
31+
32+
33+
34+
#### parseFiles(inputs, config)
35+
36+
Parse array of files, and then render the parsed data through the defined layout plugin.
37+
38+
parseFiles(['src/main.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
39+
40+
41+
42+
43+
##### Parameters
44+
45+
- **inputs** `Array` Array of directory globs and/or files.
46+
- **config** `Object` Configuration object.
47+
- **config.ignore** `String` Array of paths to ignore.
48+
- **config.parser** `String` String representing the parser to be used.
49+
- **config.layout** `String` String representing the layout plugin to be used.
50+
51+
52+
53+
2754
##### Returns
2855

2956

@@ -35,7 +62,7 @@ Parse an input file with parser.
3562

3663
Parse array of directory globs and/or files, and then render the parsed data through the defined layout plugin.
3764

38-
parseInputs(['src/main.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
65+
parseInputs(['src/*.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
3966

4067

4168

lib/doxdox.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const globby = require('globby');
66

77
const loaders = require('./loaders');
88

9-
const formatPathsArrayToIgnore = require('./utils').formatPathsArrayToIgnore;
9+
const {
10+
formatPathsArrayToIgnore,
11+
setConfigDefaults
12+
} = require('./utils');
1013

1114
const DEFAULT_IGNORE_PATHS = [
1215
'!./{node_modules,bower_components,test,tests}/**',
@@ -17,9 +20,9 @@ const DEFAULT_IGNORE_PATHS = [
1720
const REPLACE_FILENAME_REGEXP = new RegExp(`^(${process.cwd()}/|./)`);
1821

1922
/**
20-
* Parse an input file with parser.
23+
* Parse a file with custom parser.
2124
*
22-
* parseInput('src/main.js', {'parser': 'dox'}).then(files => {});
25+
* parseFile('src/main.js', {'parser': 'dox'}).then(files => {});
2326
*
2427
* @param {String} input File to parse.
2528
* @param {Object} config Configuration object.
@@ -28,8 +31,8 @@ const REPLACE_FILENAME_REGEXP = new RegExp(`^(${process.cwd()}/|./)`);
2831
* @public
2932
*/
3033

31-
const parseInput = (input, config) =>
32-
loaders.loadParser(config).then(parser => new Promise((resolve, reject) => {
34+
const parseFile = (input, config) =>
35+
loaders.loadParser(setConfigDefaults(config)).then(parser => new Promise((resolve, reject) => {
3336

3437
fs.readFile(input, 'utf8', (err, data) => {
3538

@@ -50,10 +53,31 @@ const parseInput = (input, config) =>
5053

5154
}));
5255

56+
57+
/**
58+
* Parse array of files, and then render the parsed data through the defined layout plugin.
59+
*
60+
* parseFiles(['src/main.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
61+
*
62+
* @param {Array} inputs Array of directory globs and/or files.
63+
* @param {Object} config Configuration object.
64+
* @param {String} config.ignore Array of paths to ignore.
65+
* @param {String} config.parser String representing the parser to be used.
66+
* @param {String} config.layout String representing the layout plugin to be used.
67+
* @return {Object} Promise
68+
* @public
69+
*/
70+
71+
const parseFiles = (files, config) => loaders.loadPlugin(setConfigDefaults(config)).then(plugin =>
72+
Promise.all(files.map(input => parseFile(input, config)))
73+
.then(files => plugin(Object.assign({
74+
files
75+
}, setConfigDefaults(config)))));
76+
5377
/**
5478
* Parse array of directory globs and/or files, and then render the parsed data through the defined layout plugin.
5579
*
56-
* parseInputs(['src/main.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
80+
* parseInputs(['src/*.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
5781
*
5882
* @param {Array} inputs Array of directory globs and/or files.
5983
* @param {Object} config Configuration object.
@@ -64,17 +88,13 @@ const parseInput = (input, config) =>
6488
* @public
6589
*/
6690

67-
const parseInputs = (inputs, config) => loaders.loadPlugin(config).then(plugin =>
68-
globby(inputs.concat(
69-
DEFAULT_IGNORE_PATHS,
70-
formatPathsArrayToIgnore(config.ignore)
71-
), {'nodir': true}).then(files =>
72-
Promise.all(files.map(input => parseInput(input, config))))
73-
.then(files => plugin(Object.assign({
74-
files
75-
}, config))));
91+
const parseInputs = (inputs, config) => globby(inputs.concat(DEFAULT_IGNORE_PATHS,
92+
formatPathsArrayToIgnore(setConfigDefaults(config).ignore)
93+
), {'nodir': true}).then(files => parseFiles(files, config));
94+
7695

7796
module.exports = {
78-
parseInput,
97+
parseFile,
98+
parseFiles,
7999
parseInputs
80100
};

test/fixtures/doxdox.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
### lib/doxdox.js
66

77

8-
#### parseInput(input, config)
8+
#### parseFile(input, config)
99

10-
Parse an input file with parser.
10+
Parse a file with custom parser.
1111

12-
parseInput('src/main.js', {'parser': 'dox'}).then(files => {});
12+
parseFile('src/main.js', {'parser': 'dox'}).then(files => {});
1313

1414

1515

@@ -23,6 +23,33 @@ Parse an input file with parser.
2323

2424

2525

26+
##### Returns
27+
28+
29+
- `Object` Promise
30+
31+
32+
33+
#### parseFiles(inputs, config)
34+
35+
Parse array of files, and then render the parsed data through the defined layout plugin.
36+
37+
parseFiles(['src/main.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
38+
39+
40+
41+
42+
##### Parameters
43+
44+
- **inputs** `Array` Array of directory globs and/or files.
45+
- **config** `Object` Configuration object.
46+
- **config.ignore** `String` Array of paths to ignore.
47+
- **config.parser** `String` String representing the parser to be used.
48+
- **config.layout** `String` String representing the layout plugin to be used.
49+
50+
51+
52+
2653
##### Returns
2754

2855

@@ -34,7 +61,7 @@ Parse an input file with parser.
3461

3562
Parse array of directory globs and/or files, and then render the parsed data through the defined layout plugin.
3663

37-
parseInputs(['src/main.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
64+
parseInputs(['src/*.js'], {'ignore': [], 'parser': 'dox', 'layout': 'markdown'}).then(content => {});
3865

3966

4067

test/specs/doxdox.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ describe('doxdox', () => {
88
describe('parseInput', () => {
99

1010
it('parses input from file', () =>
11-
doxdox.parseInput('./lib/doxdox.js', {'parser': 'dox'}));
11+
doxdox.parseFile('./lib/doxdox.js', {'parser': 'dox'}));
1212

1313
});
1414

1515
describe('fail to parseInput on missing file', () => {
1616

1717
it('fails to parse input from invalid file', () =>
18-
doxdox.parseInput('test.js', {'parser': 'dox'}).catch(err => {
18+
doxdox.parseFile('test.js', {'parser': 'dox'}).catch(err => {
1919

2020
if (err) {
2121

@@ -27,6 +27,23 @@ describe('doxdox', () => {
2727

2828
});
2929

30+
describe('parseFiles', () => {
31+
32+
it('parses multiple input from array', () =>
33+
doxdox.parseFiles(['./lib/doxdox.js'], {
34+
'description': '',
35+
'ignore': [],
36+
'layout': 'markdown',
37+
'parser': 'dox',
38+
'title': 'Untitled Project'
39+
}).then(content => {
40+
41+
assert.equal(content, fs.readFileSync('./test/fixtures/doxdox.md', 'utf8'));
42+
43+
}));
44+
45+
});
46+
3047
describe('parseInputs', () => {
3148

3249
it('parses multiple input from array', () =>

0 commit comments

Comments
 (0)