This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
/
index.js
68 lines (56 loc) · 1.7 KB
/
index.js
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
const pipe = require('1-liners/pipe');
const {cyan} = require('chalk');
const tinyError = require('tiny-error');
const {isArray} = Array;
const prefix = `${cyan('[doxie-core]')} `;
const coreError = tinyError({prefix});
const pluginError = tinyError({
prefix: prefix + 'Got invalid data from a plugin. ',
});
export default (
plugins,
{stdout = null, stderr = null} = {} /* jshint ignore: line */
) => {
const writeOutput = (
stdout && typeof stdout.write === 'function' ?
stdout.write.bind(stdout) :
null
);
const writeError = (
stderr && typeof stderr.write === 'function' ?
stderr.write.bind(stderr) :
null
);
return plugins
.map((plugin) => (...args) => {
// TODO: Check if `plugin` is a function
let result = plugin(...args);
if (!result || typeof result !== 'object') throw pluginError(
'It should have returned a data object.'
);
let {version, docs, error, output} = result;
if (typeof version !== 'number') throw pluginError(
'The returned data should contain a `{Number} version`'
);
if (version !== 1) throw pluginError(
'The returned `version` should equal `1` in doxie <2.0.0'
);
if (!isArray(docs)) throw pluginError(
'The returned data should contain an `{Array} docs`'
);
if (output && writeOutput) writeOutput(output);
if (error && writeError) writeError(error);
return result;
})
.reduce(pipe, (input) => {
if (!isArray(input)) throw coreError(
'Wrong input data. Expecting an array.'
);
// TODO: Test this.
return {
docs: input.map((data) => ({data})),
version: 1,
};
})
;
};