-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathseed-r2.ts
More file actions
61 lines (50 loc) · 2.14 KB
/
Copy pathseed-r2.ts
File metadata and controls
61 lines (50 loc) · 2.14 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
import { execSync } from 'node:child_process'
import { writeFileSync, mkdtempSync, rmSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { processSpec, extractProducts } from '../src/spec-processor'
import { buildNonCodemodeTools, type OperationInfo } from '../src/openapi'
const OPENAPI_SPEC_URL =
'https://raw.githubusercontent.com/cloudflare/api-schemas/main/openapi.json'
const env = process.argv[2]
if (!env || !['staging', 'production'].includes(env)) {
console.error('Usage: npx tsx scripts/seed-r2.ts <staging|production>')
process.exit(1)
}
console.log(`Fetching OpenAPI spec from ${OPENAPI_SPEC_URL}...`)
const response = await fetch(OPENAPI_SPEC_URL)
if (!response.ok) {
throw new Error(`Failed to fetch spec: ${response.status}`)
}
const rawSpec = (await response.json()) as Record<string, unknown>
console.log('Processing spec, resolving $refs...')
const processed = processSpec(rawSpec)
const specJson = JSON.stringify(processed)
const products = extractProducts(rawSpec)
const productsJson = JSON.stringify(products)
const paths = (processed as { paths: Record<string, Record<string, OperationInfo>> }).paths
const nonCodemodeToolsJson = JSON.stringify(buildNonCodemodeTools(paths))
console.log(`Spec: ${(specJson.length / 1024 / 1024).toFixed(1)} MB, ${products.length} products`)
const tmp = mkdtempSync(join(tmpdir(), 'mcp-seed-'))
const specPath = join(tmp, 'spec.json')
const productsPath = join(tmp, 'products.json')
const nonCodemodeToolsPath = join(tmp, 'non-codemode-tools.json')
try {
writeFileSync(specPath, specJson)
writeFileSync(productsPath, productsJson)
writeFileSync(nonCodemodeToolsPath, nonCodemodeToolsJson)
for (const [key, path] of [
['spec.json', specPath],
['products.json', productsPath],
['non-codemode-tools.json', nonCodemodeToolsPath]
] as const) {
console.log(`Uploading ${key} to R2 (--env ${env})...`)
execSync(
`npx wrangler r2 object put mcp-spec-${env}/${key} --file "${path}" --content-type application/json --env ${env} --remote`,
{ stdio: 'inherit' }
)
}
console.log('Done!')
} finally {
rmSync(tmp, { recursive: true })
}