2022-04-15 15:19:05 +00:00
|
|
|
import { readFile, writeFile, mkdir } from 'node:fs/promises'
|
|
|
|
import { dirname } from 'node:path'
|
2021-08-11 21:48:33 +00:00
|
|
|
import type { Schema } from 'untyped'
|
2021-10-11 12:57:54 +00:00
|
|
|
import { resolve } from 'pathe'
|
|
|
|
import { upperFirst } from 'scule'
|
2021-08-11 21:48:33 +00:00
|
|
|
|
|
|
|
export async function main () {
|
|
|
|
const rootDir = resolve(__dirname, '..')
|
2022-10-06 09:15:30 +00:00
|
|
|
const configTemplate = resolve(__dirname, 'nuxt-config.md')
|
|
|
|
const configFile = resolve(rootDir, 'content/3.api/6.configuration/nuxt-config.md')
|
2021-10-11 17:45:19 +00:00
|
|
|
await generateDocs({ configFile, configTemplate })
|
2021-08-11 21:48:33 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 15:45:04 +00:00
|
|
|
function generateMarkdown (schema: Schema, title: string, level: string) {
|
2021-08-11 21:48:33 +00:00
|
|
|
const lines: string[] = []
|
|
|
|
|
|
|
|
// Skip private
|
|
|
|
if (schema.tags?.includes('@private')) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render heading
|
|
|
|
lines.push(`${level} ${title}`, '')
|
|
|
|
|
|
|
|
// Render meta info
|
|
|
|
if (schema.type !== 'object' || !schema.properties) {
|
|
|
|
// Type and default
|
2021-11-19 16:26:15 +00:00
|
|
|
if (schema.type !== 'any') {
|
|
|
|
lines.push(`- **Type**: \`${schema.type}\``)
|
|
|
|
}
|
|
|
|
const defaultValue = formatValue(schema.default)
|
2022-04-06 05:56:08 +00:00
|
|
|
if (defaultValue && defaultValue.length) {
|
|
|
|
if (defaultValue.length === 1) {
|
|
|
|
lines.push(`- **Default:** ${defaultValue[0]}`)
|
|
|
|
} else {
|
|
|
|
lines.push('- **Default**', ...defaultValue)
|
|
|
|
}
|
2021-08-11 21:48:33 +00:00
|
|
|
}
|
2021-11-19 16:26:15 +00:00
|
|
|
|
2022-04-06 05:56:08 +00:00
|
|
|
// lines.push(`- **Version**: ${versions.join(', ')}`)
|
2021-11-19 16:26:15 +00:00
|
|
|
|
2021-08-11 21:48:33 +00:00
|
|
|
lines.push('')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render title
|
|
|
|
if (schema.title) {
|
|
|
|
lines.push('> ' + schema.title, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render description
|
|
|
|
if (schema.description) {
|
|
|
|
lines.push(schema.description, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render @ tags
|
|
|
|
if (schema.tags) {
|
|
|
|
lines.push(...schema.tags.map(renderTag).flat())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render properties
|
|
|
|
if (schema.type === 'object') {
|
|
|
|
const keys = Object.keys(schema.properties || {}).sort()
|
|
|
|
for (const key of keys) {
|
|
|
|
const val = schema.properties[key] as Schema
|
2022-10-27 15:45:04 +00:00
|
|
|
const propLines = generateMarkdown(val, `\`${key}\``, level + '#')
|
2021-08-11 21:48:33 +00:00
|
|
|
if (propLines.length) {
|
|
|
|
lines.push('', ...propLines)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
|
|
|
const TAG_REGEX = /^@([\d\w]+)[\s\n]/i
|
|
|
|
|
|
|
|
const TagAlertType = {
|
2021-10-11 12:57:54 +00:00
|
|
|
note: 'info',
|
2021-08-11 21:48:33 +00:00
|
|
|
warning: 'warning',
|
2021-10-11 12:57:54 +00:00
|
|
|
deprecated: 'danger'
|
2021-08-11 21:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const InternalTypes = new Set([
|
|
|
|
'version',
|
|
|
|
'deprecated'
|
|
|
|
])
|
|
|
|
|
|
|
|
function formatValue (val) {
|
2021-11-19 16:26:15 +00:00
|
|
|
const stringified = JSON.stringify(val, null, 2)
|
2022-04-06 05:56:08 +00:00
|
|
|
if (!stringified || stringified === '{}' || stringified === '[]') { return null }
|
|
|
|
if (stringified.includes('\n')) {
|
|
|
|
return ['```json', stringified, '```']
|
|
|
|
} else {
|
|
|
|
return ['`' + stringified + '`']
|
|
|
|
}
|
2021-08-11 21:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderTag (tag: string) {
|
|
|
|
const type = tag.match(TAG_REGEX)?.[1]
|
|
|
|
if (!type) {
|
|
|
|
return [`<!-- ${tag} -->`]
|
|
|
|
}
|
|
|
|
if (InternalTypes.has(type)) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
tag = tag.replace(`@${type}`, `**${upperFirst(type)}**:`)
|
|
|
|
if (TagAlertType[type]) {
|
|
|
|
return [`::alert{type="${TagAlertType[type]}"}`, tag, '::', '']
|
|
|
|
}
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
2021-10-11 17:45:19 +00:00
|
|
|
async function generateDocs ({ configFile, configTemplate }) {
|
2021-10-11 12:57:54 +00:00
|
|
|
const GENERATE_KEY = '<!-- GENERATED_CONFIG_DOCS -->'
|
2021-08-11 21:48:33 +00:00
|
|
|
// Prepare content directory
|
|
|
|
const start = Date.now()
|
2021-10-11 12:57:54 +00:00
|
|
|
console.log(`Updating docs on ${configFile}`)
|
2021-10-11 17:45:19 +00:00
|
|
|
const template = await readFile(configTemplate, 'utf8')
|
2021-11-21 16:14:46 +00:00
|
|
|
const rootSchema = require('../../packages/schema/schema/config.schema.json') as Schema
|
2021-08-11 21:48:33 +00:00
|
|
|
const keys = Object.keys(rootSchema.properties).sort()
|
2021-10-11 12:57:54 +00:00
|
|
|
let generatedDocs = ''
|
|
|
|
|
2021-10-11 17:45:19 +00:00
|
|
|
if (!template.includes(GENERATE_KEY)) {
|
2021-10-11 12:57:54 +00:00
|
|
|
throw new Error(`Could not find ${GENERATE_KEY} in ${configFile}`)
|
|
|
|
}
|
2021-08-11 21:48:33 +00:00
|
|
|
|
2021-10-11 12:57:54 +00:00
|
|
|
// Generate each section
|
2021-08-11 21:48:33 +00:00
|
|
|
for (const key of keys) {
|
|
|
|
const schema = rootSchema.properties[key]
|
|
|
|
|
2021-10-11 12:57:54 +00:00
|
|
|
const lines = generateMarkdown(schema, key, '##')
|
2021-08-11 21:48:33 +00:00
|
|
|
|
|
|
|
// Skip empty sections
|
|
|
|
if (lines.length < 3) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-11 12:57:54 +00:00
|
|
|
// Add lines to new file content
|
|
|
|
generatedDocs += lines.join('\n') + '\n'
|
2021-08-11 21:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 17:45:19 +00:00
|
|
|
const body = template.replace(GENERATE_KEY, generatedDocs)
|
2022-04-06 05:56:08 +00:00
|
|
|
await mkdir(dirname(configFile), { recursive: true })
|
2021-10-11 12:57:54 +00:00
|
|
|
await writeFile(configFile, body)
|
2021-08-11 21:48:33 +00:00
|
|
|
|
|
|
|
console.log(`Generate done in ${(Date.now() - start) / 1000} seconds!`)
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|