chore: Modify the oldspec to do nested as an option

This commit is contained in:
BluJ
2023-04-24 16:07:35 -06:00
parent 50cfc7aa43
commit 5f5cfe77a3
4 changed files with 22 additions and 18 deletions

View File

@@ -9,7 +9,10 @@ clean:
lib/test/output.ts: lib/test/makeOutput.ts scripts/oldSpecToBuilder.ts lib/test/output.ts: lib/test/makeOutput.ts scripts/oldSpecToBuilder.ts
npm run buildOutput npm run buildOutput
bundle: clean fmt $(TS_FILES) package.json .FORCE node_modules test buildOutput: lib/test/output.ts fmt
echo 'done'
bundle: clean $(TS_FILES) package.json .FORCE node_modules test fmt
npx tsc npx tsc
cp package.json dist/package.json cp package.json dist/package.json
cp README.md dist/README.md cp README.md dist/README.md

View File

@@ -5,7 +5,7 @@ import {
unionValueKey, unionValueKey,
} from "../config/configTypes"; } from "../config/configTypes";
import { deepMerge } from "../util"; import { deepMerge } from "../util";
import { InputSpec, matchInputSpec, testListUnion } from "./output"; import { InputSpec, matchInputSpec } from "./output";
export type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T export type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T
? 1 ? 1
@@ -97,9 +97,6 @@ describe("Inputs", () => {
}, },
}; };
test("Test just the input unions", () => {
testListUnion.validator().unsafeCast(validInput.testListUnion);
});
test("test valid input", () => { test("test valid input", () => {
const output = matchInputSpec.unsafeCast(validInput); const output = matchInputSpec.unsafeCast(validInput);
expect(output).toEqual(validInput); expect(output).toEqual(validInput);

View File

@@ -6,7 +6,7 @@
"types": "./lib/index.d.ts", "types": "./lib/index.d.ts",
"scripts": { "scripts": {
"test": "jest -c ./jest.config.js", "test": "jest -c ./jest.config.js",
"buildOutput": "ts-node --esm ./lib/test/makeOutput.ts", "buildOutput": "ts-node --esm ./lib/test/makeOutput.ts && npx prettier --write '**/*.ts'",
"check": "tsc --noEmit" "check": "tsc --noEmit"
}, },
"repository": { "repository": {

View File

@@ -16,11 +16,11 @@ export async function writeConvertedFileFromOld(
export default async function makeFileContentFromOld( export default async function makeFileContentFromOld(
inputData: Promise<any> | any, inputData: Promise<any> | any,
{ startSdk = "start-sdk" } = {}, { startSdk = "start-sdk", nested = true } = {},
) { ) {
const outputLines: string[] = []; const outputLines: string[] = [];
outputLines.push(` outputLines.push(`
import {Config, Value, List, Variants} from '${startSdk}/config/builder'; import {Config, Value, List, Variants} from '${startSdk}/config/builder'
`); `);
const data = await inputData; const data = await inputData;
@@ -41,14 +41,18 @@ export default async function makeFileContentFromOld(
outputLines.push(`export const ${variableName} = ${data};`); outputLines.push(`export const ${variableName} = ${data};`);
return variableName; return variableName;
} }
function maybeNewConst(key: string, data: string) {
if (nested) return data;
return newConst(key, data);
}
function convertInputSpec(data: any) { function convertInputSpec(data: any) {
let answer = "Config.of({"; let answer = "Config.of({";
for (const [key, value] of Object.entries(data)) { for (const [key, value] of Object.entries(data)) {
const variableName = newConst(key, convertValueSpec(value)); const variableName = maybeNewConst(key, convertValueSpec(value));
answer += `${JSON.stringify(key)}: ${variableName},`; answer += `${JSON.stringify(key)}: ${variableName},`;
} }
return `${answer}});`; return `${answer}})`;
} }
function convertValueSpec(value: any): string { function convertValueSpec(value: any): string {
switch (value.type) { switch (value.type) {
@@ -149,7 +153,7 @@ export default async function makeFileContentFromOld(
)} as const)`; )} as const)`;
} }
case "object": { case "object": {
const specName = newConst( const specName = maybeNewConst(
value.name + "_spec", value.name + "_spec",
convertInputSpec(value.spec), convertInputSpec(value.spec),
); );
@@ -160,7 +164,7 @@ export default async function makeFileContentFromOld(
}, ${specName})`; }, ${specName})`;
} }
case "union": { case "union": {
const variants = newConst( const variants = maybeNewConst(
value.name + "_variants", value.name + "_variants",
convertVariants(value.variants, value.tag["variant-names"] || {}), convertVariants(value.variants, value.tag["variant-names"] || {}),
); );
@@ -174,7 +178,7 @@ export default async function makeFileContentFromOld(
}, ${variants})`; }, ${variants})`;
} }
case "list": { case "list": {
const list = newConst(value.name + "_list", convertList(value)); const list = maybeNewConst(value.name + "_list", convertList(value));
return `Value.list(${list})`; return `Value.list(${list})`;
} }
case "pointer": { case "pointer": {
@@ -263,7 +267,7 @@ export default async function makeFileContentFromOld(
)})`; )})`;
} }
case "object": { case "object": {
const specName = newConst( const specName = maybeNewConst(
value.name + "_spec", value.name + "_spec",
convertInputSpec(value.spec.spec), convertInputSpec(value.spec.spec),
); );
@@ -281,14 +285,14 @@ export default async function makeFileContentFromOld(
})`; })`;
} }
case "union": { case "union": {
const variants = newConst( const variants = maybeNewConst(
value.name + "_variants", value.name + "_variants",
convertVariants( convertVariants(
value.spec.variants, value.spec.variants,
value.spec["variant-names"] || {}, value.spec["variant-names"] || {},
), ),
); );
const unionValueName = newConst( const unionValueName = maybeNewConst(
value.name + "_union", value.name + "_union",
`${rangeToTodoComment(value?.range)} `${rangeToTodoComment(value?.range)}
Value.union({ Value.union({
@@ -302,7 +306,7 @@ export default async function makeFileContentFromOld(
}, ${variants}) }, ${variants})
`, `,
); );
const listConfig = newConst( const listConfig = maybeNewConst(
value.name + "_list_config", value.name + "_list_config",
` `
Config.of({ Config.of({
@@ -333,7 +337,7 @@ export default async function makeFileContentFromOld(
): string { ): string {
let answer = "Variants.of({"; let answer = "Variants.of({";
for (const [key, value] of Object.entries(variants)) { for (const [key, value] of Object.entries(variants)) {
const variantSpec = newConst(key, convertInputSpec(value)); const variantSpec = maybeNewConst(key, convertInputSpec(value));
answer += `"${key}": {name: "${ answer += `"${key}": {name: "${
variantNames[key] || key variantNames[key] || key
}", spec: ${variantSpec}},`; }", spec: ${variantSpec}},`;