|
| 1 | +#!/usr/bin/env npx tsx |
| 2 | +/** |
| 3 | + * 从 references/ 目录提取所有 TCB API Action 列表 |
| 4 | + * 生成 action-list.ts 文件 |
| 5 | + */ |
| 6 | + |
| 7 | +import * as fs from 'fs'; |
| 8 | +import * as path from 'path'; |
| 9 | + |
| 10 | +const REFERENCES_DIR = path.join(__dirname, '../config/.claude/skills/cloudbase-api-direct/references'); |
| 11 | +const OUTPUT_FILE = path.join(__dirname, '../config/.claude/skills/cloudbase-api-direct/scripts/lib/src/action-list.ts'); |
| 12 | + |
| 13 | +async function extractActions(): Promise<string[]> { |
| 14 | + // 多种匹配模式 |
| 15 | + const patterns = [ |
| 16 | + /X-TC-Action:\s*(\w+)/g, // X-TC-Action: ActionName |
| 17 | + /本接口取值:(\w+)。/g, // 本接口取值:ActionName。 |
| 18 | + /Action=(\w+)/g, // Action=ActionName (URL 参数) |
| 19 | + ]; |
| 20 | + |
| 21 | + // 需要跳过的文件(非具体 API 文档) |
| 22 | + const skipFiles = ['README.md', 'API-概览', '公共参数']; |
| 23 | + |
| 24 | + // 读取并过滤文件 |
| 25 | + const files = fs.readdirSync(REFERENCES_DIR) |
| 26 | + .filter(f => f.endsWith('.md')) |
| 27 | + .filter(f => !skipFiles.some(skip => f.includes(skip))); |
| 28 | + |
| 29 | + // 并行读取文件并提取 action |
| 30 | + const results = await Promise.all( |
| 31 | + files.map(async (file) => { |
| 32 | + const content = await fs.promises.readFile(path.join(REFERENCES_DIR, file), 'utf-8'); |
| 33 | + const actions: string[] = []; |
| 34 | + |
| 35 | + for (const pattern of patterns) { |
| 36 | + const regex = new RegExp(pattern.source, pattern.flags); |
| 37 | + let match; |
| 38 | + while ((match = regex.exec(content)) !== null) { |
| 39 | + actions.push(match[1]); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return actions; |
| 44 | + }) |
| 45 | + ); |
| 46 | + |
| 47 | + // 合并去重排序 |
| 48 | + return [...new Set(results.flat())].sort(); |
| 49 | +} |
| 50 | + |
| 51 | +function generateFile(actions: string[]): string { |
| 52 | + const content = `/** |
| 53 | + * 自动生成的 TCB 对外 API Action 列表 |
| 54 | + * 从 references/ 目录下的文档中提取 |
| 55 | + * |
| 56 | + * ⚠️ 请勿手动编辑此文件,由 scripts/generate-actionlist.ts 自动生成 |
| 57 | + * |
| 58 | + * Action 数量: ${actions.length} |
| 59 | + */ |
| 60 | +
|
| 61 | +const TCB_ALLOWED_ACTIONS: string[] = [ |
| 62 | +${actions.map(a => ` '${a}',`).join('\n')} |
| 63 | +]; |
| 64 | +
|
| 65 | +export default TCB_ALLOWED_ACTIONS; |
| 66 | +`; |
| 67 | + |
| 68 | + return content; |
| 69 | +} |
| 70 | + |
| 71 | +async function main() { |
| 72 | + console.log('📖 扫描 references 目录...'); |
| 73 | + const actions = await extractActions(); |
| 74 | + console.log(`✅ 提取到 ${actions.length} 个 Action`); |
| 75 | + |
| 76 | + console.log('📝 生成 action-list.ts...'); |
| 77 | + const content = generateFile(actions); |
| 78 | + |
| 79 | + // 确保输出目录存在 |
| 80 | + const outputDir = path.dirname(OUTPUT_FILE); |
| 81 | + if (!fs.existsSync(outputDir)) { |
| 82 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 83 | + } |
| 84 | + |
| 85 | + fs.writeFileSync(OUTPUT_FILE, content, 'utf-8'); |
| 86 | + console.log(`✅ 已生成: ${OUTPUT_FILE}`); |
| 87 | + |
| 88 | + // 打印 action 列表 |
| 89 | + console.log('\n📋 Action 列表:'); |
| 90 | + actions.forEach(a => console.log(` - ${a}`)); |
| 91 | +} |
| 92 | + |
| 93 | +main(); |
| 94 | + |
0 commit comments