Skip to content

Commit

Permalink
Add API documentation generation
Browse files Browse the repository at this point in the history
  • Loading branch information
viroulep committed Nov 27, 2023
1 parent 3cf94e1 commit 71bd62e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
106 changes: 106 additions & 0 deletions gen_docs_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const dotenv = require('dotenv')
const env = process.env.ENV || 'DEV'
dotenv.config({ path: '.env.' + env })

// FIXME: reorder categories into a more structured order?
// eg: wcif, events, groups, persons, cluster, ...?
const categories = [
'array',
'boolean',
'cluster',
// FIXME: not that All useful.
//'display',
'events',
'groups',
'help',
'math',
'persons',
'sheets',
'staff',
'table',
'time',
'tuple',
'udf',
'util',
'wcif',
]

const functionToAnchorName = (name, genericParams) => {
if (typeof this.generated === 'undefined') {
this.generated = []
}
const baseName = `${name.toLowerCase()}${genericParams ? genericParams.map(t => t.toLowerCase()).join('-') : ''}`
let suffix = 1
let generatedName = baseName
while (this.generated.includes(generatedName)) {
generatedName = `${baseName}-${suffix++}`
}
this.generated.push(generatedName)
return generatedName
}

const functionName = (name, genericParams) => `${name}${genericParams ? `\\<${genericParams.join(', ')}>` : ''}`

const stringForArg = ({ name, type, defaultValue, nullable, canBeExternal, repeated, lazy, docs }) => {
const properties = []
// FIXME: document what lazy/repeated means.
if (nullable) {
properties.push('(can be null)')
}
if (canBeExternal) {
properties.push('(can be external)')
}
if (repeated) {
properties.push('(repeated)')
}
if (lazy) {
properties.push('(lazy)')
}
const propertiesString = properties.length > 0 ? ` *${properties.join('')}*` : ''
const helpForArg = docs ? `
${docs}` : ''

return ` - *${type.replace('<', '\\<')}* **${name}**${defaultValue !== undefined ? `=${defaultValue}`:''}${propertiesString}${helpForArg}`
}

const stringForFunction = ({ name, genericParams, docs, outputType, args, mutations}) => `
### ${functionName(name, genericParams)}
${docs || 'TODO'}
- Args:${args.length === 0 ? ' none' : `\n${args.map(stringForArg).join('\n')}`}
- Returns: **${outputType.replace('<', '\\<')}**
- WCIF changes: **${mutations && mutations.length > 0 ? `${mutations.join(', ')}` : 'none'}**`

const documentation = categories.map(c => {
const functions = require(`./functions/${c}.js`).functions
return `## ${c}
${functions.map(stringForFunction).join('\n')}
`
}).join('\n')

const tocFunction = ({ name, genericParams }) => ` - [${functionName(name, genericParams)}](#${functionToAnchorName(name, genericParams)})`
const toc = categories.map(c => {
const functions = require(`./functions/${c}.js`).functions
return ` - [${c}](#${c})
${functions.map(tocFunction).join('\n')}`
}).join('\n')

const { exec } = require('node:child_process')

exec('git log -1 --oneline | cut -d " " -f 1 | tr -d "\n"', (_, stdout ) => {

console.log(`# CompScript API reference
*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.*
## Index
${toc}
${documentation}`)
})

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"main": "main.js",
"scripts": {
"dev-server": "ENV=DEV npx nodemon -i wcif_data .",
"gen-docs-api": "node ./gen_docs_api.js > docs/api.md",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down

0 comments on commit 71bd62e

Please sign in to comment.