|
| 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 | + |
0 commit comments