type-crafter 0.8.0 → 0.8.2

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/index.js CHANGED
@@ -19409,7 +19409,16 @@ function resolveFilePath(filePath, useCurrentDirectory = true) {
19409
19409
  : path$1.join(useCurrentDirectory ? process.cwd() : getSourceFileDirectory(), filePath);
19410
19410
  return path$1.resolve(normalizedPath);
19411
19411
  }
19412
- async function readFile(filePath, useCurrentWorkingDirectory = true) {
19412
+ async function checkFileNameCase(filePath) {
19413
+ const directory = path$1.dirname(filePath);
19414
+ const fileName = path$1.basename(filePath);
19415
+ const files = await fs$1.readdir(directory);
19416
+ return files.includes(fileName);
19417
+ }
19418
+ async function readFile(filePath, useCurrentWorkingDirectory = true, caseSensitive = false) {
19419
+ if (caseSensitive && !(await checkFileNameCase(filePath))) {
19420
+ throw new Error(`File not found: ${filePath}`);
19421
+ }
19413
19422
  const data = await fs$1.readFile(resolveFilePath(filePath, useCurrentWorkingDirectory), 'utf-8');
19414
19423
  return data;
19415
19424
  }
@@ -19454,7 +19463,7 @@ async function writeFile(basePath, fileName, content) {
19454
19463
  await fs$1.writeFile(filePath, content);
19455
19464
  }
19456
19465
  async function readYaml(filePath) {
19457
- const fileData = await readFile(filePath);
19466
+ const fileData = await readFile(filePath, true, true);
19458
19467
  return YAML.parse(fileData);
19459
19468
  }
19460
19469
 
@@ -19573,9 +19582,21 @@ function registerTemplateHelpers() {
19573
19582
  Handlebars.registerHelper('toPascalCase', toPascalCaseHelper);
19574
19583
  Handlebars.registerHelper('isNonEmptyArray', (value) => Array.isArray(value) && value.length === 0);
19575
19584
  Handlebars.registerHelper('eq', (value1, value2) => value1 === value2);
19585
+ Handlebars.registerHelper('notEq', (value1, value2) => value1 !== value2);
19586
+ Handlebars.registerHelper('isEmptyObject', (value) => typeof value === 'object' && value !== null && Object.keys(value).length === 0);
19576
19587
  Handlebars.registerHelper('jsonKey', refineJSONKey);
19577
19588
  Handlebars.registerHelper('variableName', refineVariableName);
19578
19589
  Handlebars.registerHelper('indexKey', refineIndexKey);
19590
+ Handlebars.registerHelper('not', (value) => {
19591
+ if (typeof value === 'boolean') {
19592
+ return !value;
19593
+ }
19594
+ });
19595
+ Handlebars.registerHelper('or', (value1, value2) => {
19596
+ if (typeof value1 === 'boolean' && typeof value2 === 'boolean') {
19597
+ return value1 || value2;
19598
+ }
19599
+ });
19579
19600
  }
19580
19601
  function readNestedValue(json, keyPath) {
19581
19602
  if (!isJSON(json)) {
@@ -19789,7 +19810,7 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
19789
19810
  summary: typeInfo.summary,
19790
19811
  properties: {}
19791
19812
  };
19792
- let recursiveTypeGenOutput = null;
19813
+ const compositions = [];
19793
19814
  let dynamicGeneratedType = '';
19794
19815
  const primitives = [];
19795
19816
  const references = [];
@@ -19832,8 +19853,9 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
19832
19853
  composerType = arrayDataGenOutput.templateInput.composerType ?? null;
19833
19854
  }
19834
19855
  else if (propertyType === 'object') {
19835
- recursivePropertyName = toPascalCase(propertyName);
19836
- recursiveTypeGenOutput = await generateObjectType(recursivePropertyName, typeInfo.properties[propertyName], parentTypes);
19856
+ recursivePropertyName = typeName + toPascalCase(propertyName);
19857
+ const recursiveTypeGenOutput = await generateObjectType(recursivePropertyName, typeInfo.properties[propertyName], parentTypes);
19858
+ compositions.push(recursiveTypeGenOutput);
19837
19859
  languageDataType = recursivePropertyName;
19838
19860
  references.push(...recursiveTypeGenOutput.references);
19839
19861
  primitives.push(...recursiveTypeGenOutput.primitives);
@@ -19869,7 +19891,9 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
19869
19891
  }
19870
19892
  const result = {
19871
19893
  content: Runtime.getObjectTemplate()(templateInput) +
19872
- (recursiveTypeGenOutput?.content ?? '') +
19894
+ compositions.reduce((acc, curr) => {
19895
+ return acc + curr.content;
19896
+ }, '') +
19873
19897
  dynamicGeneratedType,
19874
19898
  primitives: new Set(primitives),
19875
19899
  references: new Set(references),
@@ -19993,13 +20017,13 @@ async function generateOneOfTypes(typeName, typeInfo, parentTypes) {
19993
20017
  for (let index = 0; index < typeInfo.oneOf.length; index++) {
19994
20018
  const oneOfItem = typeInfo.oneOf[index];
19995
20019
  if (oneOfItem.$ref !== null) {
19996
- const referenceData = await resolveTypeReference(oneOfItem.$ref);
20020
+ const referenceData = await generateReferencedType(typeName + (index + 1), oneOfItem, parentTypes);
19997
20021
  const composition = {
19998
20022
  source: 'referenced',
19999
- referencedType: referenceData.name
20023
+ referencedType: referenceData.templateInput.typeName
20000
20024
  };
20001
20025
  templateInput.compositions.push(composition);
20002
- result.references.add(referenceData.name);
20026
+ result.references.add(referenceData.templateInput.typeName);
20003
20027
  }
20004
20028
  else {
20005
20029
  const generatedType = await generateType(typeName + (index + 1), oneOfItem, parentTypes);
@@ -20045,13 +20069,13 @@ async function generateAllOfTypes(typeName, typeInfo, parentTypes) {
20045
20069
  for (let index = 0; index < typeInfo.allOf.length; index++) {
20046
20070
  const allOfItem = typeInfo.allOf[index];
20047
20071
  if (allOfItem.$ref !== null) {
20048
- const referenceData = await resolveTypeReference(allOfItem.$ref);
20072
+ const referenceData = await generateReferencedType(typeName + 'AllOf' + index, allOfItem, parentTypes);
20049
20073
  const composition = {
20050
20074
  source: 'referenced',
20051
- referencedType: referenceData.name
20075
+ referencedType: referenceData.templateInput.typeName
20052
20076
  };
20053
20077
  templateInput.compositions.push(composition);
20054
- result.references.add(referenceData.name);
20078
+ result.references.add(referenceData.templateInput.typeName);
20055
20079
  }
20056
20080
  else {
20057
20081
  const generatedType = await generateType(typeName + (index + 1), allOfItem, parentTypes);
@@ -20484,7 +20508,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
20484
20508
  }
20485
20509
  }
20486
20510
  greeting();
20487
- const program = new Command().version('0.8.0');
20511
+ const program = new Command().version('0.8.2');
20488
20512
  program
20489
20513
  .command('generate')
20490
20514
  .description('Generate types for your language from a type spec file')
@@ -7,7 +7,7 @@
7
7
  * @example {{{example}}}
8
8
  {{/if}}
9
9
  */
10
- export type {{typeName}} = {
10
+ export type {{typeName}} = {{#if (not (isEmptyObject properties))}} {
11
11
  {{#each properties}}
12
12
  /**
13
13
  {{#if this.description}}
@@ -25,6 +25,9 @@ export type {{typeName}} = {
25
25
  [keys: {{jsonKey additionalProperties.keyType}}]: {{additionalProperties.valueType}};
26
26
  {{/if}}
27
27
  };
28
+ {{else if (or (notEq length additionalProperties 0) (not (isEmptyObject additionalProperties)))}}
29
+ Record<{{jsonKey additionalProperties.keyType}}, {{additionalProperties.valueType}}>;
30
+ {{/if}}
28
31
 
29
32
  export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
30
33
  if (isJSON(rawInput)) {
@@ -1,11 +1,11 @@
1
1
  export type {{typeName}} =
2
2
  {{#each compositions}}
3
- | {{#if (eq this.source 'referenced')}}C{{referencedType}}
3
+ | {{#if (eq this.source 'referenced')}}C{{../typeName}}{{referencedType}}
4
4
  {{else if (eq this.source 'inline')}}
5
5
  {{#if this.templateInput.values}}
6
6
  {{this.templateInput.typeName}}
7
7
  {{else if (eq this.dataType 'object')}}
8
- C{{this.templateInput.typeName}}
8
+ C{{typeName}}{{this.templateInput.typeName}}
9
9
  {{else}}
10
10
  {{this.templateInput.type}}
11
11
  {{/if}}
@@ -16,7 +16,7 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
16
16
  const result: {{typeName}} | null =
17
17
  {{#each compositions}}
18
18
  {{#if (eq this.source 'referenced')}}
19
- decodeC{{referencedType}}(rawInput)
19
+ decodeC{{../typeName}}{{referencedType}}(rawInput)
20
20
  {{else if (eq this.dataType 'object')}}
21
21
  decodeC{{this.templateInput.typeName}}(rawInput)
22
22
  {{else if (eq this.dataType 'array')}}
@@ -36,26 +36,26 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
36
36
  {{#if this.templateInput.values}}
37
37
  {{{this.content}}}
38
38
  {{else if (eq this.source 'referenced')}}
39
- export class C{{referencedType}} {
39
+ export class C{{../typeName}}{{referencedType}} {
40
40
  data: {{referencedType}};
41
41
  constructor(data: {{referencedType}}) {
42
42
  this.data = data;
43
43
  }
44
44
  }
45
45
 
46
- export function decodeC{{referencedType}}(rawInput: unknown): C{{referencedType}} | null {
46
+ export function decodeC{{../typeName}}{{referencedType}}(rawInput: unknown): C{{../typeName}}{{referencedType}} | null {
47
47
  const result = decode{{referencedType}}(rawInput);
48
48
  if (result === null) {
49
49
  return null;
50
50
  }
51
- return new C{{referencedType}}(result);
51
+ return new C{{../typeName}}{{referencedType}}(result);
52
52
  }
53
53
 
54
54
  {{else if (eq this.dataType 'object')}}
55
55
 
56
56
  {{{this.content}}}
57
57
 
58
- export class C{{this.templateInput.typeName}} {
58
+ export class C{{../typeName}}{{this.templateInput.typeName}} {
59
59
  data: {{this.templateInput.typeName}};
60
60
  constructor(data: {{this.templateInput.typeName}}) {
61
61
  this.data = data;
@@ -67,7 +67,7 @@ export function decodeC{{this.templateInput.typeName}}(rawInput: unknown) {
67
67
  if (result === null) {
68
68
  return null;
69
69
  }
70
- return new C{{this.templateInput.typeName}}(result);
70
+ return new C{{../typeName}}{{this.templateInput.typeName}}(result);
71
71
  }
72
72
 
73
73
  {{/if}}
@@ -1,5 +1,5 @@
1
1
  export declare function resolveFilePath(filePath: string, useCurrentDirectory?: boolean): string;
2
- export declare function readFile(filePath: string, useCurrentWorkingDirectory?: boolean): Promise<string>;
2
+ export declare function readFile(filePath: string, useCurrentWorkingDirectory?: boolean, caseSensitive?: boolean): Promise<string>;
3
3
  export declare function createFolderWithBasePath(basePath: string, folderName: string): Promise<void>;
4
4
  export declare function createFolder(folderPath: string): Promise<void>;
5
5
  export declare function deleteFolder(folderPath: string): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-crafter",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "A tool to generate types from a yaml schema for any language",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",