The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * Regenerate `src/ApiToken/PermissionGroups.ts` from Cloudflare's live |
| 3 | * permission-group catalog. |
| 4 | * |
| 5 | * CLOUDFLARE_API_TOKEN=… CLOUDFLARE_ACCOUNT_ID=… pnpm -C packages/cloudflare sync:permission-groups |
| 6 | * pnpm -C packages/cloudflare sync:permission-groups --input groups.json # from a saved listing |
| 7 | * |
| 8 | * Run weekly by `.github/workflows/sync-cloudflare-permission-groups.yml`. |
| 9 | * |
| 10 | * The sync never drops a name, so a regenerated catalog can't break a config |
| 11 | * that type-checks today: |
| 12 | * |
| 13 | * - a group Cloudflare renamed keeps its former name as an alias on the |
| 14 | * same id; |
| 15 | * - a group the listing no longer returns is retained. The listing is |
| 16 | * filtered by what the account can see, so absence doesn't mean retired. |
| 17 | */ |
| 18 | import * as accounts from "@distilled.cloud/cloudflare/accounts"; |
| 19 | import * as Credentials from "@distilled.cloud/cloudflare/Credentials"; |
| 20 | import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 21 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 22 | import * as Config from "effect/Config"; |
| 23 | import * as Effect from "effect/Effect"; |
| 24 | import * as FileSystem from "effect/FileSystem"; |
| 25 | import { FetchHttpClient } from "effect/http"; |
| 26 | import * as Layer from "effect/Layer"; |
| 27 | import * as Path from "effect/Path"; |
| 28 | import { ChildProcess } from "effect/process"; |
| 29 | import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"; |
| 30 | import { PERMISSION_GROUPS } from "../src/ApiToken/PermissionGroups.ts"; |
| 31 | |
| 32 | interface Group { |
| 33 | id: string; |
| 34 | name: string; |
| 35 | description: string; |
| 36 | scopes: readonly string[]; |
| 37 | } |
| 38 | |
| 39 | const CATALOG = "src/ApiToken/PermissionGroups.ts"; |
| 40 | |
| 41 | const fetchGroups = Effect.gen(function* () { |
| 42 | const accountId = yield* Config.String("CLOUDFLARE_ACCOUNT_ID"); |
| 43 | const groups = yield* accounts.listTokensPermissionGroups({ accountId }); |
| 44 | return groups.map((group): Group => ({ |
| 45 | id: group.id ?? "", |
| 46 | name: group.name ?? "", |
| 47 | description: group.description ?? "", |
| 48 | scopes: group.scopes ?? [], |
| 49 | })); |
| 50 | }).pipe(Effect.provide(Layer.mergeAll(Credentials.fromEnv(), FetchHttpClient.layer))); |
| 51 | |
| 52 | const readGroups = Effect.fn(function* (file: string) { |
| 53 | const fs = yield* FileSystem.FileSystem; |
| 54 | return JSON.parse(yield* fs.readFileString(file)) as Group[]; |
| 55 | }); |
| 56 | |
| 57 | /** Live groups first, then the previous catalog's names the listing lacks. */ |
| 58 | const merge = (live: Group[], previous: readonly Group[]) => { |
| 59 | const key = (group: Group) => `${group.id}\0${group.name}`; |
| 60 | const seen = new Set<string>(); |
| 61 | const unique = (group: Group) => !seen.has(key(group)) && !!seen.add(key(group)); |
| 62 | const liveById = new Map(live.map((group) => [group.id, group])); |
| 63 | |
| 64 | const groups = live.filter(unique); |
| 65 | const unlisted = previous.filter((group) => !liveById.has(group.id)).filter(unique); |
| 66 | const renamed = previous |
| 67 | .filter((group) => liveById.has(group.id)) |
| 68 | .filter(unique) |
| 69 | // Keep the old name, but describe the group as Cloudflare does today. |
| 70 | .map((group) => ({ ...liveById.get(group.id)!, name: group.name })); |
| 71 | return { groups, unlisted, renamed }; |
| 72 | }; |
| 73 | |
| 74 | const entry = (group: Group) => |
| 75 | ` { |
| 76 | id: ${JSON.stringify(group.id)}, |
| 77 | name: ${JSON.stringify(group.name)}, |
| 78 | description: ${JSON.stringify(group.description)}, |
| 79 | scopes: ${JSON.stringify(group.scopes)}, |
| 80 | },`; |
| 81 | |
| 82 | const section = (comment: string, groups: Group[]) => |
| 83 | groups.length === 0 ? [] : [comment, ...groups.map(entry)]; |
| 84 | |
| 85 | const render = ({ groups, unlisted, renamed }: ReturnType<typeof merge>) => `/** |
| 86 | * Static catalog of Cloudflare API token permission groups, copied from the |
| 87 | * upstream \`alchemy\` repo so we can offer typed permission-group names without |
| 88 | * a network round-trip on every token create/update. |
| 89 | * |
| 90 | * Refreshed from \`GET /accounts/{account_id}/tokens/permission_groups\`. |
| 91 | * |
| 92 | * @see https://developers.cloudflare.com/fundamentals/api/reference/permissions/ |
| 93 | */ |
| 94 | |
| 95 | export interface PermissionGroup { |
| 96 | id: string; |
| 97 | name: string; |
| 98 | description: string; |
| 99 | scopes: readonly string[]; |
| 100 | } |
| 101 | |
| 102 | export const PERMISSION_GROUPS = [ |
| 103 | ${[ |
| 104 | ...groups.map(entry), |
| 105 | ...section( |
| 106 | " // No longer listed by Cloudflare's permission-groups API; kept so existing\n // configs that reference them keep resolving.", |
| 107 | unlisted, |
| 108 | ), |
| 109 | ...section( |
| 110 | " // Former names of groups Cloudflare has since renamed, kept so existing\n // configs that reference them by name keep resolving.", |
| 111 | renamed, |
| 112 | ), |
| 113 | ].join("\n")} |
| 114 | ] as const; |
| 115 | |
| 116 | export type PermissionGroupName = (typeof PERMISSION_GROUPS)[number]["name"]; |
| 117 | |
| 118 | /** |
| 119 | * Lookup map from permission-group name to the first matching permission group. |
| 120 | * |
| 121 | * Some Cloudflare names appear twice (e.g. account-scope and zone-scope variants |
| 122 | * sharing a label). We keep the first occurrence; callers that need a specific |
| 123 | * scope should pass \`{ id }\` directly instead of using the name shorthand. |
| 124 | */ |
| 125 | export const PERMISSION_GROUPS_BY_NAME: Record<PermissionGroupName, PermissionGroup> = (() => { |
| 126 | const map = {} as Record<PermissionGroupName, PermissionGroup>; |
| 127 | for (const group of PERMISSION_GROUPS) { |
| 128 | if (!(group.name in map)) { |
| 129 | map[group.name as PermissionGroupName] = group; |
| 130 | } |
| 131 | } |
| 132 | return map; |
| 133 | })(); |
| 134 | `; |
| 135 | |
| 136 | const main = Effect.gen(function* () { |
| 137 | const fs = yield* FileSystem.FileSystem; |
| 138 | const path = yield* Path.Path; |
| 139 | const inputIndex = process.argv.indexOf("--input"); |
| 140 | const live = |
| 141 | inputIndex === -1 ? yield* fetchGroups : yield* readGroups(process.argv[inputIndex + 1]!); |
| 142 | if (live.length === 0) { |
| 143 | return yield* Effect.die("Cloudflare returned no permission groups; refusing to sync."); |
| 144 | } |
| 145 | |
| 146 | const merged = merge(live, PERMISSION_GROUPS); |
| 147 | const file = path.join(import.meta.dirname, "..", CATALOG); |
| 148 | yield* fs.writeFileString(file, render(merged)); |
| 149 | const spawner = yield* ChildProcessSpawner; |
| 150 | const exitCode = yield* spawner.exitCode(ChildProcess.make("pnpm", ["exec", "oxfmt", file])); |
| 151 | if (exitCode !== 0) { |
| 152 | return yield* Effect.die(`oxfmt exited with code ${exitCode}`); |
| 153 | } |
| 154 | yield* Effect.log( |
| 155 | `${CATALOG}: ${merged.groups.length} listed, ${merged.unlisted.length} unlisted, ${merged.renamed.length} renamed`, |
| 156 | ); |
| 157 | }); |
| 158 | |
| 159 | main.pipe(Effect.scoped, Effect.provide(NodeServices.layer), NodeRuntime.runMain); |