-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocumentation.js
72 lines (59 loc) · 1.8 KB
/
documentation.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
69
70
71
72
const Promise = require('bluebird');
const path = require('path');
const fs = require('fs-promise');
const acorn = require('acorn-object-spread');
const doctrine = require('doctrine');
const filesToParse = [
'Model.js',
'ModelArray.js',
'Redink.js',
'Resource.js',
'ResourceArray.js',
];
const root = path.resolve(__dirname, 'src');
fs.readdir(root)
/**
* Extract the text from all the files to parse into an objet with the each key being the class
* name and each value being the extracted text.
*/
.then(files => {
const readFile = (file) => fs.readFile(`${root}/${file}`, { encoding: 'utf8' });
const isFileToParse = (file) => filesToParse.includes(file);
return Promise.props(
files.filter(isFileToParse).reduce((prev, next) => ({
...prev,
[next.split('.')[0]]: readFile(next),
}), {})
);
})
/**
* Convert the text of every class into an array of its JSDoc ASTs.
*/
.then(classes => {
const classesWithComments = { ...classes };
Object.keys(classesWithComments).forEach(className => {
const comments = [];
acorn.parse(classesWithComments[className], {
sourceType: 'module',
ecmaVersion: 8,
plugins: { objectSpread: true },
onComment: (block, text) => block && comments.push(text),
});
classesWithComments[className] = comments.map(comment =>
doctrine.parse(comment, {
unwrap: true,
sloppy: true,
}),
);
});
return classesWithComments;
})
/**
* Write the JSON file to disk.
*/
.then(classesWithComments => {
const file = JSON.stringify(classesWithComments);
return fs.writeFile(path.resolve(__dirname, 'docs/src/api/api.json'), file);
})
.then(() => console.log('Done creating docs!'))
.catch(console.error);