@twin.org/ts-to-schema 0.0.3-next.4 → 0.0.3-next.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/es/cli.js CHANGED
@@ -20,7 +20,7 @@ export class CLI extends CLIBase {
20
20
  return this.execute({
21
21
  title: "TWIN TypeScript To Schema",
22
22
  appName: "ts-to-schema",
23
- version: "0.0.3-next.4", // x-release-please-version
23
+ version: "0.0.3-next.6", // x-release-please-version
24
24
  icon: "⚙️ ",
25
25
  supportsEnvFiles: false,
26
26
  overrideOutputWidth: options?.overrideOutputWidth
@@ -1 +1 @@
1
- {"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAElE;;GAEG;AACH,MAAM,OAAO,GAAI,SAAQ,OAAO;IAC/B;;;;;;;OAOG;IACI,KAAK,CAAC,GAAG,CACf,IAAc,EACd,gBAAyB,EACzB,OAA0C;QAE1C,OAAO,IAAI,CAAC,OAAO,CAClB;YACC,KAAK,EAAE,2BAA2B;YAClC,OAAO,EAAE,cAAc;YACvB,OAAO,EAAE,cAAc,EAAE,2BAA2B;YACpD,IAAI,EAAE,KAAK;YACX,gBAAgB,EAAE,KAAK;YACvB,mBAAmB,EAAE,OAAO,EAAE,mBAAmB;SACjD,EACD,gBAAgB,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,EACzF,IAAI,CACJ,CAAC;IACH,CAAC;IAED;;;OAGG;IACO,aAAa,CAAC,OAAgB;QACvC,sBAAsB,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { CLIBase } from \"@twin.org/cli-core\";\nimport type { Command } from \"commander\";\nimport { buildCommandTsToSchema } from \"./commands/tsToSchema.js\";\n\n/**\n * The main entry point for the CLI.\n */\nexport class CLI extends CLIBase {\n\t/**\n\t * Run the app.\n\t * @param argv The process arguments.\n\t * @param localesDirectory The directory for the locales, default to relative to the script.\n\t * @param options Additional options.\n\t * @param options.overrideOutputWidth Override the output width.\n\t * @returns The exit code.\n\t */\n\tpublic async run(\n\t\targv: string[],\n\t\tlocalesDirectory?: string,\n\t\toptions?: { overrideOutputWidth?: number }\n\t): Promise<number> {\n\t\treturn this.execute(\n\t\t\t{\n\t\t\t\ttitle: \"TWIN TypeScript To Schema\",\n\t\t\t\tappName: \"ts-to-schema\",\n\t\t\t\tversion: \"0.0.3-next.4\", // x-release-please-version\n\t\t\t\ticon: \"⚙️ \",\n\t\t\t\tsupportsEnvFiles: false,\n\t\t\t\toverrideOutputWidth: options?.overrideOutputWidth\n\t\t\t},\n\t\t\tlocalesDirectory ?? path.join(path.dirname(fileURLToPath(import.meta.url)), \"../locales\"),\n\t\t\targv\n\t\t);\n\t}\n\n\t/**\n\t * Configure any options or actions at the root program level.\n\t * @param program The root program command.\n\t */\n\tprotected configureRoot(program: Command): void {\n\t\tbuildCommandTsToSchema(program);\n\t}\n}\n"]}
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAElE;;GAEG;AACH,MAAM,OAAO,GAAI,SAAQ,OAAO;IAC/B;;;;;;;OAOG;IACI,KAAK,CAAC,GAAG,CACf,IAAc,EACd,gBAAyB,EACzB,OAA0C;QAE1C,OAAO,IAAI,CAAC,OAAO,CAClB;YACC,KAAK,EAAE,2BAA2B;YAClC,OAAO,EAAE,cAAc;YACvB,OAAO,EAAE,cAAc,EAAE,2BAA2B;YACpD,IAAI,EAAE,KAAK;YACX,gBAAgB,EAAE,KAAK;YACvB,mBAAmB,EAAE,OAAO,EAAE,mBAAmB;SACjD,EACD,gBAAgB,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,EACzF,IAAI,CACJ,CAAC;IACH,CAAC;IAED;;;OAGG;IACO,aAAa,CAAC,OAAgB;QACvC,sBAAsB,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { CLIBase } from \"@twin.org/cli-core\";\nimport type { Command } from \"commander\";\nimport { buildCommandTsToSchema } from \"./commands/tsToSchema.js\";\n\n/**\n * The main entry point for the CLI.\n */\nexport class CLI extends CLIBase {\n\t/**\n\t * Run the app.\n\t * @param argv The process arguments.\n\t * @param localesDirectory The directory for the locales, default to relative to the script.\n\t * @param options Additional options.\n\t * @param options.overrideOutputWidth Override the output width.\n\t * @returns The exit code.\n\t */\n\tpublic async run(\n\t\targv: string[],\n\t\tlocalesDirectory?: string,\n\t\toptions?: { overrideOutputWidth?: number }\n\t): Promise<number> {\n\t\treturn this.execute(\n\t\t\t{\n\t\t\t\ttitle: \"TWIN TypeScript To Schema\",\n\t\t\t\tappName: \"ts-to-schema\",\n\t\t\t\tversion: \"0.0.3-next.6\", // x-release-please-version\n\t\t\t\ticon: \"⚙️ \",\n\t\t\t\tsupportsEnvFiles: false,\n\t\t\t\toverrideOutputWidth: options?.overrideOutputWidth\n\t\t\t},\n\t\t\tlocalesDirectory ?? path.join(path.dirname(fileURLToPath(import.meta.url)), \"../locales\"),\n\t\t\targv\n\t\t);\n\t}\n\n\t/**\n\t * Configure any options or actions at the root program level.\n\t * @param program The root program command.\n\t */\n\tprotected configureRoot(program: Command): void {\n\t\tbuildCommandTsToSchema(program);\n\t}\n}\n"]}
@@ -78,6 +78,14 @@ export async function tsToSchema(config, outputFolder, workingDirectory) {
78
78
  }, undefined, "\t"));
