|
| 1 | +import { Prop, PropParent } from "./props.ts"; |
| 2 | +import { camelCase } from "https://deno.land/x/case/mod.ts"; |
| 3 | +import { singular } from "https://deno.land/x/deno_plural/mod.ts"; |
| 4 | + |
| 5 | +type AwsScaffold = Record<string, unknown>; |
| 6 | + |
| 7 | +export function awsGenerate( |
| 8 | + awsService: string, |
| 9 | + awsCommand: string, |
| 10 | +): Array<Prop> { |
| 11 | + const scaffold = getAwsCliScaffold(awsService, awsCommand); |
| 12 | + const props = propsFromScaffold(scaffold, []); |
| 13 | + return props; |
| 14 | +} |
| 15 | + |
| 16 | +function getAwsCliScaffold( |
| 17 | + awsService: string, |
| 18 | + awsCommand: string, |
| 19 | +): AwsScaffold { |
| 20 | + const command = new Deno.Command("aws", { |
| 21 | + args: [awsService, awsCommand, "--generate-cli-skeleton"], |
| 22 | + stdin: "null", |
| 23 | + stdout: "piped", |
| 24 | + stderr: "piped", |
| 25 | + }); |
| 26 | + const { code, stdout: rawStdout, stderr: rawStderr } = command.outputSync(); |
| 27 | + const stdout = new TextDecoder().decode(rawStdout); |
| 28 | + const stderr = new TextDecoder().decode(rawStderr); |
| 29 | + |
| 30 | + if (code !== 0) { |
| 31 | + console.error(`AWS cli failed with exit code: ${code}`); |
| 32 | + console.error(`STDOUT:\n\n${stdout.toLocaleString()}`); |
| 33 | + console.error(`STDERR:\n\n${stderr.toLocaleString()}`); |
| 34 | + throw new Error("aws cli command failed"); |
| 35 | + } |
| 36 | + const result = JSON.parse(stdout); |
| 37 | + return result; |
| 38 | +} |
| 39 | + |
| 40 | +function propsFromScaffold( |
| 41 | + scaffold: AwsScaffold, |
| 42 | + props: Array<Prop>, |
| 43 | + parent?: PropParent, |
| 44 | +): Array<Prop> { |
| 45 | + for (let [key, value] of Object.entries(scaffold)) { |
| 46 | + if ( |
| 47 | + key == "KeyName" && parent?.kind == "object" && |
| 48 | + parent?.children.length == 0 |
| 49 | + ) { |
| 50 | + // @ts-ignore we know you can't do this officialy, but unofficialy, suck |
| 51 | + // it. |
| 52 | + parent.kind = "map"; |
| 53 | + key = singular(parent.name); |
| 54 | + } |
| 55 | + let prop: Prop | undefined; |
| 56 | + if (typeof value === "string") { |
| 57 | + prop = { |
| 58 | + kind: "string", |
| 59 | + name: key, |
| 60 | + variableName: camelCase(`${key}Prop`), |
| 61 | + }; |
| 62 | + } else if (typeof value === "number") { |
| 63 | + prop = { |
| 64 | + kind: "number", |
| 65 | + name: key, |
| 66 | + variableName: camelCase(`${key}Prop`), |
| 67 | + }; |
| 68 | + } else if (typeof value === "boolean") { |
| 69 | + prop = { |
| 70 | + kind: "boolean", |
| 71 | + name: key, |
| 72 | + variableName: camelCase(`${key}Prop`), |
| 73 | + }; |
| 74 | + } else if (Array.isArray(value)) { |
| 75 | + prop = { |
| 76 | + kind: "array", |
| 77 | + name: key, |
| 78 | + variableName: camelCase(`${key}Prop`), |
| 79 | + }; |
| 80 | + const childObject: AwsScaffold = {}; |
| 81 | + childObject[`${key}Child`] = value[0]; |
| 82 | + propsFromScaffold(childObject, props, prop); |
| 83 | + } else if (value == null) { |
| 84 | + // Sometimes the default value is null, and not the empty string. This |
| 85 | + // seems like a reasonable default, even if it is going to be weird. |
| 86 | + prop = { |
| 87 | + kind: "string", |
| 88 | + name: key, |
| 89 | + variableName: camelCase(`${key}Prop`), |
| 90 | + }; |
| 91 | + } else if (typeof value === "object") { |
| 92 | + prop = { |
| 93 | + kind: "object", |
| 94 | + name: key, |
| 95 | + variableName: camelCase(`${key}Prop`), |
| 96 | + children: [], |
| 97 | + }; |
| 98 | + propsFromScaffold(value as AwsScaffold, props, prop); |
| 99 | + } |
| 100 | + if (prop && parent?.kind == "object") { |
| 101 | + parent.children.push(prop); |
| 102 | + } else if (prop && parent?.kind == "array" || parent?.kind == "map") { |
| 103 | + parent.entry = prop; |
| 104 | + } else if (prop && !parent) { |
| 105 | + props.push(prop); |
| 106 | + } |
| 107 | + } |
| 108 | + return props; |
| 109 | +} |
0 commit comments