chore: Fix the tests and add new types for raw

This commit is contained in:
BluJ
2023-04-05 13:51:43 -06:00
parent b8d6addf28
commit 45192e0358
19 changed files with 222 additions and 376 deletions

View File

@@ -1,22 +1,17 @@
import camelCase from "lodash/camelCase";
import * as fs from "fs";
import { string } from "ts-matches";
import { unionSelectKey } from "../lib/config/config-types";
import { unionSelectKey } from "../lib/config/configTypes";
export async function writeConvertedFile(
file: string,
inputData: Promise<any> | any,
options: Parameters<typeof makeFileContent>[1]
) {
await fs.writeFile(file, await makeFileContent(inputData, options), (err) =>
console.error(err)
);
await fs.writeFile(file, await makeFileContent(inputData, options), (err) => console.error(err));
}
export default async function makeFileContent(
inputData: Promise<any> | any,
{ startSdk = "start-sdk" } = {}
) {
export default async function makeFileContent(inputData: Promise<any> | any, { startSdk = "start-sdk" } = {}) {
const outputLines: string[] = [];
outputLines.push(`
import {Config, Value, List, Variants} from '${startSdk}/config/builder';
@@ -25,13 +20,8 @@ export default async function makeFileContent(
const namedConsts = new Set(["Config", "Value", "List"]);
const configName = newConst("InputSpec", convertInputSpec(data));
const configMatcherName = newConst(
"matchInputSpec",
`${configName}.validator()`
);
outputLines.push(
`export type InputSpec = typeof ${configMatcherName}._TYPE;`
);
const configMatcherName = newConst("matchInputSpec", `${configName}.validator()`);
outputLines.push(`export type InputSpec = typeof ${configMatcherName}._TYPE;`);
return outputLines.join("\n");
@@ -45,7 +35,7 @@ export default async function makeFileContent(
for (const [key, value] of Object.entries(data)) {
const variableName = newConst(key, convertValueSpec(value));
answer += `"${key}": ${variableName},`;
answer += `${JSON.stringify(key)}: ${variableName},`;
}
return `${answer}});`;
}
@@ -111,10 +101,7 @@ export default async function makeFileContent(
)})`;
}
case "enum": {
const allValueNames = new Set([
...(value?.["values"] || []),
...Object.keys(value?.["value-names"] || {}),
]);
const allValueNames = new Set([...(value?.["values"] || []), ...Object.keys(value?.["value-names"] || {})]);
const values = Object.fromEntries(
Array.from(allValueNames)
.filter(string.test)
@@ -134,10 +121,7 @@ export default async function makeFileContent(
)} as const)`;
}
case "object": {
const specName = newConst(
value.name + "_spec",
convertInputSpec(value.spec)
);
const specName = newConst(value.name + "_spec", convertInputSpec(value.spec));
return `Value.object({
name: ${JSON.stringify(value.name || null)},
description: ${JSON.stringify(value.description || null)},
@@ -174,9 +158,7 @@ export default async function makeFileContent(
function convertList(value: any) {
switch (value.subtype) {
case "string": {
return `List.${
value?.spec?.textarea === true ? "textarea" : "string"
}(${JSON.stringify(
return `List.${value?.spec?.textarea === true ? "textarea" : "string"}(${JSON.stringify(
{
name: value.name || null,
range: value.range || null,
@@ -235,10 +217,7 @@ export default async function makeFileContent(
)})`;
}
case "object": {
const specName = newConst(
value.name + "_spec",
convertInputSpec(value.spec.spec)
);
const specName = newConst(value.name + "_spec", convertInputSpec(value.spec.spec));
return `List.obj({
name: ${JSON.stringify(value.name || null)},
range: ${JSON.stringify(value.range || null)},
@@ -254,19 +233,14 @@ export default async function makeFileContent(
case "union": {
const variants = newConst(
value.name + "_variants",
convertVariants(
value.spec.variants,
value.spec["variant-names"] || {}
)
convertVariants(value.spec.variants, value.spec["variant-names"] || {})
);
const unionValueName = newConst(
value.name + "_union",
`
Value.union({
name: ${JSON.stringify(value?.spec?.tag?.name || null)},
description: ${JSON.stringify(
value?.spec?.tag?.description || null
)},
description: ${JSON.stringify(value?.spec?.tag?.description || null)},
warning: ${JSON.stringify(value?.spec?.tag?.warning || null)},
required: ${JSON.stringify(!(value?.spec?.tag?.nullable || false))},
default: ${JSON.stringify(value?.spec?.default || null)},
@@ -277,7 +251,7 @@ export default async function makeFileContent(
value.name + "_list_config",
`
Config.of({
${unionSelectKey}: ${unionValueName}
"union": ${unionValueName}
})
`
);
@@ -297,16 +271,11 @@ export default async function makeFileContent(
throw new Error(`Unknown subtype "${value.subtype}"`);
}
function convertVariants(
variants: Record<string, unknown>,
variantNames: Record<string, string>
): string {
function convertVariants(variants: Record<string, unknown>, variantNames: Record<string, string>): string {
let answer = "Variants.of({";
for (const [key, value] of Object.entries(variants)) {
const variantSpec = newConst(key, convertInputSpec(value));
answer += `"${key}": {name: "${
variantNames[key] || key
}", spec: ${variantSpec}},`;
answer += `"${key}": {name: "${variantNames[key] || key}", spec: ${variantSpec}},`;
}
return `${answer}})`;
}