-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-cli-docs.ts
215 lines (167 loc) · 5.32 KB
/
generate-cli-docs.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import fs from 'fs'
import path from 'path'
/**
* Standalone script to generate CLI documentation for SDK packages
* This creates MDX files in the /content/docs/clis directory
*/
/**
* Creates a _meta.ts file for a directory to control the order of items in the sidebar
*/
const createMetaFile = (dir: string, items: string[]) => {
const metaPath = path.join(dir, '_meta.ts')
let itemsStr = ''
for (const item of items) {
itemsStr += ` '${item}': '',\n`
}
const metaContent = `import type { MetaRecord } from 'nextra'
const meta: MetaRecord = {
${itemsStr}}
export default meta`
fs.writeFileSync(metaPath, metaContent)
}
/**
* Extract CLI commands and help text from a bin.ts file
*/
const extractCliCommands = (binFilePath: string): { commands: string[]; envVars: string[]; helpText: string } => {
const binContent = fs.readFileSync(binFilePath, 'utf8')
let helpText = ''
const helpMatch = binContent.match(/console\.log\(`\n([^`]+)`\)/s)
if (helpMatch && helpMatch[1]) {
helpText = helpMatch[1].trim()
}
const commands: string[] = []
const commandMatches = helpText.match(/\s\s(\w+)\s+([^\n]+)/g)
if (commandMatches) {
commandMatches.forEach((match) => {
const trimmed = match.trim()
if (trimmed && !trimmed.startsWith('--')) {
commands.push(trimmed.split(/\s+/)[0])
}
})
}
const envVars: string[] = []
const envVarMatches = helpText.match(/\s\s(\w+_API_KEY)\s+([^\n]+)/g)
if (envVarMatches) {
envVarMatches.forEach((match) => {
const envVar = match.trim().split(/\s+/)[0]
if (envVar) {
envVars.push(envVar)
}
})
}
return { commands, envVars, helpText }
}
/**
* Generate documentation for a single CLI package
*/
const generateCliDoc = async (packageDir: string, clisDir: string) => {
const packageJsonPath = path.join(packageDir, 'package.json')
if (!fs.existsSync(packageJsonPath)) {
console.log(`Skipping ${packageDir} - No package.json found`)
return
}
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
const { name, bin, description } = packageJson
if (!bin) {
console.log(`Skipping ${name} - No bin entry in package.json`)
return
}
const packageNameBase = name.replace('.do', '')
const binFile = Object.values(bin)[0]
if (!binFile) {
console.log(`Skipping ${name} - No bin file specified`)
return
}
const binFilePath = path.join(packageDir, 'src/bin.ts')
if (!fs.existsSync(binFilePath)) {
console.log(`Skipping ${name} - No bin.ts file found at ${binFilePath}`)
return
}
const { commands, envVars, helpText } = extractCliCommands(binFilePath)
const docContent = `---
title: ${name} CLI
sidebarTitle: ${packageNameBase}
sdk: true
cli: true
---
# ${name} CLI
${description || `Command-line interface for ${name}`}
## Installation
\`\`\`bash
npm install -g ${name}
# or
npx ${name} [command]
\`\`\`
## Usage
\`\`\`bash
${Object.keys(bin)[0]} [command] [options]
\`\`\`
## Commands
${commands.map((cmd) => `### \`${cmd}\`\n\n`).join('')}
## Environment Variables
${envVars.length > 0 ? envVars.map((env) => `- \`${env}\``).join('\n') : 'No environment variables documented.'}
## Help Output
\`\`\`
${helpText}
\`\`\`
`
const docPath = path.join(clisDir, `${packageNameBase}.mdx`)
fs.writeFileSync(docPath, docContent)
console.log(`Generated CLI documentation for ${name}`)
}
/**
* Generate an index page for the CLIs section
*/
const generateCliIndexPage = (clisDir: string, packageNames: string[]) => {
const indexContent = `---
title: CLI Reference
sidebarTitle: Overview
asIndexPage: true
---
# CLI Reference
This section contains documentation for command-line interfaces available in our SDK packages.
${packageNames.map((name) => `- [${name.replace('.do', '')}](/${name.replace('.do', '')})`).join('\n')}
`
const indexPath = path.join(clisDir, 'index.mdx')
fs.writeFileSync(indexPath, indexContent)
console.log('Generated CLI index page')
}
/**
* Main function to generate all CLI documentation
*/
const generateCliDocs = async () => {
try {
console.log('Generating CLI documentation for SDK packages...')
const clisDir = path.resolve(process.cwd(), 'content/docs/clis')
if (!fs.existsSync(clisDir)) {
fs.mkdirSync(clisDir, { recursive: true })
}
const sdksDir = path.resolve(process.cwd(), 'sdks')
const packageDirs = fs
.readdirSync(sdksDir)
.map((dir) => path.join(sdksDir, dir))
.filter((dir) => fs.statSync(dir).isDirectory())
const cliPackages: string[] = []
for (const packageDir of packageDirs) {
const packageJsonPath = path.join(packageDir, 'package.json')
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
if (packageJson.bin) {
await generateCliDoc(packageDir, clisDir)
cliPackages.push(packageJson.name)
}
}
}
generateCliIndexPage(clisDir, cliPackages)
const cliFiles = fs
.readdirSync(clisDir)
.filter((file) => file.endsWith('.mdx') && file !== 'index.mdx')
.map((file) => file.replace('.mdx', ''))
createMetaFile(clisDir, ['index', ...cliFiles])
console.log('CLI documentation generation complete')
} catch (error) {
console.error('Error generating CLI documentation:', error)
process.exit(1)
}
}
generateCliDocs()