This commit is contained in:
BluJ
2023-03-27 17:07:17 -06:00
parent ce0170b808
commit 72c9e5fd26
10 changed files with 73 additions and 62 deletions

View File

@@ -9,9 +9,8 @@ make 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: fmt $(TS_FILES) .FORCE node_modules bundle: fmt $(TS_FILES) package.json .FORCE node_modules
npx tsc-multi npx tsc
npx tsc --emitDeclarationOnly
check: check:
npm run check npm run check
@@ -22,7 +21,7 @@ fmt: node_modules
node_modules: package.json node_modules: package.json
npm install npm install
publish: bundle publish: bundle package.json README.md LICENSE
cp package.json dist/package.json cp package.json dist/package.json
cp README.md dist/README.md cp README.md dist/README.md
cp LICENSE dist/LICENSE cp LICENSE dist/LICENSE

View File

@@ -1,5 +1,7 @@
import { typeFromProps } from "../../util";
export class IBuilder<A> { export class IBuilder<A> {
protected constructor(readonly a: A) {} protected constructor(readonly a: A) { }
public build(): A { public build(): A {
return this.a; return this.a;

View File

@@ -7,6 +7,7 @@ import {
ValueSpecList, ValueSpecList,
ValueSpecListOf, ValueSpecListOf,
} from "../config-types"; } from "../config-types";
import { guardAll, typeFromProps } from "../../util";
/** /**
* Used as a subtype of Value.list * Used as a subtype of Value.list
@@ -39,7 +40,7 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
default: string[]; default: string[];
range: string; range: string;
spec: { spec: {
masked: boolean | null; masked: boolean;
placeholder: string | null; placeholder: string | null;
pattern: string | null; pattern: string | null;
patternDescription: string | null; patternDescription: string | null;
@@ -51,7 +52,7 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
type: "list" as const, type: "list" as const,
subtype: "string" as const, subtype: "string" as const,
...a, ...a,
} as ValueSpecListOf<"string">); });
} }
static number< static number<
A extends { A extends {
@@ -143,4 +144,8 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
...value, ...value,
}); });
} }
public validator() {
return guardAll(this.a);
}
} }

View File

@@ -7,15 +7,17 @@ import {
ValueSpec, ValueSpec,
ValueSpecList, ValueSpecList,
ValueSpecNumber, ValueSpecNumber,
ValueSpecOf,
ValueSpecString, ValueSpecString,
} from "../config-types"; } from "../config-types";
import { guardAll } from "../../util";
export type DefaultString = export type DefaultString =
| string | string
| { | {
charset: string | null | undefined; charset: string | null | undefined;
len: number; len: number;
}; };
/** /**
* A value is going to be part of the form in the FE of the OS. * A value is going to be part of the form in the FE of the OS.
@@ -98,8 +100,9 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
warning: string | null; warning: string | null;
nullable: boolean; nullable: boolean;
default: string | null; default: string | null;
values: Record<string, string>; values: B;
} },
B extends Record<string, string>
>(a: A) { >(a: A) {
return new Value({ return new Value({
type: "select" as const, type: "select" as const,
@@ -156,7 +159,10 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
}); });
} }
static list<A extends List<ValueSpecList>>(a: A) { static list<A extends ValueSpecList>(a: List<A>) {
return new Value(a.build()); return new Value(a.build());
} }
public validator() {
return guardAll(this.a);
}
} }

View File

@@ -10,7 +10,8 @@ testOutput<InputSpec["rpc"]["enable"], string>()(null);
testOutput<InputSpec["rpc"]["enable"], boolean>()(null); testOutput<InputSpec["rpc"]["enable"], boolean>()(null);
testOutput<InputSpec["rpc"]["username"], string>()(null); testOutput<InputSpec["rpc"]["username"], string>()(null);
// testOutput<InputSpec["rpc"]["advanced"]["auth"], string[]>()(null); testOutput<InputSpec["rpc"]["advanced"]["auth"], readonly string[]>()(null);
testOutput<InputSpec["rpc"]["advanced"]["serialversion"], readonly string[]>()(null);
describe("Inputs", () => { describe("Inputs", () => {
test("test", () => { test("test", () => {
expect(true).toEqual(true); expect(true).toEqual(true);

View File

@@ -42,21 +42,35 @@ type GuardObject<A> =
{ _error: "Invalid Spec" } { _error: "Invalid Spec" }
) : ) :
unknown unknown
// prettier-ignore // prettier-ignore
export type GuardList<A> = export type GuardList<A> =
A extends { readonly type: TypeList, readonly subtype: infer B, spec?: { spec?: infer C } } ? ReadonlyArray<GuardAll<Omit<A, "type" | "spec"> & ({ type: B, spec: C })>> : A extends { readonly type: TypeList, readonly subtype: infer B, spec?: { spec?: infer C } } ? ReadonlyArray<GuardAll<Omit<A, "type" | "subtype" | "spec"> & ({ type: B, spec: C })>> :
A extends { readonly type: TypeList, readonly subtype: infer B, spec?: {} } ? ReadonlyArray<GuardAll<Omit<A, "type"> & ({ type: B })>> : A extends { readonly type: TypeList, readonly subtype: infer B, spec?: {} } ? ReadonlyArray<GuardAll<Omit<A, "type"> & ({ type: B })>> :
unknown unknown
// prettier-ignore // prettier-ignore
type GuardSelect<A> = type GuardSelect<A> =
A extends { readonly type: TypeSelect, variants: { [key in infer B & string]: string } } ? B : A extends { readonly type: TypeSelect, variants: infer B } ? (
unknown B extends Record<string, string> ? keyof B : never
// prettier-ignore ) :
type GuardMultiselect<A> =
A extends { readonly type: TypeMultiselect, variants: { [key in infer B & string]: string } } ? B[] :
unknown unknown
const bluj: GuardSelect<{
type: "select";
} & {
readonly name: "Serialization Version";
readonly description: "Return raw transaction or block hex with Segwit or non-SegWit serialization.";
readonly warning: null;
readonly default: "segwit";
readonly nullable: false;
readonly values: {
...;
};
}>
// prettier-ignore
type GuardMultiselect<A> =
A extends { readonly type: TypeMultiselect, variants: { [key in infer B & string]: string } } ?B[] :
unknown
// prettier-ignore // prettier-ignore
type VariantValue<A> = type VariantValue<A> =
A extends { name: string, spec: infer B } ? { name: A['name'], spec: TypeFromProps<B> } : A extends { name: string, spec: infer B } ? { name: A['name'], spec: TypeFromProps<B> } :
@@ -158,13 +172,13 @@ export function matchNumberWithRange(range: string) {
leftValue === "*" leftValue === "*"
? (_) => true ? (_) => true
: left === "[" : left === "["
? (x) => x >= Number(leftValue) ? (x) => x >= Number(leftValue)
: (x) => x > Number(leftValue), : (x) => x > Number(leftValue),
leftValue === "*" leftValue === "*"
? "any" ? "any"
: left === "[" : left === "["
? `greaterThanOrEqualTo${leftValue}` ? `greaterThanOrEqualTo${leftValue}`
: `greaterThan${leftValue}` : `greaterThan${leftValue}`
) )
.validate( .validate(
// prettier-ignore // prettier-ignore

20
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "start-sdk", "name": "start-sdk",
"version": "0.4.0-alpha10", "version": "0.4.0-lib0.beta1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "start-sdk", "name": "start-sdk",
"version": "0.4.0-alpha10", "version": "0.4.0-lib0.beta1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@iarna/toml": "^2.2.5", "@iarna/toml": "^2.2.5",
@@ -20,7 +20,6 @@
"jest": "^29.4.3", "jest": "^29.4.3",
"ts-jest": "^29.0.5", "ts-jest": "^29.0.5",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"tsc": "^2.0.4",
"tsc-multi": "^0.6.1", "tsc-multi": "^0.6.1",
"tsconfig-paths": "^3.14.2", "tsconfig-paths": "^3.14.2",
"typescript": "^4.9.5", "typescript": "^4.9.5",
@@ -5042,15 +5041,6 @@
"node": ">=0.3.1" "node": ">=0.3.1"
} }
}, },
"node_modules/tsc": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/tsc/-/tsc-2.0.4.tgz",
"integrity": "sha512-fzoSieZI5KKJVBYGvwbVZs/J5za84f2lSTLPYf6AGiIf43tZ3GNrI1QzTLcjtyDDP4aLxd46RTZq1nQxe7+k5Q==",
"dev": true,
"bin": {
"tsc": "bin/tsc"
}
},
"node_modules/tsc-multi": { "node_modules/tsc-multi": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/tsc-multi/-/tsc-multi-0.6.1.tgz", "resolved": "https://registry.npmjs.org/tsc-multi/-/tsc-multi-0.6.1.tgz",
@@ -9399,12 +9389,6 @@
} }
} }
}, },
"tsc": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/tsc/-/tsc-2.0.4.tgz",
"integrity": "sha512-fzoSieZI5KKJVBYGvwbVZs/J5za84f2lSTLPYf6AGiIf43tZ3GNrI1QzTLcjtyDDP4aLxd46RTZq1nQxe7+k5Q==",
"dev": true
},
"tsc-multi": { "tsc-multi": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/tsc-multi/-/tsc-multi-0.6.1.tgz", "resolved": "https://registry.npmjs.org/tsc-multi/-/tsc-multi-0.6.1.tgz",

View File

@@ -1,10 +1,9 @@
{ {
"name": "start-sdk", "name": "start-sdk",
"version": "0.4.0-alpha10", "version": "0.4.0-lib0.beta1",
"description": "For making the patterns that are wanted in making services for the startOS.", "description": "For making the patterns that are wanted in making services for the startOS.",
"main": "./index.cjs", "main": "./lib/index.js",
"types": "./index.d.ts", "types": "./lib/index.d.ts",
"module": "./index.mjs",
"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",
@@ -32,7 +31,6 @@
"jest": "^29.4.3", "jest": "^29.4.3",
"ts-jest": "^29.0.5", "ts-jest": "^29.0.5",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"tsc": "^2.0.4",
"tsc-multi": "^0.6.1", "tsc-multi": "^0.6.1",
"tsconfig-paths": "^3.14.2", "tsconfig-paths": "^3.14.2",
"typescript": "^4.9.5", "typescript": "^4.9.5",

View File

@@ -98,9 +98,9 @@ export default async function makeFileContent(
)})`; )})`;
} }
case "enum": { case "enum": {
const allValueNames = new Set( const allValueNames = new Set([
...(value?.spec?.["values"] || []), ...(value?.["values"] || []),
...Object.keys(value?.spec?.["value-names"] || {}) ...Object.keys(value?.["value-names"] || {})]
); );
const values = Object.fromEntries( const values = Object.fromEntries(
Array.from(allValueNames) Array.from(allValueNames)
@@ -118,7 +118,7 @@ export default async function makeFileContent(
}, },
null, null,
2 2
)})`; )} as const)`;
} }
case "object": { case "object": {
const specName = newConst( const specName = newConst(
@@ -255,8 +255,8 @@ export default async function makeFileContent(
spec: { spec: {
variants: ${variants}, variants: ${variants},
displayAs: ${JSON.stringify( displayAs: ${JSON.stringify(
value?.spec?.["display-as"] || null value?.spec?.["display-as"] || null
)}, )},
uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)}, uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)},
default: ${JSON.stringify(value?.spec?.default || null)}, default: ${JSON.stringify(value?.spec?.default || null)},
}, },
@@ -277,9 +277,8 @@ export default async function makeFileContent(
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 = newConst(key, convertInputSpec(value));
answer += `"${key}": {name: "${ answer += `"${key}": {name: "${variantNames[key] || key
variantNames[key] || key }", spec: ${variantSpec}},`;
}", spec: ${variantSpec}},`;
} }
return `${answer}})`; return `${answer}})`;
} }

View File

@@ -1,6 +1,11 @@
{ {
"include": ["./lib/**/*.ts", "scripts/oldSpecToBuilder.ts"], "include": [
"inputs": ["./lib/index.ts"], "./lib/**/*.ts",
"scripts/oldSpecToBuilder.ts"
],
"inputs": [
"./lib/index.ts"
],
"compilerOptions": { "compilerOptions": {
"target": "es2022", "target": "es2022",
"module": "es2022", "module": "es2022",
@@ -9,9 +14,7 @@
"outDir": "./dist/", "outDir": "./dist/",
"esModuleInterop": true, "esModuleInterop": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strict": true, "strict": true,
"skipLibCheck": true "skipLibCheck": true
}, },
"ts-node": { "ts-node": {
@@ -19,4 +22,4 @@
"module": "commonjs" "module": "commonjs"
} }
} }
} }