@kinotic-ai/kinotic-cli 1.0.0

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.
Files changed (82) hide show
  1. package/README.md +594 -0
  2. package/bin/dev.cmd +3 -0
  3. package/bin/dev.js +6 -0
  4. package/bin/run.cmd +3 -0
  5. package/bin/run.js +6 -0
  6. package/dist/commands/generate.d.ts +11 -0
  7. package/dist/commands/generate.js +36 -0
  8. package/dist/commands/initialize.d.ts +12 -0
  9. package/dist/commands/initialize.js +102 -0
  10. package/dist/commands/synchronize.d.ts +17 -0
  11. package/dist/commands/synchronize.js +154 -0
  12. package/dist/index.d.ts +1 -0
  13. package/dist/index.js +1 -0
  14. package/dist/internal/CodeGenerationService.d.ts +24 -0
  15. package/dist/internal/CodeGenerationService.js +256 -0
  16. package/dist/internal/Logger.d.ts +27 -0
  17. package/dist/internal/Logger.js +35 -0
  18. package/dist/internal/ProjectMigrationService.d.ts +28 -0
  19. package/dist/internal/ProjectMigrationService.js +99 -0
  20. package/dist/internal/Utils.d.ts +66 -0
  21. package/dist/internal/Utils.js +349 -0
  22. package/dist/internal/converter/ConverterConstants.d.ts +3 -0
  23. package/dist/internal/converter/ConverterConstants.js +4 -0
  24. package/dist/internal/converter/DefaultConversionContext.d.ts +29 -0
  25. package/dist/internal/converter/DefaultConversionContext.js +112 -0
  26. package/dist/internal/converter/IConversionContext.d.ts +64 -0
  27. package/dist/internal/converter/IConversionContext.js +11 -0
  28. package/dist/internal/converter/IConverterStrategy.d.ts +35 -0
  29. package/dist/internal/converter/IConverterStrategy.js +1 -0
  30. package/dist/internal/converter/ITypeConverter.d.ts +28 -0
  31. package/dist/internal/converter/ITypeConverter.js +1 -0
  32. package/dist/internal/converter/SpecificTypesConverter.d.ts +12 -0
  33. package/dist/internal/converter/SpecificTypesConverter.js +24 -0
  34. package/dist/internal/converter/codegen/ArrayC3TypeToStatementMapper.d.ts +9 -0
  35. package/dist/internal/converter/codegen/ArrayC3TypeToStatementMapper.js +46 -0
  36. package/dist/internal/converter/codegen/ObjectC3TypeToStatementMapper.d.ts +13 -0
  37. package/dist/internal/converter/codegen/ObjectC3TypeToStatementMapper.js +67 -0
  38. package/dist/internal/converter/codegen/PrimitiveC3TypeToStatementMapper.d.ts +9 -0
  39. package/dist/internal/converter/codegen/PrimitiveC3TypeToStatementMapper.js +24 -0
  40. package/dist/internal/converter/codegen/StatementMapper.d.ts +40 -0
  41. package/dist/internal/converter/codegen/StatementMapper.js +132 -0
  42. package/dist/internal/converter/codegen/StatementMapperConversionState.d.ts +9 -0
  43. package/dist/internal/converter/codegen/StatementMapperConversionState.js +17 -0
  44. package/dist/internal/converter/codegen/StatementMapperConverterStrategy.d.ts +16 -0
  45. package/dist/internal/converter/codegen/StatementMapperConverterStrategy.js +30 -0
  46. package/dist/internal/converter/codegen/UnionC3TypeToStatementMapper.d.ts +9 -0
  47. package/dist/internal/converter/codegen/UnionC3TypeToStatementMapper.js +46 -0
  48. package/dist/internal/converter/common/BaseConversionState.d.ts +9 -0
  49. package/dist/internal/converter/common/BaseConversionState.js +13 -0
  50. package/dist/internal/converter/typescript/ArrayToC3Type.d.ts +9 -0
  51. package/dist/internal/converter/typescript/ArrayToC3Type.js +17 -0
  52. package/dist/internal/converter/typescript/ConverterUtils.d.ts +4 -0
  53. package/dist/internal/converter/typescript/ConverterUtils.js +261 -0
  54. package/dist/internal/converter/typescript/EnumToC3Type.d.ts +12 -0
  55. package/dist/internal/converter/typescript/EnumToC3Type.js +26 -0
  56. package/dist/internal/converter/typescript/ObjectLikeToC3Type.d.ts +15 -0
  57. package/dist/internal/converter/typescript/ObjectLikeToC3Type.js +111 -0
  58. package/dist/internal/converter/typescript/PrimitiveToC3Type.d.ts +10 -0
  59. package/dist/internal/converter/typescript/PrimitiveToC3Type.js +33 -0
  60. package/dist/internal/converter/typescript/QueryOptionsToC3Type.d.ts +9 -0
  61. package/dist/internal/converter/typescript/QueryOptionsToC3Type.js +9 -0
  62. package/dist/internal/converter/typescript/TenantSelectionToC3Type.d.ts +9 -0
  63. package/dist/internal/converter/typescript/TenantSelectionToC3Type.js +9 -0
  64. package/dist/internal/converter/typescript/TypescriptConversionState.d.ts +24 -0
  65. package/dist/internal/converter/typescript/TypescriptConversionState.js +26 -0
  66. package/dist/internal/converter/typescript/TypescriptConverterStrategy.d.ts +16 -0
  67. package/dist/internal/converter/typescript/TypescriptConverterStrategy.js +44 -0
  68. package/dist/internal/converter/typescript/UnionToC3Type.d.ts +15 -0
  69. package/dist/internal/converter/typescript/UnionToC3Type.js +184 -0
  70. package/dist/internal/state/Environment.d.ts +13 -0
  71. package/dist/internal/state/Environment.js +65 -0
  72. package/dist/internal/state/IStateManager.d.ts +19 -0
  73. package/dist/internal/state/IStateManager.js +41 -0
  74. package/dist/internal/state/KinoticProjectConfigUtil.d.ts +9 -0
  75. package/dist/internal/state/KinoticProjectConfigUtil.js +153 -0
  76. package/dist/templates/AdminEntityService.liquid +14 -0
  77. package/dist/templates/BaseAdminEntityService.liquid +19 -0
  78. package/dist/templates/BaseEntityService.liquid +48 -0
  79. package/dist/templates/EntityService.liquid +14 -0
  80. package/dist/templates/KinoticProjectConfig.ts.liquid +10 -0
  81. package/oclif.manifest.json +161 -0
  82. package/package.json +97 -0
