forked from google/clasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocs.ts
79 lines (76 loc) · 2.56 KB
/
docs.ts
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
73
74
75
76
77
78
79
/**
* Generates the How To section of the docs.
* Ensures the code for clasp commands is well documented.
* @todo Generate a list of commands for the beginning of the README.
*/
const fs = require('fs');
const parseComments = require('parse-comments');
const extract = require('extract-comments');
const file = fs.readFileSync('./index.ts').toString();
const ucfirst = require('ucfirst');
// The README will be a concatenation of lines in this variable.
let readme = [
'## How To...',
];
// Remove first line (#!/usr/bin/env node)
const fileLines = file.split('\n');
fileLines.splice(0, 1);
const fileWithoutFirstLine = fileLines.join('\n');
// Extract JSDoc comments out of our file.
const comments = extract(fileWithoutFirstLine);
for (const command of comments) {
// To use the parseComments module, complete the stripped comment.
const comment = `/*${command.raw}*/`;
const claspCommand = parseComments(comment)[0];
// Only print valid commands.
if (claspCommand && claspCommand.description && claspCommand.name) {
readme.push('');
readme.push(`### ${ucfirst(claspCommand.name)}`);
readme.push('');
readme.push(claspCommand.description);
// Parameters (@param)
if (claspCommand.params && claspCommand.params.length) {
readme.push('');
readme.push('#### Options\n');
claspCommand.params.map(param => {
const isOptional = param.type.indexOf('?') !== -1;
// readme.push(JSON.stringify(param));
const paramName = param.parent || param.description.split(' ')[0];
if (isOptional) {
readme.push([
// `\`clasp ${claspCommand.name}`,
`- \`${paramName}\`:`,
param.description,
].join(' '));
} else {
// Required parameters descriptions aren't parsed by parse-comments. Parse them manually.
readme.push([
// `\`clasp ${claspCommand.name}`,
`- \`${paramName}\`:`,
param.description,
].join(' '));
}
});
}
// Examples (@example)
if (claspCommand.example) {
readme.push('');
readme.push('#### Examples\n');
const examples = claspCommand.example.split(',');
examples.map(example => {
readme.push(`- \`clasp ${example}\``);
});
}
// Extra Description (@desc)
if (claspCommand.desc) {
readme.push('');
const lines = claspCommand.desc.split('-');
lines.map((line, i) => {
let value = '';
if (i) value += '- ';
readme.push(value + line.trim());
});
}
}
}
console.log(readme.join('\n'));