-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbuild.ts
More file actions
129 lines (104 loc) · 3.68 KB
/
build.ts
File metadata and controls
129 lines (104 loc) · 3.68 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint-disable no-console */
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import * as metadata from '@vueuse/metadata'
import { getTypeDefinition } from './vueuse/scripts/utils'
type FunctionInvocation = 'AUTO' | 'EXTERNAL' | 'EXPLICIT_ONLY'
interface FunctionReference {
name: string
description: string
reference: string
}
const SKILL_DIR = './skills/vueuse-functions'
const SKILL_REFERENCE_DIR = './references'
const SKILLS_TEMPLATE_PATH = './templates/vueuse-functions-skills.md'
const VUEUSE_ROOT = './vueuse'
const EXPLICIT_ONLY_FUNCTIONS = new Set([
'get',
'set',
'toRef',
])
;(async () => {
const categories = await prepareFunctionReferences(SKILL_DIR)
const functionsTable = prepareFunctionsTable(categories)
// Generate main skills markdown
let templateContent = readFileSync(SKILLS_TEMPLATE_PATH, 'utf-8')
templateContent = templateContent.replace('<!-- FUNCTIONS_TABLE_PLACEHOLDER -->', functionsTable)
const outputPath = path.join(SKILL_DIR, 'SKILL.md')
writeFileSync(outputPath, templateContent)
console.log(`Generated skills documentation at: ${outputPath}`)
})()
// Utils
async function prepareFunctionReferences(outDir: string, referenceDir = SKILL_REFERENCE_DIR): Promise<Record<string, FunctionReference[]>> {
mkdirSync(path.join(outDir, referenceDir), { recursive: true })
const categories: Record<string, FunctionReference[]> = {}
for (const catagory of metadata.categoryNames) {
if (catagory.startsWith('_'))
continue
const refs: FunctionReference[] = []
const functions = metadata.functions.filter(i => i.category === catagory && !i.internal)
for (const fn of functions) {
const description = toTitleCase(fn.description?.replace(/\|/g, '\\|') ?? '')
if (fn.external) {
refs.push({ name: fn.name, description, reference: fn.external })
continue
}
const docPath = path.join(VUEUSE_ROOT, `packages/${fn.package}/${fn.name}/index.md`)
if (existsSync(docPath)) {
const outputPath = path.join(referenceDir, `${fn.name}.md`)
const docContent = await genFunctionReference(fn.package, fn.name, docPath)
writeFileSync(path.join(outDir, outputPath), docContent)
refs.push({ name: fn.name, description, reference: outputPath })
}
else {
console.warn(`Missing doc: ${fn.name}`)
}
}
categories[catagory] = refs
}
return categories
}
function prepareFunctionsTable(categories: Record<string, FunctionReference[]>) {
let table = ''
for (const [category, functions] of Object.entries(categories)) {
table += `### ${category}\n\n`
table += `| Function | Description | Invocation |\n`
table += `|----------|-------------|------------|\n`
for (const fn of functions) {
const invocation = resolveFunctionInvocation(fn.name, category)
table += `| [\`${fn.name}\`](${fn.reference}) | ${fn.description} | ${invocation} |\n`
}
table += `\n`
}
return table
}
function resolveFunctionInvocation(name: string, category: string): FunctionInvocation {
if (EXPLICIT_ONLY_FUNCTIONS.has(name)) {
return 'EXPLICIT_ONLY'
}
if (category.startsWith('@')) {
return 'EXTERNAL'
}
return 'AUTO'
}
function toTitleCase(str: string): string {
if (!str)
return str
const first = str[0]
if (first < 'a' || first > 'z')
return str
return first.toUpperCase() + str.slice(1)
}
async function genFunctionReference(pkg: string, name: string, mdPath: string) {
const md = readFileSync(mdPath, 'utf-8')
const types = await getTypeDefinition(pkg, name)
if (types) {
return `${md}
## Type Declarations
\`\`\`ts
${types}
\`\`\`
`
}
return md
}