@@ -0,0 +1,64 @@
1
+ import { IConverterStrategy } from './IConverterStrategy.js';
2
+ /**
3
+ * {@link IConversionContext} allows for conversion of a specific language type to Continuum IDL types.
4
+ * The {@link IConversionContext} contains state and can be reused but will retain state between requests.
5
+ * If state needs to be reset a new {@link IConversionContext} should be created.
6
+ *
7
+ * @param <BASE_TYPE> The base type that will be converted
8
+ * @param <S> The state type
9
+ *s
10
+ * Created by Navíd Mitchell 🤪 on 4/26/23.
11
+ */
12
+ export interface IConversionContext<T, R, S> {
13
+ /**
14
+ * The current json path being processed.
15
+ * This is only updated if beginProcessingProperty and endProcessingProperty are called appropriately.
16
+ * This cannot be updated manually
17
+ */
18
+ actualJsonPath: string;
19
+ /**
20
+ * The property stack. This is only updated if beginProcessingProperty and endProcessingProperty are called appropriately.
21
+ * This cannot be updated manually
22
+ */
23
+ actualPropertyStack: string[];
24
+ /**
25
+ * The current json path being processed.
26
+ * This is only updated if beginProcessingProperty and endProcessingProperty are called appropriately.
27
+ * You should not update this manually unless you know what you are doing.
28
+ */
29
+ currentJsonPath: string;
30
+ /**
31
+ * The property stack. This is only updated if beginProcessingProperty and endProcessingProperty are called appropriately.
32
+ * You should not update this manually unless you know what you are doing.
33
+ */
34
+ propertyStack: string[];
35
+ /**
36
+ * Should be called before processing a property.
37
+ * This improves debug output
38
+ * @param name of the property
39
+ */
40
+ beginProcessingProperty(name: string): void;
41
+ /**
42
+ * Should be called after processing a property.
43
+ */
44
+ endProcessingProperty(): void;
45
+ /**
46
+ * Converts the given type to a {@link R} by resolving the proper {@link ITypeConverter}.
47
+ *
48
+ * @param value to convert
49
+ * @return the converted value
50
+ */
51
+ convert(value: T): R;
52
+ /**
53
+ * @return the state of this {@link IConversionContext}
54
+ */
55
+ state(): S;
56
+ }
57
+ /**
58
+ * Creates {@link IConversionContext} instances based on a {@link IConverterStrategy}
59
+ * The {@link IConversionContext} allows for conversion of a specific language type to Continuum IDL types.
60
+ * The {@link IConversionContext} contains state and can be reused but will retain state between requests.
61
+ * If state needs to be reset a new {@link IConversionContext} should be created.
62
+ * @param strategy
63
+ */
64
+ export declare function createConversionContext<T, R, S>(strategy: IConverterStrategy<T, R, S>): IConversionContext<T, R, S>;
@@ -0,0 +1,11 @@
1
+ import { DefaultConversionContext } from './DefaultConversionContext.js';
2
+ /**
3
+ * Creates {@link IConversionContext} instances based on a {@link IConverterStrategy}
4
+ * The {@link IConversionContext} allows for conversion of a specific language type to Continuum IDL types.
5
+ * The {@link IConversionContext} contains state and can be reused but will retain state between requests.
6
+ * If state needs to be reset a new {@link IConversionContext} should be created.
7
+ * @param strategy
8
+ */
9
+ export function createConversionContext(strategy) {
10
+ return new DefaultConversionContext(strategy);
11
+ }
@@ -0,0 +1,35 @@
1
+ import { ITypeConverter } from './ITypeConverter.js';
2
+ import { Logger } from '../Logger.js';
3
+ /**
4
+ * The {@link IConverterStrategy} is used to determine how to convert a specific language type to a Continuum IDL.
5
+ * The {@link IConverterStrategy} should be reusable and thread safe.
6
+ *
7
+ * @param <BASE_TYPE> The base type to convert from
8
+ * @param <T> The type to convert to
9
+ * @param <S> The state type
10
+ *
11
+ * Created by Navíd Mitchell 🤪 on 4/26/23.
12
+ */
13
+ export interface IConverterStrategy<T, R, S> {
14
+ /**
15
+ * An array of {@link ITypeConverter}'s that will be used to convert a specific language type.
16
+ */
17
+ typeConverters(): Array<ITypeConverter<T, R, S>>;
18
+ /**
19
+ * The object that will be available via the {@link IConversionContext#state()}.
20
+ * This can be a simple {@link Map} or something with better type safety.
21
+ * This can return a new instance each time it is called.
22
+ * This will be called each time a new {@link IConversionContext} is created.
23
+ * @return the conversion context state.
24
+ */
25
+ initialState(): S | (() => S);
26
+ /**
27
+ * Returns a logger that can be used to log messages.
28
+ */
29
+ logger(): Logger | (() => Logger);
30
+ /**
31
+ * Prints a value to a string.
32
+ * This is used in error messages to help the user understand what value caused the error.
33
+ */
34
+ valueToString(value: T): string;
35
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ import { IConversionContext } from './IConversionContext.js';
2
+ /**
3
+ * {@link ITypeConverter} are the base interface for converting a specific type to another specific type.
4
+ * All {@link ITypeConverter}'s should be stateless. Any state that needs to be retained should be stored in the {@link IConversionContext}.
5
+ *
6
+ * @param <BASE_TYPE> the base type to convert from
7
+ * @param <T> the type to convert to
8
+ * @param <S> the state type
9
+ *
10
+ * Created by Navíd Mitchell 🤪 on 4/26/23.
11
+ */
12
+ export interface ITypeConverter<T, R, S> {
13
+ /**
14
+ * Converts the given value to the specified return type.
15
+ * @param value to convert
16
+ * @param conversionContext the context to use for conversion
17
+ * @return the converted type
18
+ */
19
+ convert(value: T, conversionContext: IConversionContext<T, R, S>): R;
20
+ /**
21
+ * Checks if the given value is supported by this converter
22
+ *
23
+ * @param value to check if supported
24
+ * @param conversionState the state of the conversion, that is contained in the {@link IConversionContext}
25
+ * @return true if this converter can convert the type false if not
26
+ */
27
+ supports(value: T, conversionState: S): boolean;
28
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ import { ITypeConverter } from './ITypeConverter.js';
2
+ import { IConversionContext } from './IConversionContext.js';
3
+ /**
4
+ * {@link ITypeConverter} are the base interface for converting a specific type to another specific type.
5
+ */
6
+ export declare abstract class SpecificTypesConverter<T, R, S, MATCH_VALUE> implements ITypeConverter<T, R, S> {
7
+ private readonly matchValueExtractor;
8
+ private typeConverterFunctions;
9
+ protected constructor(matchValueExtractor: (arg: T) => MATCH_VALUE, typeConverterFunctions: Map<MATCH_VALUE, ((type: T, context: IConversionContext<T, R, S>) => R)>);
10
+ convert(value: T, conversionContext: IConversionContext<T, R, S>): R;
11
+ supports(value: T): boolean;
12
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * {@link ITypeConverter} are the base interface for converting a specific type to another specific type.
3
+ */
4
+ export class SpecificTypesConverter {
5
+ matchValueExtractor;
6
+ typeConverterFunctions;
7
+ constructor(matchValueExtractor, typeConverterFunctions) {
8
+ this.matchValueExtractor = matchValueExtractor;
9
+ this.typeConverterFunctions = typeConverterFunctions;
10
+ }
11
+ convert(value, conversionContext) {
12
+ const matchValue = this.matchValueExtractor(value);
13
+ const converterFunction = this.typeConverterFunctions.get(matchValue);
14
+ if (!converterFunction) {
15
+ // Should never happen due to supports call!
16
+ throw new Error("Converter function could not be resolved, WAT!");
17
+ }
18
+ return converterFunction(value, conversionContext);
19
+ }
20
+ supports(value) {
21
+ const matchValue = this.matchValueExtractor(value);
22
+ return this.typeConverterFunctions.has(matchValue);
23
+ }
24
+ }
@@ -0,0 +1,9 @@
1
+ import { C3Type } from '@kinotic-ai/idl';
2
+ import { ITypeConverter } from '../ITypeConverter.js';
3
+ import { IConversionContext } from '../IConversionContext.js';
4
+ import { StatementMapper } from './StatementMapper.js';
5
+ import { StatementMapperConversionState } from './StatementMapperConversionState.js';
6
+ export declare class ArrayC3TypeToStatementMapper implements ITypeConverter<C3Type, StatementMapper, StatementMapperConversionState> {
7
+ convert(value: C3Type, conversionContext: IConversionContext<C3Type, StatementMapper, StatementMapperConversionState>): StatementMapper;
8
+ supports(value: C3Type, conversionState: StatementMapperConversionState): boolean;
9
+ }
@@ -0,0 +1,46 @@
1
+ import { ArrayC3Type } from '@kinotic-ai/idl';
2
+ import { MultiStatementMapper, } from './StatementMapper.js';
3
+ import { camel } from 'radash';
4
+ export class ArrayC3TypeToStatementMapper {
5
+ convert(value, conversionContext) {
6
+ const arrayC3Type = value;
7
+ const ret = new MultiStatementMapper(conversionContext.state());
8
+ const currentJsonPath = conversionContext.currentJsonPath;
9
+ const currentPropertyStack = conversionContext.propertyStack;
10
+ const targetName = conversionContext.state().targetName;
11
+ const sourceName = conversionContext.state().sourceName;
12
+ const targetJsonPath = targetName + (currentJsonPath.length > 0 ? '.' + currentJsonPath : '');
13
+ const sourceJsonPath = sourceName + (currentJsonPath.length > 0 ? '.' + currentJsonPath : '');
14
+ const itemName = camel(sourceJsonPath.replaceAll('.', ' ') + ' Item');
15
+ const internalTargetName = camel(targetJsonPath.replaceAll('.', ' ') + ' Value');
16
+ ret.addLiteral(`if (${sourceJsonPath}) {`);
17
+ conversionContext.state().indentMore();
18
+ ret.addLiteral(`${targetJsonPath} = []`);
19
+ ret.addLiteral(`for (let ${itemName} of ${sourceJsonPath}) {`);
20
+ conversionContext.state().indentMore();
21
+ ret.addLiteral(`let ${internalTargetName}: any`);
22
+ if (arrayC3Type.contains) {
23
+ conversionContext.state().targetName = internalTargetName;
24
+ conversionContext.state().sourceName = itemName;
25
+ conversionContext.currentJsonPath = '';
26
+ conversionContext.propertyStack = [];
27
+ ret.add(conversionContext.convert(arrayC3Type.contains));
28
+ conversionContext.currentJsonPath = currentJsonPath;
29
+ conversionContext.propertyStack = currentPropertyStack;
30
+ conversionContext.state().targetName = targetName;
31
+ conversionContext.state().sourceName = sourceName;
32
+ }
33
+ else {
34
+ throw new Error('ArrayC3Type.contains is undefined');
35
+ }
36
+ ret.addLiteral(`${targetJsonPath}.push(${internalTargetName})`);
37
+ conversionContext.state().indentLess();
38
+ ret.addLiteral('}');
39
+ conversionContext.state().indentLess();
40
+ ret.addLiteral('}');
41
+ return ret;
42
+ }
43
+ supports(value, conversionState) {
44
+ return value instanceof ArrayC3Type || value.type === 'array';
45
+ }
46
+ }
@@ -0,0 +1,13 @@
1
+ import { C3Type } from '@kinotic-ai/idl';
2
+ import { ITypeConverter } from '../ITypeConverter.js';
3
+ import { IConversionContext } from '../IConversionContext.js';
4
+ import { StatementMapper } from './StatementMapper.js';
5
+ import { StatementMapperConversionState } from './StatementMapperConversionState.js';
6
+ export declare class ObjectC3TypeToStatementMapper implements ITypeConverter<C3Type, StatementMapper, StatementMapperConversionState> {
7
+ convert(value: C3Type, conversionContext: IConversionContext<C3Type, StatementMapper, StatementMapperConversionState>): StatementMapper;
8
+ private createStatementForFunction;
9
+ private removeSourceFilePath;
10
+ private removeSourceFilePathFromObject;
11
+ private removeSourceFilePathFromUnion;
12
+ supports(value: C3Type, conversionState: StatementMapperConversionState): boolean;
13
+ }
@@ -0,0 +1,67 @@
1
+ import { ArrayC3Type, ObjectC3Type, UnionC3Type } from '@kinotic-ai/idl';
2
+ import { MultiStatementMapper, LiteralStatementMapper } from './StatementMapper.js';
3
+ export class ObjectC3TypeToStatementMapper {
4
+ convert(value, conversionContext) {
5
+ const state = conversionContext.state();
6
+ const objectC3Type = value;
7
+ const ret = new MultiStatementMapper(state);
8
+ const targetName = state.targetName;
9
+ const sourceName = state.sourceName;
10
+ const jsonPath = (conversionContext.currentJsonPath.length > 0 ? '.' + conversionContext.currentJsonPath : '');
11
+ const lhs = targetName + jsonPath;
12
+ const condition = sourceName + jsonPath;
13
+ ret.addLiteral('if (' + condition + ') {');
14
+ state.indentMore();
15
+ ret.add(new LiteralStatementMapper(`${lhs} = (${lhs} ? ${lhs} : {})`));
16
+ for (const property of objectC3Type.properties) {
17
+ conversionContext.beginProcessingProperty(property.name);
18
+ ret.add(conversionContext.convert(property.type));
19
+ conversionContext.endProcessingProperty();
20
+ }
21
+ state.indentLess();
22
+ ret.addLiteral('}');
23
+ if (value?.metadata?.sourceFilePath) {
24
+ delete value.metadata.sourceFilePath;
25
+ }
26
+ return ret;
27
+ }
28
+ createStatementForFunction(functionDeclaration, conversionContext) {
29
+ const targetName = conversionContext.state().targetName;
30
+ const lhs = targetName + (conversionContext.currentJsonPath.length > 0 ? '.' + conversionContext.currentJsonPath : '');
31
+ const functionName = functionDeclaration.getNameOrThrow('Could not find function name');
32
+ let ret = new LiteralStatementMapper(`${lhs} = ${functionName}(entity)`);
33
+ ret.neededImports.push({ importName: functionName,
34
+ sourcePath: functionDeclaration.getSourceFile().getFilePath(),
35
+ defaultExport: functionDeclaration.isDefaultExport() });
36
+ return ret;
37
+ }
38
+ removeSourceFilePath(value) {
39
+ if (value instanceof ObjectC3Type) {
40
+ this.removeSourceFilePathFromObject(value);
41
+ }
42
+ else if (value instanceof UnionC3Type) {
43
+ this.removeSourceFilePathFromUnion(value);
44
+ }
45
+ else if (value instanceof ArrayC3Type) {
46
+ this.removeSourceFilePath(value.contains);
47
+ }
48
+ }
49
+ removeSourceFilePathFromObject(value) {
50
+ const objectC3Type = value;
51
+ if (objectC3Type?.metadata?.sourceFilePath) {
52
+ delete objectC3Type.metadata.sourceFilePath;
53
+ }
54
+ for (const property of objectC3Type.properties) {
55
+ this.removeSourceFilePath(property.type);
56
+ }
57
+ }
58
+ removeSourceFilePathFromUnion(value) {
59
+ const unionC3Type = value;
60
+ for (const type of unionC3Type.types) {
61
+ this.removeSourceFilePath(type);
62
+ }
63
+ }
64
+ supports(value, conversionState) {
65
+ return value instanceof ObjectC3Type || value.type === 'object';
66
+ }
67
+ }
@@ -0,0 +1,9 @@
1
+ import { C3Type } from '@kinotic-ai/idl';
2
+ import { IConversionContext } from '../IConversionContext.js';
3
+ import { ITypeConverter } from '../ITypeConverter.js';
4
+ import { StatementMapper } from './StatementMapper.js';
5
+ import { StatementMapperConversionState } from './StatementMapperConversionState.js';
6
+ export declare class PrimitiveC3TypeToStatementMapper implements ITypeConverter<C3Type, StatementMapper, StatementMapperConversionState> {
7
+ convert(value: C3Type, conversionContext: IConversionContext<C3Type, StatementMapper, StatementMapperConversionState>): StatementMapper;
8
+ supports(value: C3Type, conversionState: StatementMapperConversionState): boolean;
9
+ }
@@ -0,0 +1,24 @@
1
+ import { BooleanC3Type, ByteC3Type, CharC3Type, DateC3Type, DoubleC3Type, EnumC3Type, FloatC3Type, IntC3Type, LongC3Type, ShortC3Type, StringC3Type } from '@kinotic-ai/idl';
2
+ import { LiteralStatementMapper } from './StatementMapper.js';
3
+ export class PrimitiveC3TypeToStatementMapper {
4
+ convert(value, conversionContext) {
5
+ const targetName = conversionContext.state().targetName;
6
+ const sourceName = conversionContext.state().sourceName;
7
+ const lhs = targetName + (conversionContext.currentJsonPath.length > 0 ? '.' + conversionContext.currentJsonPath : '');
8
+ const rhs = sourceName + (conversionContext.currentJsonPath.length > 0 ? '.' + conversionContext.currentJsonPath : '');
9
+ return new LiteralStatementMapper(`${lhs} = ${rhs}`);
10
+ }
11
+ supports(value, conversionState) {
12
+ return (value instanceof BooleanC3Type || value.type === 'boolean')
13
+ || (value instanceof ByteC3Type || value.type === 'byte')
14
+ || (value instanceof CharC3Type || value.type === 'char')
15
+ || (value instanceof DateC3Type || value.type === 'date')
16
+ || (value instanceof DoubleC3Type || value.type === 'double')
17
+ || (value instanceof EnumC3Type || value.type === 'enum')
18
+ || (value instanceof FloatC3Type || value.type === 'float')
19
+ || (value instanceof IntC3Type || value.type === 'int')
20
+ || (value instanceof LongC3Type || value.type === 'long')
21
+ || (value instanceof ShortC3Type || value.type === 'short')
22
+ || (value instanceof StringC3Type || value.type === 'string');
23
+ }
24
+ }
@@ -0,0 +1,40 @@
1
+ import { StatementMapperConversionState } from './StatementMapperConversionState.js';
2
+ export interface NeededImport {
3
+ importName: string;
4
+ defaultExport?: boolean;
5
+ sourcePath: string;
6
+ }
7
+ export interface StatementMapper {
8
+ getNeededImports(): NeededImport[];
9
+ toStatementString(): string | null;
10
+ }
11
+ export declare class MultiStatementMapper implements StatementMapper {
12
+ private readonly _state;
13
+ private readonly _mappers;
14
+ constructor(state: StatementMapperConversionState, mappers?: Array<StatementMapper>);
15
+ add(mapper: StatementMapper): MultiStatementMapper;
16
+ addLiteral(stringLiteral: string): MultiStatementMapper;
17
+ getNeededImports(): NeededImport[];
18
+ toStatementString(): string | null;
19
+ }
20
+ export declare class IndentingStatementMapper implements StatementMapper {
21
+ private readonly _indent;
22
+ private readonly _mapper;
23
+ constructor(indent: number, mapper: StatementMapper);
24
+ getNeededImports(): NeededImport[];
25
+ toStatementString(): string | null;
26
+ }
27
+ export declare class LiteralStatementMapper implements StatementMapper {
28
+ statementString: string | null;
29
+ neededImports: NeededImport[];
30
+ constructor(statementString?: string | null, neededImports?: NeededImport[]);
31
+ getNeededImports(): NeededImport[];
32
+ toStatementString(): string | null;
33
+ }
34
+ /**
35
+ * Creates a string that can be used to import all the needed imports for a file.
36
+ * @param statementMapper The statement mapper that contains the needed imports.
37
+ * @param targetFilePath The path to the file that will be generated.
38
+ * @param fileExtensionForImports The file extension to use for imports.
39
+ */
40
+ export declare function createImportString(statementMapper: StatementMapper | null, targetFilePath: string, fileExtensionForImports: string): string | null;
@@ -0,0 +1,132 @@
1
+ import { getRelativeImportPath, tryGetNodeModuleName } from '../../Utils.js';
2
+ export class MultiStatementMapper {
3
+ _state;
4
+ _mappers;
5
+ constructor(state, mappers) {
6
+ this._state = state;
7
+ this._mappers = mappers || [];
8
+ }
9
+ add(mapper) {
10
+ if (mapper instanceof MultiStatementMapper) {
11
+ this._mappers.push(mapper);
12
+ }
13
+ else {
14
+ this._mappers.push(new IndentingStatementMapper(this._state.indent, mapper));
15
+ }
16
+ return this;
17
+ }
18
+ addLiteral(stringLiteral) {
19
+ this.add(new LiteralStatementMapper(stringLiteral));
20
+ return this;
21
+ }
22
+ getNeededImports() {
23
+ return this._mappers.map(mapper => mapper.getNeededImports()).flat();
24
+ }
25
+ toStatementString() {
26
+ let ret = null;
27
+ for (const mapper of this._mappers) {
28
+ const statementString = mapper.toStatementString();
29
+ if (statementString != null) {
30
+ if (ret == null) {
31
+ ret = '';
32
+ }
33
+ ret += statementString + (statementString.endsWith('\n') ? '' : '\n');
34
+ }
35
+ }
36
+ return ret;
37
+ }
38
+ }
39
+ export class IndentingStatementMapper {
40
+ _indent;
41
+ _mapper;
42
+ constructor(indent, mapper) {
43
+ this._indent = indent;
44
+ this._mapper = mapper;
45
+ }
46
+ getNeededImports() {
47
+ return this._mapper.getNeededImports();
48
+ }
49
+ toStatementString() {
50
+ const out = this._mapper.toStatementString();
51
+ return ' '.repeat(this._indent) + out;
52
+ }
53
+ }
54
+ export class LiteralStatementMapper {
55
+ statementString = null;
56
+ neededImports = [];
57
+ constructor(statementString, neededImports) {
58
+ if (statementString) {
59
+ this.statementString = statementString;
60
+ }
61
+ if (neededImports) {
62
+ this.neededImports = neededImports;
63
+ }
64
+ }
65
+ getNeededImports() {
66
+ return this.neededImports;
67
+ }
68
+ toStatementString() {
69
+ return this.statementString;
70
+ }
71
+ }
72
+ /**
73
+ * Creates a string that can be used to import all the needed imports for a file.
74
+ * @param statementMapper The statement mapper that contains the needed imports.
75
+ * @param targetFilePath The path to the file that will be generated.
76
+ * @param fileExtensionForImports The file extension to use for imports.
77
+ */
78
+ export function createImportString(statementMapper, targetFilePath, fileExtensionForImports) {
79
+ const importsWithoutDuplicates = statementMapper != null ? removeDuplicateImports(statementMapper.getNeededImports()) : [];
80
+ // Now make sure all imports for the same paths are grouped together in the import string
81
+ const importMap = {};
82
+ for (const imp of importsWithoutDuplicates) {
83
+ if (importMap[imp.sourcePath]) {
84
+ importMap[imp.sourcePath].push(imp);
85
+ }
86
+ else {
87
+ importMap[imp.sourcePath] = [imp];
88
+ }
89
+ }
90
+ let importString = '';
91
+ for (let importPath of Object.keys(importMap)) {
92
+ const imports = importMap[importPath];
93
+ // modify the source path to be correct for the target file
94
+ let newImportPath = tryGetNodeModuleName(importPath);
95
+ if (!newImportPath) {
96
+ newImportPath = getRelativeImportPath(targetFilePath, importPath, fileExtensionForImports);
97
+ }
98
+ importPath = newImportPath;
99
+ const defaultImports = imports.filter(imp => imp.defaultExport);
100
+ const namedImports = imports.filter(imp => !imp.defaultExport);
101
+ if (defaultImports.length > 0) {
102
+ if (defaultImports.length > 1) {
103
+ throw new Error(`Multiple default exports for import path ${importPath}`);
104
+ }
105
+ importString += `import ${defaultImports[0].importName} from '${importPath}'\n`;
106
+ }
107
+ if (namedImports.length > 0) {
108
+ importString += `import {${namedImports.map(imp => imp.importName).join(',\n')}} from '${importPath}'\n`;
109
+ }
110
+ }
111
+ return importString.length > 0 ? importString : null;
112
+ }
113
+ function removeDuplicateImports(imports) {
114
+ const ret = [];
115
+ const importMap = {};
116
+ for (const imp of imports) {
117
+ if (importMap[imp.importName]) {
118
+ const existing = importMap[imp.importName];
119
+ if (existing.sourcePath !== imp.sourcePath) {
120
+ throw new Error(`Import name ${imp.importName} is used for multiple imports with different paths: ${existing.sourcePath} and ${imp.sourcePath}`);
121
+ }
122
+ if (imp.defaultExport) {
123
+ importMap[imp.importName].defaultExport = true;
124
+ }
125
+ }
126
+ else {
127
+ importMap[imp.importName] = imp;
128
+ ret.push(imp);
129
+ }
130
+ }
131
+ return ret;
132
+ }
@@ -0,0 +1,9 @@
1
+ import { BaseConversionState } from '../common/BaseConversionState.js';
2
+ export declare class StatementMapperConversionState extends BaseConversionState {
3
+ sourceName: string;
4
+ targetName: string;
5
+ indent: number;
6
+ constructor(application: string);
7
+ indentMore(): StatementMapperConversionState;
8
+ indentLess(): StatementMapperConversionState;
9
+ }
@@ -0,0 +1,17 @@
1
+ import { BaseConversionState } from '../common/BaseConversionState.js';
2
+ export class StatementMapperConversionState extends BaseConversionState {
3
+ sourceName = 'entity';
4
+ targetName = 'ret';
5
+ indent = 4;
6
+ constructor(application) {
7
+ super(application);
8
+ }
9
+ indentMore() {
10
+ this.indent += 2;
11
+ return this;
12
+ }
13
+ indentLess() {
14
+ this.indent -= 2;
15
+ return this;
16
+ }
17
+ }
@@ -0,0 +1,16 @@
1
+ import { C3Type } from '@kinotic-ai/idl';
2
+ import { Logger } from '../../Logger.js';
3
+ import { IConverterStrategy } from '../IConverterStrategy.js';
4
+ import { ITypeConverter } from '../ITypeConverter.js';
5
+ import { StatementMapper } from './StatementMapper.js';
6
+ import { StatementMapperConversionState } from './StatementMapperConversionState.js';
7
+ export declare class StatementMapperConverterStrategy implements IConverterStrategy<C3Type, StatementMapper, StatementMapperConversionState> {
8
+ private readonly _initialState;
9
+ private readonly _logger;
10
+ private readonly _typeConverters;
11
+ constructor(initialState: (() => StatementMapperConversionState) | StatementMapperConversionState, logger: Logger);
12
+ initialState(): (() => StatementMapperConversionState) | StatementMapperConversionState;
13
+ logger(): Logger | (() => Logger);
14
+ typeConverters(): Array<ITypeConverter<C3Type, StatementMapper, StatementMapperConversionState>>;
15
+ valueToString(value: C3Type): string;
16
+ }
@@ -0,0 +1,30 @@
1
+ import { ArrayC3TypeToStatementMapper } from './ArrayC3TypeToStatementMapper.js';
2
+ import { ObjectC3TypeToStatementMapper } from './ObjectC3TypeToStatementMapper.js';
3
+ import { PrimitiveC3TypeToStatementMapper } from './PrimitiveC3TypeToStatementMapper.js';
4
+ import { UnionC3TypeToStatementMapper } from './UnionC3TypeToStatementMapper.js';
5
+ export class StatementMapperConverterStrategy {
6
+ _initialState;
7
+ _logger;
8
+ _typeConverters = [
9
+ new PrimitiveC3TypeToStatementMapper(),
10
+ new ArrayC3TypeToStatementMapper(),
11
+ new ObjectC3TypeToStatementMapper(),
12
+ new UnionC3TypeToStatementMapper()
13
+ ];
14
+ constructor(initialState, logger) {
15
+ this._initialState = initialState;
16
+ this._logger = logger;
17
+ }
18
+ initialState() {
19
+ return this._initialState;
20
+ }
21
+ logger() {
22
+ return this._logger;
23
+ }
24
+ typeConverters() {
25
+ return this._typeConverters;
26
+ }
27
+ valueToString(value) {
28
+ return value.type;
29
+ }
30
+ }
@@ -0,0 +1,9 @@
1
+ import { C3Type } from '@kinotic-ai/idl';
2
+ import { ITypeConverter } from '../ITypeConverter.js';
3
+ import { IConversionContext } from '../IConversionContext.js';
4
+ import { StatementMapper } from './StatementMapper.js';
5
+ import { StatementMapperConversionState } from './StatementMapperConversionState.js';
6
+ export declare class UnionC3TypeToStatementMapper implements ITypeConverter<C3Type, StatementMapper, StatementMapperConversionState> {
7
+ convert(value: C3Type, conversionContext: IConversionContext<C3Type, StatementMapper, StatementMapperConversionState>): StatementMapper;
8
+ supports(value: C3Type, conversionState: StatementMapperConversionState): boolean;
9
+ }