79
79
  CLIDisplay.break();
80
80
  CLIDisplay.task(I18n.formatMessage("commands.ts-to-schema.progress.writingSchemas"));
81
+ const autoExpandTypes = config.autoExpandTypes ?? [];
82
+ const defaultExpandTypes = ["/ObjectOrArray<.*>/"];
83
+ for (const defaultType of defaultExpandTypes) {
84
+ if (!autoExpandTypes.includes(defaultType)) {
85
+ autoExpandTypes.push(defaultType);
86
+ }
87
+ }
88
+ let combinedSchemas = {};
81
89
  for (const typeSource of config.types) {
82
90
  const typeSourceParts = typeSource.split("/");
83
91
  const type = StringHelper.pascalCase(typeSourceParts[typeSourceParts.length - 1].replace(/(\.d)?\.ts$/, ""), false);
@@ -88,18 +96,15 @@ export async function tsToSchema(config, outputFolder, workingDirectory) {
88
96
  }
89
97
  else {
90
98
  CLIDisplay.task(I18n.formatMessage("commands.ts-to-schema.progress.generatingSchema"));
91
- const autoExpandTypes = config.autoExpandTypes ?? [];
92
- const defaultExpandTypes = ["/ObjectOrArray<.*>/"];
93
- for (const defaultType of defaultExpandTypes) {
94
- if (!autoExpandTypes.includes(defaultType)) {
95
- autoExpandTypes.push(defaultType);
99
+ CLIDisplay.value(I18n.formatMessage("commands.ts-to-schema.progress.models"), typeSource, 1);
100
+ if (!combinedSchemas[type]) {
101
+ const schemas = await generateSchemas(typeSource, type, autoExpandTypes, workingDirectory);
102
+ if (Is.empty(schemas[type])) {
103
+ throw new GeneralError("commands", "commands.ts-to-schema.schemaNotFound", { type });
96
104
  }
105
+ combinedSchemas = { ...combinedSchemas, ...schemas };
97
106
  }
98
- const schemas = await generateSchemas(typeSource, type, autoExpandTypes, workingDirectory);
99
- if (Is.empty(schemas[type])) {
100
- throw new GeneralError("commands", "commands.ts-to-schema.schemaNotFound", { type });
101
- }
102
- schemaObject = schemas[type];
107
+ schemaObject = combinedSchemas[type];
103
108
  }
104
109
  schemaObject = finaliseSchema(schemaObject, config.baseUrl, type);
105
110
  let content = JSON.stringify(schemaObject, undefined, "\t");
@@ -128,7 +133,6 @@ export async function tsToSchema(config, outputFolder, workingDirectory) {
128
133
  */
129
134
  async function generateSchemas(typeSource, type, autoExpandTypes, outputWorkingDir) {
130
135
  const allSchemas = {};
131
- CLIDisplay.value(I18n.formatMessage("commands.ts-to-schema.progress.models"), typeSource, 1);
132
136
  const generator = createGenerator({
133
137
  path: typeSource,
134
138
  type,
@@ -136,7 +140,7 @@ async function generateSchemas(typeSource, type, autoExpandTypes, outputWorkingD
136
140
  skipTypeCheck: true,
137
141
  expose: "all"
138
142
  });
139
- const schema = generator.createSchema("*");
143
+ const schema = generator.createSchema(type);
140
144
  if (schema.definitions) {
141
145
  for (const def in schema.definitions) {
142
146
  const defSub = JsonSchemaHelper.normaliseTypeName(def);
@@ -1 +1 @@
1
- {"version":3,"file":"tsToSchema.js","sourceRoot":"","sources":["../../../src/commands/tsToSchema.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACtE,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ED;;;;;;GAMG;AACH,SAAS,cAAc,CAAC,YAAyB,EAAE,OAAe,EAAE,IAAY;IAC/E,gBAAgB,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;IAC7C,MAAM,EAAE,WAAW,EAAE,GAAG,IAAI,EAAE,GAAG,YAAY,CAAC;IAC9C,OAAO;QACN,OAAO,EAAE,gBAAgB,CAAC,cAAc;QACxC,GAAG,EAAE,GAAG,OAAO,GAAG,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE;QAClD,WAAW;QACX,GAAG,IAAI;KACP,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { mkdir, rm, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { CLIDisplay, CLIUtils } from \"@twin.org/cli-core\";\nimport { GeneralError, I18n, Is, StringHelper } from \"@twin.org/core\";\nimport { type IJsonSchema, JsonSchemaHelper } from \"@twin.org/tools-core\";\nimport type { Command } from \"commander\";\nimport { createGenerator } from \"ts-json-schema-generator\";\nimport type { ITsToSchemaConfig } from \"../models/ITsToSchemaConfig.js\";\n\n/**\n * Build the root command to be consumed by the CLI.\n * @param program The command to build on.\n */\nexport function buildCommandTsToSchema(program: Command): void {\n\tprogram\n\t\t.argument(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.options.config.param\"),\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.options.config.description\")\n\t\t)\n\t\t.argument(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.options.output-folder.param\"),\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.options.output-folder.description\")\n\t\t)\n\t\t.action(async (config, outputFolder, opts) => {\n\t\t\tawait actionCommandTsToSchema(config, outputFolder, opts);\n\t\t});\n}\n\n/**\n * Action the root command.\n * @param configFile The optional configuration file.\n * @param outputFolder The output folder for the schemas.\n * @param opts The options for the command.\n */\nexport async function actionCommandTsToSchema(\n\tconfigFile: string,\n\toutputFolder: string,\n\topts: unknown\n): Promise<void> {\n\tlet outputWorkingDir: string | undefined;\n\ttry {\n\t\tlet config: ITsToSchemaConfig | undefined;\n\n\t\tconst fullConfigFile = path.resolve(configFile);\n\t\tconst fullOutputFolder = path.resolve(outputFolder);\n\t\toutputWorkingDir = path.join(fullOutputFolder, \"working\");\n\n\t\tCLIDisplay.value(I18n.formatMessage(\"commands.ts-to-schema.labels.configJson\"), fullConfigFile);\n\t\tCLIDisplay.value(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.labels.outputFolder\"),\n\t\t\tfullOutputFolder\n\t\t);\n\t\tCLIDisplay.value(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.labels.outputWorkingDir\"),\n\t\t\toutputWorkingDir\n\t\t);\n\t\tCLIDisplay.break();\n\n\t\ttry {\n\t\t\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.loadingConfigJson\"));\n\t\t\tCLIDisplay.break();\n\n\t\t\tconfig = await CLIUtils.readJsonFile<ITsToSchemaConfig>(fullConfigFile);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\"commands\", \"commands.ts-to-schema.configFailed\", undefined, err);\n\t\t}\n\n\t\tif (Is.empty(config)) {\n\t\t\tthrow new GeneralError(\"commands\", \"commands.ts-to-schema.configFailed\");\n\t\t}\n\n\t\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.creatingWorkingDir\"));\n\t\tawait mkdir(outputWorkingDir, { recursive: true });\n\t\tCLIDisplay.break();\n\n\t\tawait tsToSchema(config ?? {}, fullOutputFolder, outputWorkingDir);\n\n\t\tCLIDisplay.break();\n\t\tCLIDisplay.done();\n\t} finally {\n\t\ttry {\n\t\t\tif (outputWorkingDir) {\n\t\t\t\tawait rm(outputWorkingDir, { recursive: true });\n\t\t\t}\n\t\t} catch {}\n\t}\n}\n\n/**\n * Convert the TypeScript definitions to JSON Schemas.\n * @param config The configuration for the app.\n * @param outputFolder The location of the folder to output the JSON schemas.\n * @param workingDirectory The folder the app was run from.\n */\nexport async function tsToSchema(\n\tconfig: ITsToSchemaConfig,\n\toutputFolder: string,\n\tworkingDirectory: string\n): Promise<void> {\n\tawait writeFile(\n\t\tpath.join(workingDirectory, \"tsconfig.json\"),\n\t\tJSON.stringify(\n\t\t\t{\n\t\t\t\tcompilerOptions: {\n\t\t\t\t\tmodule: \"nodenext\",\n\t\t\t\t\tmoduleResolution: \"nodenext\",\n\t\t\t\t\ttarget: \"ES2022\"\n\t\t\t\t}\n\t\t\t},\n\t\t\tundefined,\n\t\t\t\"\\t\"\n\t\t)\n\t);\n\n\tCLIDisplay.break();\n\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.writingSchemas\"));\n\tfor (const typeSource of config.types) {\n\t\tconst typeSourceParts = typeSource.split(\"/\");\n\t\tconst type = StringHelper.pascalCase(\n\t\t\ttypeSourceParts[typeSourceParts.length - 1].replace(/(\\.d)?\\.ts$/, \"\"),\n\t\t\tfalse\n\t\t);\n\n\t\tlet schemaObject;\n\t\tif (Is.object<IJsonSchema>(config.overrides?.[type])) {\n\t\t\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.overridingSchema\"));\n\t\t\tschemaObject = config.overrides?.[type];\n\t\t} else {\n\t\t\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.generatingSchema\"));\n\n\t\t\tconst autoExpandTypes = config.autoExpandTypes ?? [];\n\t\t\tconst defaultExpandTypes = [\"/ObjectOrArray<.*>/\"];\n\t\t\tfor (const defaultType of defaultExpandTypes) {\n\t\t\t\tif (!autoExpandTypes.includes(defaultType)) {\n\t\t\t\t\tautoExpandTypes.push(defaultType);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst schemas = await generateSchemas(typeSource, type, autoExpandTypes, workingDirectory);\n\t\t\tif (Is.empty(schemas[type])) {\n\t\t\t\tthrow new GeneralError(\"commands\", \"commands.ts-to-schema.schemaNotFound\", { type });\n\t\t\t}\n\t\t\tschemaObject = schemas[type];\n\t\t}\n\n\t\tschemaObject = finaliseSchema(schemaObject, config.baseUrl, type);\n\n\t\tlet content = JSON.stringify(schemaObject, undefined, \"\\t\");\n\n\t\tif (Is.objectValue(config.externalReferences)) {\n\t\t\tfor (const external in config.externalReferences) {\n\t\t\t\tcontent = content.replace(\n\t\t\t\t\tnew RegExp(`#/definitions/${external}`, \"g\"),\n\t\t\t\t\tconfig.externalReferences[external]\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t// First replace all types that start with II to a single I with the new base url\n\t\tcontent = content.replace(/#\\/definitions\\/II(.*)/g, `${config.baseUrl}I$1`);\n\n\t\t// Then other types starting with capitals (optionally interfaces starting with I)\n\t\tcontent = content.replace(/#\\/definitions\\/I?([A-Z].*)/g, `${config.baseUrl}$1`);\n\n\t\tconst filename = path.join(outputFolder, `${StringHelper.stripPrefix(type)}.json`);\n\t\tCLIDisplay.value(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.progress.writingSchema\"),\n\t\t\tfilename,\n\t\t\t1\n\t\t);\n\t\tawait writeFile(filename, `${content}\\n`);\n\t}\n}\n\n/**\n * Generate schemas for the models.\n * @param modelDirWildcards The filenames for all the models.\n * @param types The types of the schema objects.\n * @param autoExpandTypes The types to automatically expand.\n * @param outputWorkingDir The working directory.\n * @returns Nothing.\n * @internal\n */\nasync function generateSchemas(\n\ttypeSource: string,\n\ttype: string,\n\tautoExpandTypes: string[],\n\toutputWorkingDir: string\n): Promise<{\n\t[id: string]: IJsonSchema;\n}> {\n\tconst allSchemas: { [id: string]: IJsonSchema } = {};\n\n\tCLIDisplay.value(I18n.formatMessage(\"commands.ts-to-schema.progress.models\"), typeSource, 1);\n\tconst generator = createGenerator({\n\t\tpath: typeSource,\n\t\ttype,\n\t\ttsconfig: path.join(outputWorkingDir, \"tsconfig.json\"),\n\t\tskipTypeCheck: true,\n\t\texpose: \"all\"\n\t});\n\n\tconst schema = generator.createSchema(\"*\");\n\n\tif (schema.definitions) {\n\t\tfor (const def in schema.definitions) {\n\t\t\tconst defSub = JsonSchemaHelper.normaliseTypeName(def);\n\n\t\t\tallSchemas[defSub] ??= schema.definitions[def] as IJsonSchema;\n\t\t}\n\t}\n\n\tconst referencedSchemas: { [id: string]: IJsonSchema } = {};\n\n\tJsonSchemaHelper.extractTypes(allSchemas, [type, ...autoExpandTypes], referencedSchemas);\n\tJsonSchemaHelper.expandTypes(referencedSchemas, autoExpandTypes);\n\n\treturn referencedSchemas;\n}\n\n/**\n * Process the schema object to ensure it has the correct properties.\n * @param schemaObject The schema object to process.\n * @param baseUrl The base URL for the schema references.\n * @param type The type of the schema object.\n * @returns The finalised schema object.\n */\nfunction finaliseSchema(schemaObject: IJsonSchema, baseUrl: string, type: string): IJsonSchema {\n\tJsonSchemaHelper.processArrays(schemaObject);\n\tconst { description, ...rest } = schemaObject;\n\treturn {\n\t\t$schema: JsonSchemaHelper.SCHEMA_VERSION,\n\t\t$id: `${baseUrl}${StringHelper.stripPrefix(type)}`,\n\t\tdescription,\n\t\t...rest\n\t};\n}\n"]}
1
+ {"version":3,"file":"tsToSchema.js","sourceRoot":"","sources":["../../../src/commands/tsToSchema.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAoB,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAE1E,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAG3D;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,OAAgB;IACtD,OAAO;SACL,QAAQ,CACR,IAAI,CAA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,cAAc,CAAC,YAAyB,EAAE,OAAe,EAAE,IAAY;IAC/E,gBAAgB,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;IAC7C,MAAM,EAAE,WAAW,EAAE,GAAG,IAAI,EAAE,GAAG,YAAY,CAAC;IAC9C,OAAO;QACN,OAAO,EAAE,gBAAgB,CAAC,cAAc;QACxC,GAAG,EAAE,GAAG,OAAO,GAAG,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE;QAClD,WAAW;QACX,GAAG,IAAI;KACP,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { mkdir, rm, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { CLIDisplay, CLIUtils } from \"@twin.org/cli-core\";\nimport { GeneralError, I18n, Is, StringHelper } from \"@twin.org/core\";\nimport { type IJsonSchema, JsonSchemaHelper } from \"@twin.org/tools-core\";\nimport type { Command } from \"commander\";\nimport { createGenerator } from \"ts-json-schema-generator\";\nimport type { ITsToSchemaConfig } from \"../models/ITsToSchemaConfig.js\";\n\n/**\n * Build the root command to be consumed by the CLI.\n * @param program The command to build on.\n */\nexport function buildCommandTsToSchema(program: Command): void {\n\tprogram\n\t\t.argument(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.options.config.param\"),\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.options.config.description\")\n\t\t)\n\t\t.argument(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.options.output-folder.param\"),\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.options.output-folder.description\")\n\t\t)\n\t\t.action(async (config, outputFolder, opts) => {\n\t\t\tawait actionCommandTsToSchema(config, outputFolder, opts);\n\t\t});\n}\n\n/**\n * Action the root command.\n * @param configFile The optional configuration file.\n * @param outputFolder The output folder for the schemas.\n * @param opts The options for the command.\n */\nexport async function actionCommandTsToSchema(\n\tconfigFile: string,\n\toutputFolder: string,\n\topts: unknown\n): Promise<void> {\n\tlet outputWorkingDir: string | undefined;\n\ttry {\n\t\tlet config: ITsToSchemaConfig | undefined;\n\n\t\tconst fullConfigFile = path.resolve(configFile);\n\t\tconst fullOutputFolder = path.resolve(outputFolder);\n\t\toutputWorkingDir = path.join(fullOutputFolder, \"working\");\n\n\t\tCLIDisplay.value(I18n.formatMessage(\"commands.ts-to-schema.labels.configJson\"), fullConfigFile);\n\t\tCLIDisplay.value(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.labels.outputFolder\"),\n\t\t\tfullOutputFolder\n\t\t);\n\t\tCLIDisplay.value(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.labels.outputWorkingDir\"),\n\t\t\toutputWorkingDir\n\t\t);\n\t\tCLIDisplay.break();\n\n\t\ttry {\n\t\t\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.loadingConfigJson\"));\n\t\t\tCLIDisplay.break();\n\n\t\t\tconfig = await CLIUtils.readJsonFile<ITsToSchemaConfig>(fullConfigFile);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\"commands\", \"commands.ts-to-schema.configFailed\", undefined, err);\n\t\t}\n\n\t\tif (Is.empty(config)) {\n\t\t\tthrow new GeneralError(\"commands\", \"commands.ts-to-schema.configFailed\");\n\t\t}\n\n\t\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.creatingWorkingDir\"));\n\t\tawait mkdir(outputWorkingDir, { recursive: true });\n\t\tCLIDisplay.break();\n\n\t\tawait tsToSchema(config ?? {}, fullOutputFolder, outputWorkingDir);\n\n\t\tCLIDisplay.break();\n\t\tCLIDisplay.done();\n\t} finally {\n\t\ttry {\n\t\t\tif (outputWorkingDir) {\n\t\t\t\tawait rm(outputWorkingDir, { recursive: true });\n\t\t\t}\n\t\t} catch {}\n\t}\n}\n\n/**\n * Convert the TypeScript definitions to JSON Schemas.\n * @param config The configuration for the app.\n * @param outputFolder The location of the folder to output the JSON schemas.\n * @param workingDirectory The folder the app was run from.\n */\nexport async function tsToSchema(\n\tconfig: ITsToSchemaConfig,\n\toutputFolder: string,\n\tworkingDirectory: string\n): Promise<void> {\n\tawait writeFile(\n\t\tpath.join(workingDirectory, \"tsconfig.json\"),\n\t\tJSON.stringify(\n\t\t\t{\n\t\t\t\tcompilerOptions: {\n\t\t\t\t\tmodule: \"nodenext\",\n\t\t\t\t\tmoduleResolution: \"nodenext\",\n\t\t\t\t\ttarget: \"ES2022\"\n\t\t\t\t}\n\t\t\t},\n\t\t\tundefined,\n\t\t\t\"\\t\"\n\t\t)\n\t);\n\n\tCLIDisplay.break();\n\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.writingSchemas\"));\n\n\tconst autoExpandTypes = config.autoExpandTypes ?? [];\n\tconst defaultExpandTypes = [\"/ObjectOrArray<.*>/\"];\n\tfor (const defaultType of defaultExpandTypes) {\n\t\tif (!autoExpandTypes.includes(defaultType)) {\n\t\t\tautoExpandTypes.push(defaultType);\n\t\t}\n\t}\n\n\tlet combinedSchemas: { [id: string]: IJsonSchema } = {};\n\tfor (const typeSource of config.types) {\n\t\tconst typeSourceParts = typeSource.split(\"/\");\n\t\tconst type = StringHelper.pascalCase(\n\t\t\ttypeSourceParts[typeSourceParts.length - 1].replace(/(\\.d)?\\.ts$/, \"\"),\n\t\t\tfalse\n\t\t);\n\n\t\tlet schemaObject;\n\t\tif (Is.object<IJsonSchema>(config.overrides?.[type])) {\n\t\t\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.overridingSchema\"));\n\t\t\tschemaObject = config.overrides?.[type];\n\t\t} else {\n\t\t\tCLIDisplay.task(I18n.formatMessage(\"commands.ts-to-schema.progress.generatingSchema\"));\n\t\t\tCLIDisplay.value(I18n.formatMessage(\"commands.ts-to-schema.progress.models\"), typeSource, 1);\n\n\t\t\tif (!combinedSchemas[type]) {\n\t\t\t\tconst schemas = await generateSchemas(typeSource, type, autoExpandTypes, workingDirectory);\n\t\t\t\tif (Is.empty(schemas[type])) {\n\t\t\t\t\tthrow new GeneralError(\"commands\", \"commands.ts-to-schema.schemaNotFound\", { type });\n\t\t\t\t}\n\t\t\t\tcombinedSchemas = { ...combinedSchemas, ...schemas };\n\t\t\t}\n\t\t\tschemaObject = combinedSchemas[type];\n\t\t}\n\n\t\tschemaObject = finaliseSchema(schemaObject, config.baseUrl, type);\n\n\t\tlet content = JSON.stringify(schemaObject, undefined, \"\\t\");\n\n\t\tif (Is.objectValue(config.externalReferences)) {\n\t\t\tfor (const external in config.externalReferences) {\n\t\t\t\tcontent = content.replace(\n\t\t\t\t\tnew RegExp(`#/definitions/${external}`, \"g\"),\n\t\t\t\t\tconfig.externalReferences[external]\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t// First replace all types that start with II to a single I with the new base url\n\t\tcontent = content.replace(/#\\/definitions\\/II(.*)/g, `${config.baseUrl}I$1`);\n\n\t\t// Then other types starting with capitals (optionally interfaces starting with I)\n\t\tcontent = content.replace(/#\\/definitions\\/I?([A-Z].*)/g, `${config.baseUrl}$1`);\n\n\t\tconst filename = path.join(outputFolder, `${StringHelper.stripPrefix(type)}.json`);\n\t\tCLIDisplay.value(\n\t\t\tI18n.formatMessage(\"commands.ts-to-schema.progress.writingSchema\"),\n\t\t\tfilename,\n\t\t\t1\n\t\t);\n\t\tawait writeFile(filename, `${content}\\n`);\n\t}\n}\n\n/**\n * Generate schemas for the models.\n * @param modelDirWildcards The filenames for all the models.\n * @param types The types of the schema objects.\n * @param autoExpandTypes The types to automatically expand.\n * @param outputWorkingDir The working directory.\n * @returns Nothing.\n * @internal\n */\nasync function generateSchemas(\n\ttypeSource: string,\n\ttype: string,\n\tautoExpandTypes: string[],\n\toutputWorkingDir: string\n): Promise<{\n\t[id: string]: IJsonSchema;\n}> {\n\tconst allSchemas: { [id: string]: IJsonSchema } = {};\n\n\tconst generator = createGenerator({\n\t\tpath: typeSource,\n\t\ttype,\n\t\ttsconfig: path.join(outputWorkingDir, \"tsconfig.json\"),\n\t\tskipTypeCheck: true,\n\t\texpose: \"all\"\n\t});\n\n\tconst schema = generator.createSchema(type);\n\n\tif (schema.definitions) {\n\t\tfor (const def in schema.definitions) {\n\t\t\tconst defSub = JsonSchemaHelper.normaliseTypeName(def);\n\n\t\t\tallSchemas[defSub] ??= schema.definitions[def] as IJsonSchema;\n\t\t}\n\t}\n\n\tconst referencedSchemas: { [id: string]: IJsonSchema } = {};\n\n\tJsonSchemaHelper.extractTypes(allSchemas, [type, ...autoExpandTypes], referencedSchemas);\n\tJsonSchemaHelper.expandTypes(referencedSchemas, autoExpandTypes);\n\n\treturn referencedSchemas;\n}\n\n/**\n * Process the schema object to ensure it has the correct properties.\n * @param schemaObject The schema object to process.\n * @param baseUrl The base URL for the schema references.\n * @param type The type of the schema object.\n * @returns The finalised schema object.\n */\nfunction finaliseSchema(schemaObject: IJsonSchema, baseUrl: string, type: string): IJsonSchema {\n\tJsonSchemaHelper.processArrays(schemaObject);\n\tconst { description, ...rest } = schemaObject;\n\treturn {\n\t\t$schema: JsonSchemaHelper.SCHEMA_VERSION,\n\t\t$id: `${baseUrl}${StringHelper.stripPrefix(type)}`,\n\t\tdescription,\n\t\t...rest\n\t};\n}\n"]}
package/docs/changelog.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # @twin.org/ts-to-schema - Changelog
2
2
 
3
+ ## [0.0.3-next.6](https://github.com/twinfoundation/tools/compare/ts-to-schema-v0.0.3-next.5...ts-to-schema-v0.0.3-next.6) (2026-01-28)
4
+
5
+
6
+ ### Miscellaneous Chores
7
+
8
+ * **ts-to-schema:** Synchronize repo versions
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/tools-core bumped from 0.0.3-next.5 to 0.0.3-next.6
16
+
17
+ ## [0.0.3-next.5](https://github.com/twinfoundation/tools/compare/ts-to-schema-v0.0.3-next.4...ts-to-schema-v0.0.3-next.5) (2026-01-06)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * speed up schema generation ([#64](https://github.com/twinfoundation/tools/issues/64)) ([2dc1829](https://github.com/twinfoundation/tools/commit/2dc1829965cb5917d6672bc633e47788a361b071))
23
+
24
+
25
+ ### Dependencies
26
+
27
+ * The following workspace dependencies were updated
28
+ * dependencies
29
+ * @twin.org/tools-core bumped from 0.0.3-next.4 to 0.0.3-next.5
30
+
3
31
  ## [0.0.3-next.4](https://github.com/twinfoundation/tools/compare/ts-to-schema-v0.0.3-next.3...ts-to-schema-v0.0.3-next.4) (2026-01-05)
4
32
 
5
33
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/ts-to-schema",
3
- "version": "0.0.3-next.4",
3
+ "version": "0.0.3-next.6",
4
4
  "description": "Tool to convert TypeScript definitions to JSON schemas",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,7 +17,7 @@
17
17
  "@twin.org/cli-core": "next",
18
18
  "@twin.org/core": "next",
19
19
  "@twin.org/nameof": "next",
20
- "@twin.org/tools-core": "0.0.3-next.4",
20
+ "@twin.org/tools-core": "0.0.3-next.6",
21
21
  "commander": "14.0.2",
22
22
  "glob": "11.0.3",
23
23
  "ts-json-schema-generator": "2.4.0"