diff --git a/config_builder/mod.ts b/config_builder/mod.ts index 4077a35..bb761f9 100644 --- a/config_builder/mod.ts +++ b/config_builder/mod.ts @@ -1,4 +1,13 @@ -export { Config } from "./config.ts"; -export { List } from "./list.ts"; -export { Value } from "./value.ts"; -export { Variants } from "./variants.ts"; +import { Config } from "./config.ts"; +import { List } from "./list.ts"; +import { Value } from "./value.ts"; +import { Variants } from "./variants.ts"; + +export { + /** @typedef { import("./config.ts").Config } Pet + */ + Config, + List, + Value, + Variants, +}; diff --git a/config_tools/config_file.ts b/config_tools/config_file.ts index 57d542a..6acaa15 100644 --- a/config_tools/config_file.ts +++ b/config_tools/config_file.ts @@ -17,17 +17,16 @@ const previousPath = /(.+?)\/([^/]*)$/; }) const jsonFile = ConfigFile.json({ path: 'data.json', - data: someValidator, + validator: someValidator, volume: 'main' }) const tomlFile = ConfigFile.toml({ path: 'data.toml', - data: someValidator, + validator: someValidator, volume: 'main' }) const rawFile = ConfigFile.raw({ path: 'data.amazingSettings', - data: someValidator, volume: 'main' fromData(dataIn: Data): string { return `myDatais ///- ${dataIn.data}` @@ -39,7 +38,7 @@ const previousPath = /(.+?)\/([^/]*)$/; }) export const setConfig : T.ExpectedExports.setConfig= async (effects, config) => { - await jsonFile.write({ data: 'here lies data'}, effects) + await jsonFile.write({ data: 'here lies data'}, effects) } export const getConfig: T.ExpectedExports.getConfig = async (effects, config) => ({ @@ -56,12 +55,16 @@ export class ConfigFile { volume: string; writeData(dataIn: A): string; readData(stringValue: string): A; - } + }, ) {} async write(data: A, effects: T.Effects) { let matched; - if ((matched = previousPath.exec(this.options.path))) - await effects.createDir({ volumeId: this.options.volume, path: matched[1] }); + if ((matched = previousPath.exec(this.options.path))) { + await effects.createDir({ + volumeId: this.options.volume, + path: matched[1], + }); + } await effects.writeFile({ path: this.options.path, @@ -74,10 +77,17 @@ export class ConfigFile { await effects.readFile({ path: this.options.path, volumeId: this.options.volume, - }) + }), ); } - static raw(options: { path: string; volume: string; fromData(dataIn: A): string; toData(rawData: string): A }) { + static raw( + options: { + path: string; + volume: string; + fromData(dataIn: A): string; + toData(rawData: string): A; + }, + ) { return new ConfigFile({ path: options.path, volume: options.volume, @@ -85,7 +95,13 @@ export class ConfigFile { readData: options.toData, }); } - static json(options: { path: string; volume: string; data: matches.Validator }) { + static json( + options: { + path: string; + volume: string; + validator: matches.Validator; + }, + ) { return new ConfigFile({ path: options.path, volume: options.volume, @@ -93,14 +109,14 @@ export class ConfigFile { return JSON.stringify(inData, null, 2); }, readData(inString) { - return options.data.unsafeCast(JSON.parse(inString)); + return options.validator.unsafeCast(JSON.parse(inString)); }, }); } static toml>(options: { path: string; volume: string; - data: matches.Validator; + validator: matches.Validator; }) { return new ConfigFile({ path: options.path, @@ -109,14 +125,14 @@ export class ConfigFile { return TOML.stringify(inData); }, readData(inString) { - return options.data.unsafeCast(TOML.parse(inString)); + return options.validator.unsafeCast(TOML.parse(inString)); }, }); } static yaml>(options: { path: string; volume: string; - data: matches.Validator; + validator: matches.Validator; }) { return new ConfigFile({ path: options.path, @@ -125,7 +141,7 @@ export class ConfigFile { return YAML.stringify(inData); }, readData(inString) { - return options.data.unsafeCast(YAML.parse(inString)); + return options.validator.unsafeCast(YAML.parse(inString)); }, }); } diff --git a/scripts/oldSpecToBuilder.ts b/scripts/oldSpecToBuilder.ts index f805c61..e91587a 100644 --- a/scripts/oldSpecToBuilder.ts +++ b/scripts/oldSpecToBuilder.ts @@ -16,9 +16,12 @@ console.log(` const data = JSON.parse(list.join("\n")); const namedConsts = new Set(["Config", "Value", "List"]); -const configName = newConst("config", convertConfigSpec(data)); -const configMatcherName = newConst("matchConfig", `${configName}.validator()`); -console.log(`export type Config = typeof ${configMatcherName}._TYPE;`); +const configName = newConst("configSpec", convertConfigSpec(data)); +const configMatcherName = newConst( + "matchConfigSpec", + `${configName}.validator()`, +); +console.log(`export type ConfigSpec = typeof ${configMatcherName}._TYPE;`); function newConst(key: string, data: string) { const variableName = getNextConstName(camelCase(key)); @@ -37,68 +40,79 @@ function convertConfigSpec(data: any) { function convertValueSpec(value: any): string { switch (value.type) { case "string": { - return `Value.string(${JSON.stringify( - { - name: value.name || null, - default: value.default || null, - description: value.description || null, - warning: value.warning || null, - nullable: value.nullable || false, - masked: value.masked || null, - placeholder: value.placeholder || null, - pattern: value.pattern || null, - "pattern-description": value["pattern-description"] || null, - textarea: value.textarea || null, - }, - null, - 2 - )})`; + return `Value.string(${ + JSON.stringify( + { + name: value.name || null, + default: value.default || null, + description: value.description || null, + warning: value.warning || null, + nullable: value.nullable || false, + masked: value.masked || null, + placeholder: value.placeholder || null, + pattern: value.pattern || null, + "pattern-description": value["pattern-description"] || null, + textarea: value.textarea || null, + }, + null, + 2, + ) + })`; } case "number": { - return `Value.number(${JSON.stringify( - { - name: value.name || null, - default: value.default || null, - description: value.description || null, - warning: value.warning || null, - nullable: value.nullable || false, - range: value.range || null, - integral: value.integral || false, - units: value.units || null, - placeholder: value.placeholder || null, - }, - null, - 2 - )})`; + return `Value.number(${ + JSON.stringify( + { + name: value.name || null, + default: value.default || null, + description: value.description || null, + warning: value.warning || null, + nullable: value.nullable || false, + range: value.range || null, + integral: value.integral || false, + units: value.units || null, + placeholder: value.placeholder || null, + }, + null, + 2, + ) + })`; } case "boolean": { - return `Value.boolean(${JSON.stringify( - { - name: value.name || null, - default: value.default || false, - description: value.description || null, - warning: value.warning || null, - }, - null, - 2 - )})`; + return `Value.boolean(${ + JSON.stringify( + { + name: value.name || null, + default: value.default || false, + description: value.description || null, + warning: value.warning || null, + }, + null, + 2, + ) + })`; } case "enum": { - return `Value.enum(${JSON.stringify( - { - name: value.name || null, - description: value.description || null, - warning: value.warning || null, - default: value.default || null, - values: value.values || null, - "value-names": value["value-names"] || null, - }, - null, - 2 - )})`; + return `Value.enum(${ + JSON.stringify( + { + name: value.name || null, + description: value.description || null, + warning: value.warning || null, + default: value.default || null, + values: value.values || null, + "value-names": value["value-names"] || null, + }, + null, + 2, + ) + })`; } case "object": { - const specName = newConst(value.name + "_spec", convertConfigSpec(value.spec)); + const specName = newConst( + value.name + "_spec", + convertConfigSpec(value.spec), + ); return `Value.object({ name: ${JSON.stringify(value.name || null)}, description: ${JSON.stringify(value.description || null)}, @@ -111,23 +125,30 @@ function convertValueSpec(value: any): string { })`; } case "union": { - const variants = newConst(value.name + "_variants", convertVariants(value.variants)); + const variants = newConst( + value.name + "_variants", + convertVariants(value.variants), + ); return `Value.union({ name: ${JSON.stringify(value.name || null)}, description: ${JSON.stringify(value.description || null)}, warning: ${JSON.stringify(value.warning || null)}, default: ${JSON.stringify(value.default || null)}, variants: ${variants}, - tag: ${JSON.stringify({ + tag: ${ + JSON.stringify({ id: value?.tag?.["id"] || null, name: value?.tag?.["name"] || null, description: value?.tag?.["description"] || null, warning: value?.tag?.["warning"] || null, "variant-names": value?.tag?.["variant-names"] || {}, - })}, + }) + }, "display-as": ${JSON.stringify(value["display-as"] || null)}, "unique-by": ${JSON.stringify(value["unique-by"] || null)}, - "variant-names": ${JSON.stringify((value["variant-names"] as any) || null)}, + "variant-names": ${ + JSON.stringify((value["variant-names"] as any) || null) + }, })`; } case "list": { @@ -144,69 +165,81 @@ function convertValueSpec(value: any): string { function convertList(value: any) { switch (value.subtype) { case "string": { - return `List.string(${JSON.stringify( - { - name: value.name || null, - range: value.range || null, - spec: { - masked: value?.spec?.["masked"] || null, - placeholder: value?.spec?.["placeholder"] || null, - pattern: value?.spec?.["pattern"] || null, - "pattern-description": value?.spec?.["pattern-description"] || null, - textarea: value?.spec?.["textarea"] || false, + return `List.string(${ + JSON.stringify( + { + name: value.name || null, + range: value.range || null, + spec: { + masked: value?.spec?.["masked"] || null, + placeholder: value?.spec?.["placeholder"] || null, + pattern: value?.spec?.["pattern"] || null, + "pattern-description": value?.spec?.["pattern-description"] || + null, + textarea: value?.spec?.["textarea"] || false, + }, + default: value.default || null, + description: value.description || null, + warning: value.warning || null, }, - default: value.default || null, - description: value.description || null, - warning: value.warning || null, - }, - null, - 2 - )})`; + null, + 2, + ) + })`; } case "number": { - return `List.number(${JSON.stringify( - { - name: value.name || null, - range: value.range || null, - spec: { - range: value?.spec?.range || null, - integral: value?.spec?.integral || false, - units: value?.spec?.units || null, - placeholder: value?.spec?.placeholder || null, + return `List.number(${ + JSON.stringify( + { + name: value.name || null, + range: value.range || null, + spec: { + range: value?.spec?.range || null, + integral: value?.spec?.integral || false, + units: value?.spec?.units || null, + placeholder: value?.spec?.placeholder || null, + }, + default: value.default || null, + description: value.description || null, + warning: value.warning || null, }, - default: value.default || null, - description: value.description || null, - warning: value.warning || null, - }, - null, - 2 - )})`; + null, + 2, + ) + })`; } case "enum": { - return `List.enum(${JSON.stringify( - { - name: value.name || null, - range: value.range || null, - spec: { - values: value?.spec?.["values"] || null, - "value-names": value?.spec?.["value-names"] || {}, + return `List.enum(${ + JSON.stringify( + { + name: value.name || null, + range: value.range || null, + spec: { + values: value?.spec?.["values"] || null, + "value-names": value?.spec?.["value-names"] || {}, + }, + default: value.default || null, + description: value.description || null, + warning: value.warning || null, }, - default: value.default || null, - description: value.description || null, - warning: value.warning || null, - }, - null, - 2 - )})`; + null, + 2, + ) + })`; } case "object": { - const specName = newConst(value.name + "_spec", convertConfigSpec(value.spec.spec)); + const specName = newConst( + value.name + "_spec", + convertConfigSpec(value.spec.spec), + ); return `List.obj({ name: ${JSON.stringify(value.name || null)}, range: ${JSON.stringify(value.range || null)}, spec: { spec: ${specName}, - "display-as": ${JSON.stringify(value?.spec?.["display-as"] || null)}, + "display-as": ${ + JSON.stringify(value?.spec?.["display-as"] || null) + }, "unique-by": ${JSON.stringify(value?.spec?.["unique-by"] || null)}, }, default: ${JSON.stringify(value.default || null)}, @@ -215,7 +248,10 @@ function convertList(value: any) { })`; } case "union": { - const variants = newConst(value.name + "_variants", convertConfigSpec(value.spec.variants)); + const variants = newConst( + value.name + "_variants", + convertConfigSpec(value.spec.variants), + ); return `List.union( { name:${JSON.stringify(value.name || null)}, @@ -223,14 +259,26 @@ function convertList(value: any) { spec: { tag: { "id":${JSON.stringify(value?.spec?.tag?.["id"] || null)}, - "name": ${JSON.stringify(value?.spec?.tag?.["name"] || null)}, - "description": ${JSON.stringify(value?.spec?.tag?.["description"] || null)}, - "warning": ${JSON.stringify(value?.spec?.tag?.["warning"] || null)}, - "variant-names": ${JSON.stringify(value?.spec?.tag?.["variant-names"] || {})}, + "name": ${ + JSON.stringify(value?.spec?.tag?.["name"] || null) + }, + "description": ${ + JSON.stringify(value?.spec?.tag?.["description"] || null) + }, + "warning": ${ + JSON.stringify(value?.spec?.tag?.["warning"] || null) + }, + "variant-names": ${ + JSON.stringify(value?.spec?.tag?.["variant-names"] || {}) + }, }, variants: ${variants}, - "display-as": ${JSON.stringify(value?.spec?.["display-as"] || null)}, - "unique-by": ${JSON.stringify(value?.spec?.["unique-by"] || null)}, + "display-as": ${ + JSON.stringify(value?.spec?.["display-as"] || null) + }, + "unique-by": ${ + JSON.stringify(value?.spec?.["unique-by"] || null) + }, default: ${JSON.stringify(value?.spec?.["default"] || null)}, }, default: ${JSON.stringify(value.default || null)}, diff --git a/utils/propertiesMatcher.test.ts b/utils/propertiesMatcher.test.ts index d2688b4..f45019e 100644 --- a/utils/propertiesMatcher.test.ts +++ b/utils/propertiesMatcher.test.ts @@ -1,7 +1,7 @@ import * as PM from "./propertiesMatcher.ts"; import { expect } from "https://deno.land/x/expect@v0.2.9/mod.ts"; import { matches } from "../dependencies.ts"; -import { config as bitcoinPropertiesConfig } from "./test/output.ts"; +import { configSpec as bitcoinPropertiesConfig } from "./test/output.ts"; const randWithSeed = (seed = 1) => { return function random() { diff --git a/utils/test/output.ts b/utils/test/output.ts index a01882c..375acb2 100644 --- a/utils/test/output.ts +++ b/utils/test/output.ts @@ -1,7 +1,7 @@ import { configBuilder } from "../../mod.ts"; const { Config, Value, List, Variants } = configBuilder; -export const enable = Value.boolean({ +export const enable = configBuilder.Value.boolean({ "name": "Enable", "default": true, "description": "Allow remote RPC requests.", @@ -443,12 +443,12 @@ export const advanced1 = Value.object({ spec: advancedSpec1, "value-names": {}, }); -export const config = Config.of({ +export const configSpec = configBuilder.Config.of({ "rpc": rpc, "zmq-enabled": zmqEnabled, "txindex": txindex, "wallet": wallet, "advanced": advanced1, }); -export const matchConfig = config.validator(); -export type Config = typeof matchConfig._TYPE; +export const matchConfigSpec = configSpec.validator(); +export type ConfigSpec = typeof matchConfigSpec._TYPE;