|
| 1 | +import type { ProviderSource } from "./provider-source.ts"; |
| 2 | + |
1 | 3 | import { readFile, writeFile } from "node:fs/promises"; |
2 | 4 | import { join } from "node:path"; |
3 | 5 | import { loadProviderSources } from "./provider-source.ts"; |
4 | 6 |
|
5 | 7 | const providersDir = join(process.cwd(), "src/providers"); |
6 | 8 | const providerSources = await loadProviderSources(); |
7 | | -const services = providerSources.map((source) => source.service); |
8 | | -const executableActionIds = new Map<string, string[]>( |
9 | | - providerSources.map((source) => [ |
10 | | - source.service, |
11 | | - source.definition.actions.map((action) => action.id).sort((a, b) => a.localeCompare(b)), |
12 | | - ]), |
13 | | -); |
| 9 | + |
| 10 | +await Promise.all([ |
| 11 | + writeRegistry("registry.generated.ts", providerSources), |
| 12 | + writeRegistry( |
| 13 | + "registry.cloudflare.generated.ts", |
| 14 | + providerSources.filter((source) => !source.nodeOnly), |
| 15 | + ), |
| 16 | +]); |
14 | 17 |
|
15 | 18 | function propertyName(service: string): string { |
16 | 19 | return /^[A-Za-z_$][\w$]*$/.test(service) ? service : JSON.stringify(service); |
17 | 20 | } |
18 | 21 |
|
19 | | -const registryLines = [ |
20 | | - 'import type { CredentialValidators, ProviderExecutors, ProviderProxyExecutor } from "../core/types.ts";', |
21 | | - "", |
22 | | - "/** Lazy-loaded provider executor module shape. */", |
23 | | - "export type ExecutorModule = {", |
24 | | - " credentialValidators?: CredentialValidators;", |
25 | | - " executors: ProviderExecutors;", |
26 | | - " proxy?: ProviderProxyExecutor;", |
27 | | - "};", |
28 | | - "", |
29 | | - "/** Generated lazy imports for provider executors. Do not hand-edit. */", |
30 | | - "export const executorModules: Record<string, () => Promise<ExecutorModule>> = {", |
31 | | - ...services.map( |
32 | | - (service) => ` ${propertyName(service)}: (): Promise<ExecutorModule> => import("./${service}/executors.ts"),`, |
33 | | - ), |
34 | | - "};", |
35 | | - "", |
36 | | - "/** Generated local executable action ids by provider. Do not hand-edit. */", |
37 | | - "export const executableActionIds: Record<string, string[]> = {", |
38 | | - ...services.flatMap((service) => [ |
39 | | - ` ${propertyName(service)}: [`, |
40 | | - ...(executableActionIds.get(service) ?? []).map((actionId) => ` ${JSON.stringify(actionId)},`), |
41 | | - " ],", |
42 | | - ]), |
43 | | - "};", |
44 | | -]; |
| 22 | +async function writeRegistry(filename: string, sources: ProviderSource[]): Promise<void> { |
| 23 | + const services = sources.map((source) => source.service); |
| 24 | + const executableActionIds = new Map<string, string[]>( |
| 25 | + sources.map((source) => [ |
| 26 | + source.service, |
| 27 | + source.definition.actions.map((action) => action.id).sort((a, b) => a.localeCompare(b)), |
| 28 | + ]), |
| 29 | + ); |
| 30 | + const lines = [ |
| 31 | + 'import type { ExecutorModule } from "./provider-loader.ts";', |
| 32 | + "", |
| 33 | + "/** Generated lazy imports for provider executors. Do not hand-edit. */", |
| 34 | + "export const executorModules: Record<string, () => Promise<ExecutorModule>> = {", |
| 35 | + ...services.map( |
| 36 | + (service) => ` ${propertyName(service)}: (): Promise<ExecutorModule> => import("./${service}/executors.ts"),`, |
| 37 | + ), |
| 38 | + "};", |
| 39 | + "", |
| 40 | + "/** Generated local executable action ids by provider. Do not hand-edit. */", |
| 41 | + "export const executableActionIds: Record<string, string[]> = {", |
| 42 | + ...services.flatMap((service) => [ |
| 43 | + ` ${propertyName(service)}: [`, |
| 44 | + ...(executableActionIds.get(service) ?? []).map((actionId) => ` ${JSON.stringify(actionId)},`), |
| 45 | + " ],", |
| 46 | + ]), |
| 47 | + "};", |
| 48 | + ]; |
45 | 49 |
|
46 | | -const registryPath = join(providersDir, "registry.generated.ts"); |
47 | | -const registryContent = `${registryLines.join("\n")}\n`; |
48 | | -const existingContent = await readTextFile(registryPath); |
49 | | -if (existingContent !== registryContent) { |
50 | | - await writeFile(registryPath, registryContent); |
51 | | - console.log(`Generated provider registry for ${services.length} providers.`); |
52 | | -} else { |
53 | | - console.log(`Provider registry is up to date for ${services.length} providers.`); |
| 50 | + const path = join(providersDir, filename); |
| 51 | + const content = `${lines.join("\n")}\n`; |
| 52 | + const existingContent = await readTextFile(path); |
| 53 | + if (existingContent !== content) { |
| 54 | + await writeFile(path, content); |
| 55 | + console.log(`Generated ${filename} for ${services.length} providers.`); |
| 56 | + } else { |
| 57 | + console.log(`${filename} is up to date for ${services.length} providers.`); |
| 58 | + } |
54 | 59 | } |
55 | 60 |
|
56 | 61 | async function readTextFile(path: string): Promise<string | undefined> { |
|
0 commit comments