Skip to content

Commit 99029e2

Browse files
committed
Add API documentation generation
1 parent ffe8c31 commit 99029e2

3 files changed

Lines changed: 111 additions & 20 deletions

File tree

functions/functions.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1+
const categories = [
2+
{ name: 'array' },
3+
{ name: 'boolean' },
4+
{ name: 'cluster' },
5+
{ name: 'display' },
6+
{ name: 'events' },
7+
{ name: 'groups' },
8+
{ name: 'help' },
9+
{ name: 'math' },
10+
{ name: 'persons' },
11+
{ name: 'sheets' },
12+
{ name: 'staff' },
13+
{ name: 'table' },
14+
{ name: 'time' },
15+
{ name: 'tuple' },
16+
{ name: 'udf' },
17+
{ name: 'util' },
18+
{ name: 'wcif', docs: 'This category gathers all functions regarding high level WCIF manipulation' },
19+
]
20+
121
module.exports = {
2-
allFunctions:
3-
[].concat(
4-
require('./array').functions,
5-
require('./boolean').functions,
6-
require('./cluster').functions,
7-
require('./display').functions,
8-
require('./events').functions,
9-
require('./groups').functions,
10-
require('./help').functions,
11-
require('./math').functions,
12-
require('./persons').functions,
13-
require('./sheets').functions,
14-
require('./staff').functions,
15-
require('./table').functions,
16-
require('./time').functions,
17-
require('./tuple').functions,
18-
require('./udf').functions,
19-
require('./util').functions,
20-
require('./wcif').functions,
21-
)
22+
categories,
23+
allFunctions: categories.flatMap(c => require(`./${c.name}`).functions)
2224
}

gen_docs_api.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const dotenv = require('dotenv')
2+
const env = process.env.ENV || 'DEV'
3+
dotenv.config({ path: '.env.' + env })
4+
5+
const functions = require('./functions/functions')
6+
const categories = functions.categories.filter(c => c.name !== 'display')
7+
8+
const functionToAnchorName = (name, genericParams) => {
9+
if (typeof this.generated === 'undefined') {
10+
this.generated = []
11+
}
12+
const baseName = `${name.toLowerCase()}${genericParams ? genericParams.map(t => t.toLowerCase()).join('-') : ''}`
13+
let suffix = 1
14+
let generatedName = baseName
15+
while (this.generated.includes(generatedName)) {
16+
generatedName = `${baseName}-${suffix++}`
17+
}
18+
this.generated.push(generatedName)
19+
return generatedName
20+
}
21+
22+
const functionName = (name, genericParams) => `${name}${genericParams ? `\\<${genericParams.join(', ')}>` : ''}`
23+
24+
const stringForArg = ({ name, type, defaultValue, nullable, canBeExternal, repeated, lazy, docs }) => {
25+
const properties = []
26+
if (nullable) {
27+
properties.push('(can be null)')
28+
}
29+
if (canBeExternal) {
30+
properties.push('(can be external)')
31+
}
32+
if (repeated) {
33+
properties.push('(variadic)')
34+
}
35+
if (lazy) {
36+
properties.push('(lazy evaluated)')
37+
}
38+
const propertiesString = properties.length > 0 ? ` *${properties.join('')}*` : ''
39+
const helpForArg = docs ? `
40+
41+
${docs}` : ''
42+
43+
return ` - *${type.replace('<', '\\<')}* **${name}**${defaultValue !== undefined ? `=${defaultValue}`:''}${propertiesString}${helpForArg}`
44+
}
45+
46+
const stringForFunction = ({ name, genericParams, docs, outputType, args, mutations}) => `
47+
### ${functionName(name, genericParams)}
48+
49+
${docs || 'TODO'}
50+
51+
- Args:${args.length === 0 ? ' none' : `\n${args.map(stringForArg).join('\n')}`}
52+
53+
- Returns: **${outputType.replace('<', '\\<')}**
54+
55+
- WCIF changes: **${mutations && mutations.length > 0 ? `${mutations.join(', ')}` : 'none'}**`
56+
57+
const documentation = categories.map(({ name, docs }) => {
58+
const functions = require(`./functions/${name}.js`).functions
59+
return `## ${name}
60+
61+
${docs || 'TODO'}
62+
63+
${functions.map(stringForFunction).join('\n')}
64+
`
65+
}).join('\n')
66+
67+
const tocFunction = ({ name, genericParams }) => ` - [${functionName(name, genericParams)}](#${functionToAnchorName(name, genericParams)})`
68+
const toc = categories.map(({ name }) => {
69+
const functions = require(`./functions/${name}.js`).functions
70+
return ` - [${name}](#${name})
71+
${functions.map(tocFunction).join('\n')}`
72+
}).join('\n')
73+
74+
const { exec } = require('node:child_process')
75+
76+
exec('git log -1 --oneline | cut -d " " -f 1 | tr -d "\n"', (_, stdout ) => {
77+
78+
console.log(`# CompScript API reference
79+
80+
*This documentation was automatically generated on commit [${stdout}](https://github.com/cubingusa/compscript/commit/${stdout}) with \`npm run gen-docs-api\`, don't edit this file directly.*
81+
82+
## Index
83+
84+
${toc}
85+
86+
${documentation}`)
87+
})
88+

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"main": "main.js",
2020
"scripts": {
2121
"dev-server": "ENV=DEV npx nodemon -i wcif_data .",
22+
"gen-docs-api": "node ./gen_docs_api.js > docs/api.md",
2223
"test": "echo \"Error: no test specified\" && exit 1"
2324
},
2425
"repository": {

0 commit comments

Comments
 (0)