| 1 | #!/usr/bin/env node |
| 2 | import { existsSync, readFileSync, writeFileSync } from "node:fs"; |
| 3 | import { dirname, resolve } from "node:path"; |
| 4 | import { fileURLToPath } from "node:url"; |
| 5 | |
| 6 | const here = dirname(fileURLToPath(import.meta.url)); |
| 7 | const root = resolve( |
| 8 | process.argv[2] ?? |
| 9 | process.env.CODEWHALE_MARKETPLACE ?? |
| 10 | [resolve(here, "../../../codewhale-plugin-marketplace"), resolve(here, "../../../../codewhale-plugin-marketplace")].find((p) => |
| 11 | existsSync(resolve(p, "marketplace.json")), |
| 12 | ) ?? |
| 13 | "codewhale-plugin-marketplace", |
| 14 | ); |
| 15 | |
| 16 | const marketplace = JSON.parse(readFileSync(resolve(root, "marketplace.json"), "utf8")); |
| 17 | const catalog = JSON.parse(readFileSync(resolve(root, "connections/catalog.json"), "utf8")); |
| 18 | |
| 19 | const AUTH = { oauth: "oauth", bearer: "token", none: "none" }; |
| 20 | const connectors = catalog.connections |
| 21 | .filter((c) => c.status === "documented") |
| 22 | .map((c) => { |
| 23 | const auth = AUTH[c.auth]; |
| 24 | if (!auth) throw new Error(`connector ${c.id}: unknown auth ${c.auth}`); |
| 25 | return { id: c.id, name: c.name, url: c.url, auth }; |
| 26 | }); |
| 27 | |
| 28 | const plugins = marketplace.plugins.map((p) => ({ name: p.name, version: p.version })); |
| 29 | |
| 30 | const out = `// Generated by scripts/derive-connectors.mjs from codewhale-plugin-marketplace |
| 31 | // (connections/catalog.json checked ${catalog.checked_at}). Do not edit by hand. |
| 32 | export type ConnectorAuth = "oauth" | "token" | "none"; |
| 33 | |
| 34 | export const CONNECTORS: readonly { id: string; name: string; url: string; auth: ConnectorAuth }[] = ${JSON.stringify(connectors, null, 2)}; |
| 35 | |
| 36 | export const CATALOG_PLUGINS: readonly { name: string; version: string }[] = ${JSON.stringify(plugins, null, 2)}; |
| 37 | `; |
| 38 | writeFileSync(resolve(here, "../lib/content/connectors.generated.ts"), out); |
| 39 | console.log(`[derive-connectors] wrote ${connectors.length} connectors and ${plugins.length} plugins from ${root}`); |
| 40 |