@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 03:37 UTC

main
ts 129 lines 5.06 KB
Raw
1 /**
2 * Generate `packages/aws/src/IAM/actions.generated.ts` — a SHALLOW
3 * string-literal union type `IamAction` built from distilled's per-service
4 * operation names.
5 *
6 * Coordinator-run (like generate-api-reference.ts):
7 *
8 * node scripts/generate-iam-actions.ts
9 *
10 * Source of truth: `submodules/distilled/packages/aws/src/services/*.ts`. For each
11 * service module we extract
12 *
13 * - the sigv4 signing name (`T.AwsAuthSigv4({ name: "..." })`), which is the
14 * best distilled-local approximation of the IAM action service prefix
15 * (a small override map corrects the known mismatches, e.g.
16 * `monitoring` -> `cloudwatch`), and
17 * - every `operationName: "X"` registered via `API.make`.
18 *
19 * The emitted type is a flat union of fully-expanded literals
20 * (`"s3:GetObject" | "s3:*" | ... | (string & {})`) — deliberately NO
21 * template-literal type computation, per the DSL-track type-level cost
22 * ceiling (processes/AWS/design/dsl-abstractions.md, risk #5). The
23 * `(string & {})` escape hatch means unknown/condition-only IAM actions are
24 * never a hard break; the union exists purely for autocomplete.
25 *
26 * Note: distilled operation names approximate IAM actions but are not 1:1
27 * (IAM has actions with no API operation and vice versa) — see the open
28 * question in dsl-abstractions.md. The escape hatch covers the gap.
29 */
30
31 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
32 import * as NodeServices from "@effect/platform-node/NodeServices";
33 import { Command } from "effect/cli";
34 import * as Console from "effect/Console";
35 import * as Effect from "effect/Effect";
36 import * as FileSystem from "effect/FileSystem";
37 import * as Path from "effect/Path";
38
39 /**
40 * sigv4 signing name -> IAM action service prefix, for the known cases where
41 * the two differ. Everything else uses the (lowercased) signing name as-is.
42 */
43 const PREFIX_OVERRIDES: Record<string, string> = {
44 // CloudWatch metrics sign as "monitoring" but IAM actions are `cloudwatch:*`.
45 monitoring: "cloudwatch",
46 // IoT data planes sign with dedicated names but share the `iot:` prefix.
47 iotdata: "iot",
48 ioteventsdata: "iotevents",
49 IoTSecuredTunneling: "iot",
50 "iot-jobs-data": "iot",
51 };
52
53 const command = Command.make(
54 "generate-iam-actions",
55 {},
56 Effect.fn(function* () {
57 const fs = yield* FileSystem.FileSystem;
58 const path = yield* Path.Path;
59 const root = path.resolve(import.meta.dirname, "..");
60 const SERVICES_DIR = path.join(root, "submodules/distilled/packages/aws/src/services");
61 const OUT_FILE = path.join(root, "packages/aws/src/IAM/actions.generated.ts");
62
63 const files = (yield* fs.readDirectory(SERVICES_DIR)).filter(
64 (f) => f.endsWith(".ts") && f !== "index.ts",
65 );
66
67 /** IAM action prefix -> operation names */
68 const byPrefix = new Map<string, Set<string>>();
69 let totalOps = 0;
70 const skipped: string[] = [];
71
72 for (const file of files.sort()) {
73 const source = yield* fs.readFileString(path.join(SERVICES_DIR, file));
74 const nameMatch = source.match(/AwsAuthSigv[24]\(\{\s*name:\s*"([^"]+)"/);
75 if (!nameMatch) {
76 skipped.push(file);
77 continue;
78 }
79 const sigv4Name = nameMatch[1]!;
80 const prefix = (PREFIX_OVERRIDES[sigv4Name] ?? sigv4Name).toLowerCase();
81 const ops = byPrefix.get(prefix) ?? new Set<string>();
82 byPrefix.set(prefix, ops);
83 for (const match of source.matchAll(/operationName:\s*"([A-Za-z0-9_]+)"/g)) {
84 if (!ops.has(match[1]!)) {
85 ops.add(match[1]!);
86 totalOps++;
87 }
88 }
89 }
90
91 const prefixes = [...byPrefix.keys()].sort();
92 const lines: string[] = [
93 "// AUTO-GENERATED FILE — DO NOT EDIT.",
94 "//",
95 "// Regenerate with: node scripts/generate-iam-actions.ts",
96 "//",
97 `// Source: submodules/distilled/packages/aws/src/services/*.ts (${files.length - skipped.length} service modules,`,
98 `// ${prefixes.length} IAM action prefixes, ${totalOps} operations).`,
99 "//",
100 "// A SHALLOW literal union (no template-literal type computation) of",
101 "// `service:Operation` IAM action names, plus `service:*` per service and a",
102 "// `(string & {})` escape hatch so arbitrary action strings always remain",
103 "// assignable. Exists purely for editor autocomplete on",
104 "// `PolicyStatement.Action`.",
105 "",
106 "export type IamAction =",
107 ];
108 for (const prefix of prefixes) {
109 lines.push(` | "${prefix}:*"`);
110 for (const op of [...byPrefix.get(prefix)!].sort()) {
111 lines.push(` | "${prefix}:${op}"`);
112 }
113 }
114 lines.push(" | (string & {});", "");
115
116 yield* fs.writeFileString(OUT_FILE, lines.join("\n"));
117 yield* Console.log(
118 `wrote ${OUT_FILE}: ${prefixes.length} prefixes, ${totalOps} operations, ${lines.length} lines`,
119 );
120 if (skipped.length > 0) {
121 yield* Console.log(`skipped (no AwsAuthSigv4 name): ${skipped.join(", ")}`);
122 }
123 }),
124 ).pipe(Command.withDescription("Generate the IamAction union from distilled's AWS services"));
125
126 Command.run(command, { version: "0.0.0" }).pipe(
127 Effect.provide(NodeServices.layer),
128 NodeRuntime.runMain,
129 );