@useparagon/cli 0.0.1-canary.1

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 (207) hide show
  1. package/dist/codegen/codegen.types.d.ts +9 -0
  2. package/dist/codegen/codegen.types.js +3 -0
  3. package/dist/codegen/codegen.types.js.map +1 -0
  4. package/dist/codegen/codegen.utils.d.ts +4 -0
  5. package/dist/codegen/codegen.utils.js +70 -0
  6. package/dist/codegen/codegen.utils.js.map +1 -0
  7. package/dist/codegen/codegens/condition.codegen.d.ts +2 -0
  8. package/dist/codegen/codegens/condition.codegen.js +28 -0
  9. package/dist/codegen/codegens/condition.codegen.js.map +1 -0
  10. package/dist/codegen/codegens/event.codegen.d.ts +2 -0
  11. package/dist/codegen/codegens/event.codegen.js +35 -0
  12. package/dist/codegen/codegens/event.codegen.js.map +1 -0
  13. package/dist/codegen/codegens/integration.codegen.d.ts +2 -0
  14. package/dist/codegen/codegens/integration.codegen.js +18 -0
  15. package/dist/codegen/codegens/integration.codegen.js.map +1 -0
  16. package/dist/codegen/codegens/source.codegen.d.ts +5 -0
  17. package/dist/codegen/codegens/source.codegen.js +104 -0
  18. package/dist/codegen/codegens/source.codegen.js.map +1 -0
  19. package/dist/codegen/codegens/workflow.codegen.d.ts +3 -0
  20. package/dist/codegen/codegens/workflow.codegen.js +278 -0
  21. package/dist/codegen/codegens/workflow.codegen.js.map +1 -0
  22. package/dist/commands/build/build.action.d.ts +8 -0
  23. package/dist/commands/build/build.action.js +49 -0
  24. package/dist/commands/build/build.action.js.map +1 -0
  25. package/dist/commands/build/build.command.d.ts +8 -0
  26. package/dist/commands/build/build.command.js +27 -0
  27. package/dist/commands/build/build.command.js.map +1 -0
  28. package/dist/commands/build/build.types.d.ts +6 -0
  29. package/dist/commands/build/build.types.js +3 -0
  30. package/dist/commands/build/build.types.js.map +1 -0
  31. package/dist/commands/build/build.utils.d.ts +1 -0
  32. package/dist/commands/build/build.utils.js +17 -0
  33. package/dist/commands/build/build.utils.js.map +1 -0
  34. package/dist/commands/build/index.d.ts +1 -0
  35. package/dist/commands/build/index.js +5 -0
  36. package/dist/commands/build/index.js.map +1 -0
  37. package/dist/commands/command.interface.d.ts +7 -0
  38. package/dist/commands/command.interface.js +3 -0
  39. package/dist/commands/command.interface.js.map +1 -0
  40. package/dist/commands/command.loader.d.ts +5 -0
  41. package/dist/commands/command.loader.js +27 -0
  42. package/dist/commands/command.loader.js.map +1 -0
  43. package/dist/commands/index.d.ts +2 -0
  44. package/dist/commands/index.js +6 -0
  45. package/dist/commands/index.js.map +1 -0
  46. package/dist/commands/init/index.d.ts +1 -0
  47. package/dist/commands/init/index.js +5 -0
  48. package/dist/commands/init/index.js.map +1 -0
  49. package/dist/commands/init/init.action.d.ts +11 -0
  50. package/dist/commands/init/init.action.js +92 -0
  51. package/dist/commands/init/init.action.js.map +1 -0
  52. package/dist/commands/init/init.command.d.ts +8 -0
  53. package/dist/commands/init/init.command.js +27 -0
  54. package/dist/commands/init/init.command.js.map +1 -0
  55. package/dist/commands/init/init.constants.d.ts +1 -0
  56. package/dist/commands/init/init.constants.js +21 -0
  57. package/dist/commands/init/init.constants.js.map +1 -0
  58. package/dist/commands/init/init.types.d.ts +7 -0
  59. package/dist/commands/init/init.types.js +3 -0
  60. package/dist/commands/init/init.types.js.map +1 -0
  61. package/dist/commands/init/init.utils.d.ts +11 -0
  62. package/dist/commands/init/init.utils.js +50 -0
  63. package/dist/commands/init/init.utils.js.map +1 -0
  64. package/dist/commands/login/index.d.ts +1 -0
  65. package/dist/commands/login/index.js +5 -0
  66. package/dist/commands/login/index.js.map +1 -0
  67. package/dist/commands/login/login.command.d.ts +5 -0
  68. package/dist/commands/login/login.command.js +26 -0
  69. package/dist/commands/login/login.command.js.map +1 -0
  70. package/dist/commands/login/login.types.d.ts +3 -0
  71. package/dist/commands/login/login.types.js +3 -0
  72. package/dist/commands/login/login.types.js.map +1 -0
  73. package/dist/commands/login/login.utils.d.ts +2 -0
  74. package/dist/commands/login/login.utils.js +47 -0
  75. package/dist/commands/login/login.utils.js.map +1 -0
  76. package/dist/compilers/compiler.interface.d.ts +3 -0
  77. package/dist/compilers/compiler.interface.js +3 -0
  78. package/dist/compilers/compiler.interface.js.map +1 -0
  79. package/dist/compilers/compiler.types.d.ts +23 -0
  80. package/dist/compilers/compiler.types.js +3 -0
  81. package/dist/compilers/compiler.types.js.map +1 -0
  82. package/dist/compilers/compiler.utils.d.ts +2 -0
  83. package/dist/compilers/compiler.utils.js +25 -0
  84. package/dist/compilers/compiler.utils.js.map +1 -0
  85. package/dist/compilers/event.compiler.d.ts +7 -0
  86. package/dist/compilers/event.compiler.js +25 -0
  87. package/dist/compilers/event.compiler.js.map +1 -0
  88. package/dist/compilers/integration.compiler.d.ts +9 -0
  89. package/dist/compilers/integration.compiler.js +55 -0
  90. package/dist/compilers/integration.compiler.js.map +1 -0
  91. package/dist/compilers/project.compiler.d.ts +10 -0
  92. package/dist/compilers/project.compiler.js +50 -0
  93. package/dist/compilers/project.compiler.js.map +1 -0
  94. package/dist/compilers/ts.compiler.d.ts +11 -0
  95. package/dist/compilers/ts.compiler.js +70 -0
  96. package/dist/compilers/ts.compiler.js.map +1 -0
  97. package/dist/compilers/workflow.compiler.d.ts +10 -0
  98. package/dist/compilers/workflow.compiler.js +36 -0
  99. package/dist/compilers/workflow.compiler.js.map +1 -0
  100. package/dist/config/config.constants.d.ts +4 -0
  101. package/dist/config/config.constants.js +12 -0
  102. package/dist/config/config.constants.js.map +1 -0
  103. package/dist/config/config.types.d.ts +31 -0
  104. package/dist/config/config.types.js +18 -0
  105. package/dist/config/config.types.js.map +1 -0
  106. package/dist/config/config.utils.d.ts +9 -0
  107. package/dist/config/config.utils.js +136 -0
  108. package/dist/config/config.utils.js.map +1 -0
  109. package/dist/config/index.d.ts +2 -0
  110. package/dist/config/index.js +6 -0
  111. package/dist/config/index.js.map +1 -0
  112. package/dist/helpers/constants.d.ts +2 -0
  113. package/dist/helpers/constants.js +6 -0
  114. package/dist/helpers/constants.js.map +1 -0
  115. package/dist/helpers/json.d.ts +1 -0
  116. package/dist/helpers/json.js +8 -0
  117. package/dist/helpers/json.js.map +1 -0
  118. package/dist/helpers/spinners.d.ts +3 -0
  119. package/dist/helpers/spinners.js +6 -0
  120. package/dist/helpers/spinners.js.map +1 -0
  121. package/dist/helpers/strings.d.ts +1 -0
  122. package/dist/helpers/strings.js +8 -0
  123. package/dist/helpers/strings.js.map +1 -0
  124. package/dist/index.d.ts +4 -0
  125. package/dist/index.js +16 -0
  126. package/dist/index.js.map +1 -0
  127. package/dist/logger/index.d.ts +1 -0
  128. package/dist/logger/index.js +9 -0
  129. package/dist/logger/index.js.map +1 -0
  130. package/dist/logger/logger.d.ts +3 -0
  131. package/dist/logger/logger.js +50 -0
  132. package/dist/logger/logger.js.map +1 -0
  133. package/dist/logger/logger.type.d.ts +8 -0
  134. package/dist/logger/logger.type.js +3 -0
  135. package/dist/logger/logger.type.js.map +1 -0
  136. package/dist/templates/configurations/javascript/.prettierignore +2 -0
  137. package/dist/templates/configurations/javascript/.prettierrc +11 -0
  138. package/dist/templates/configurations/typescript/.prettierignore +2 -0
  139. package/dist/templates/configurations/typescript/.prettierrc +19 -0
  140. package/dist/templates/javascript/src/events/hello-world.js +13 -0
  141. package/dist/templates/javascript/src/integrations/slack/config.js +11 -0
  142. package/dist/templates/javascript/src/integrations/slack/workflows/example-workflow.js +30 -0
  143. package/dist/templates/typescript/src/events/hello-world.ts +21 -0
  144. package/dist/templates/typescript/src/integrations/slack/config.ts +18 -0
  145. package/dist/templates/typescript/src/integrations/slack/workflows/example-workflow.ts +30 -0
  146. package/dist/templates/typescript/tsconfig.json +31 -0
  147. package/package.json +56 -0
  148. package/paragon.json +3 -0
  149. package/sample/sample.js +404 -0
  150. package/src/codegen/codegen.types.ts +14 -0
  151. package/src/codegen/codegen.utils.ts +145 -0
  152. package/src/codegen/codegens/condition.codegen.ts +49 -0
  153. package/src/codegen/codegens/event.codegen.ts +39 -0
  154. package/src/codegen/codegens/integration.codegen.ts +23 -0
  155. package/src/codegen/codegens/source.codegen.ts +193 -0
  156. package/src/codegen/codegens/workflow.codegen.ts +495 -0
  157. package/src/commands/build/build.action.ts +73 -0
  158. package/src/commands/build/build.command.ts +61 -0
  159. package/src/commands/build/build.types.ts +7 -0
  160. package/src/commands/build/build.utils.ts +21 -0
  161. package/src/commands/build/index.ts +1 -0
  162. package/src/commands/command.interface.ts +22 -0
  163. package/src/commands/command.loader.ts +33 -0
  164. package/src/commands/index.ts +2 -0
  165. package/src/commands/init/index.ts +1 -0
  166. package/src/commands/init/init.action.ts +166 -0
  167. package/src/commands/init/init.command.ts +71 -0
  168. package/src/commands/init/init.constants.ts +29 -0
  169. package/src/commands/init/init.types.ts +8 -0
  170. package/src/commands/init/init.utils.ts +91 -0
  171. package/src/commands/login/index.ts +1 -0
  172. package/src/commands/login/login.command.ts +48 -0
  173. package/src/commands/login/login.types.ts +9 -0
  174. package/src/commands/login/login.utils.ts +68 -0
  175. package/src/compilers/compiler.interface.ts +9 -0
  176. package/src/compilers/compiler.types.ts +52 -0
  177. package/src/compilers/compiler.utils.ts +32 -0
  178. package/src/compilers/event.compiler.ts +33 -0
  179. package/src/compilers/integration.compiler.ts +84 -0
  180. package/src/compilers/project.compiler.ts +86 -0
  181. package/src/compilers/ts.compiler.ts +116 -0
  182. package/src/compilers/workflow.compiler.ts +69 -0
  183. package/src/config/config.constants.ts +27 -0
  184. package/src/config/config.types.ts +112 -0
  185. package/src/config/config.utils.ts +251 -0
  186. package/src/config/index.ts +2 -0
  187. package/src/helpers/constants.ts +9 -0
  188. package/src/helpers/json.ts +8 -0
  189. package/src/helpers/spinners.ts +3 -0
  190. package/src/helpers/strings.ts +9 -0
  191. package/src/index.ts +26 -0
  192. package/src/logger/index.ts +1 -0
  193. package/src/logger/logger.ts +97 -0
  194. package/src/logger/logger.type.ts +8 -0
  195. package/templates/configurations/javascript/.prettierignore +2 -0
  196. package/templates/configurations/javascript/.prettierrc +11 -0
  197. package/templates/configurations/typescript/.prettierignore +2 -0
  198. package/templates/configurations/typescript/.prettierrc +19 -0
  199. package/templates/javascript/src/events/hello-world.js +13 -0
  200. package/templates/javascript/src/integrations/slack/config.js +11 -0
  201. package/templates/javascript/src/integrations/slack/workflows/example-workflow.js +30 -0
  202. package/templates/typescript/src/events/hello-world.ts +21 -0
  203. package/templates/typescript/src/integrations/slack/config.ts +18 -0
  204. package/templates/typescript/src/integrations/slack/workflows/example-workflow.ts +30 -0
  205. package/templates/typescript/tsconfig.json +31 -0
  206. package/tsconfig.json +13 -0
  207. package/tsconfig.release.json +13 -0
@@ -0,0 +1,193 @@
1
+ import {
2
+ ConditionWrapper,
3
+ DataType,
4
+ KeyedSource,
5
+ OperatorCondition,
6
+ Source,
7
+ TokenizedSource,
8
+ } from '@useparagon/core/resolvers';
9
+
10
+ import { conditionToCode } from './condition.codegen';
11
+
12
+ export const EMPTY_TOKENIZED_SOURCE: TokenizedSource<DataType.STRING> = {
13
+ type: 'TOKENIZED',
14
+ parts: [
15
+ {
16
+ dataType: DataType.STRING,
17
+ type: 'VALUE',
18
+ value: '',
19
+ },
20
+ ],
21
+ };
22
+
23
+ /**
24
+ * convert source to ts code
25
+ * @param source
26
+ * @param stepIdToVariableMap
27
+ * @returns
28
+ */
29
+ export const convertSourceToCode = (
30
+ source: Source,
31
+ stepIdToVariableMap: Record<string, string>,
32
+ isTokenizedPart: boolean = false,
33
+ ): string => {
34
+ let value = '';
35
+
36
+ switch (source.type) {
37
+ case 'VALUE':
38
+ value = '"' + source.value + '"';
39
+ break;
40
+ case 'TOKENIZED':
41
+ value = source.parts.reduce((acc: string, currentSource: Source) => {
42
+ return `${acc}${convertSourceToCode(
43
+ currentSource,
44
+ stepIdToVariableMap,
45
+ true,
46
+ )}`;
47
+ }, '');
48
+
49
+ value = '`' + value + '`';
50
+ break;
51
+ case 'VARIABLE':
52
+ const sanitizedPaths: string[] = source.path.filter((path) =>
53
+ Boolean(path),
54
+ );
55
+
56
+ value = `context.getOutput(${stepIdToVariableMap[source.stepId]})${
57
+ sanitizedPaths.length ? `?.${sanitizedPaths.join('?.')}` : ''
58
+ }`;
59
+
60
+ if (isTokenizedPart) {
61
+ value = '${' + value + '}';
62
+ }
63
+ break;
64
+ case 'CONDITION':
65
+ const conditionWrapper: ConditionWrapper = source.condition;
66
+
67
+ // in paragon cloud top condition is `OR`
68
+ if (conditionWrapper.type !== 'JOIN' || conditionWrapper.join !== 'OR') {
69
+ return 'undefined';
70
+ }
71
+
72
+ const conditions: Array<OperatorCondition[]> = conditionWrapper.conditions
73
+ .map((condition) => {
74
+ if (condition.type === 'JOIN' && condition.join === 'AND') {
75
+ const andConditions: OperatorCondition[] =
76
+ condition.conditions as unknown as OperatorCondition[];
77
+ return andConditions;
78
+ }
79
+ return [];
80
+ })
81
+ .filter((conditions: OperatorCondition[]) => conditions.length);
82
+
83
+ /**
84
+ * convert conditions to code
85
+ * @param conditions
86
+ * @returns
87
+ */
88
+ const convertAndConditionsToCode = (
89
+ conditions: OperatorCondition[],
90
+ stepIdToVariableMap: Record<string, string>,
91
+ ) => {
92
+ return conditions.reduce(
93
+ (
94
+ accumulator: string,
95
+ condition: OperatorCondition,
96
+ index: number,
97
+ ) => {
98
+ const conditionCode: string = conditionToCode(
99
+ condition,
100
+ stepIdToVariableMap,
101
+ );
102
+ if (index === conditions.length - 1) {
103
+ return `${accumulator}${conditionCode}]`;
104
+ }
105
+ return `${accumulator}${conditionCode},`;
106
+ },
107
+ '[',
108
+ );
109
+ };
110
+
111
+ if (conditions.length === 1 && conditions[0].length === 1) {
112
+ // only add code for only one Condition
113
+ const condition: OperatorCondition = conditions[0][0];
114
+ value = conditionToCode(condition, stepIdToVariableMap);
115
+ } else if (conditions.length === 1) {
116
+ // add code for AND(...operators) condition type
117
+ const andConditions: OperatorCondition[] = conditions[0];
118
+ value = convertAndConditionsToCode(andConditions, stepIdToVariableMap);
119
+ } else {
120
+ // add code for OR(AND(...operators)) condition type
121
+ const orConditions: OperatorCondition[][] = conditions;
122
+ value = orConditions.reduce(
123
+ (
124
+ accumulator: string,
125
+ andConditions: OperatorCondition[],
126
+ index: number,
127
+ ) => {
128
+ const andConditionCode: string = convertAndConditionsToCode(
129
+ andConditions,
130
+ stepIdToVariableMap,
131
+ );
132
+ if (index === orConditions.length - 1) {
133
+ return `${accumulator}${andConditionCode}]`;
134
+ }
135
+ return `${accumulator}${andConditionCode},`;
136
+ },
137
+ '[',
138
+ );
139
+ }
140
+
141
+ break;
142
+ default:
143
+ throw new Error(`source type ${source.type} not implemented.`);
144
+ }
145
+ return value;
146
+ };
147
+
148
+ /**
149
+ * convert keyed source to code
150
+ * @param source
151
+ * @param stepIdToVariableMap
152
+ * @returns
153
+ */
154
+ export const convertKeyedSourceToCode = (
155
+ source: KeyedSource,
156
+ stepIdToVariableMap: Record<string, string>,
157
+ ): [string, string] => {
158
+ return [source.key, convertSourceToCode(source.source, stepIdToVariableMap)];
159
+ };
160
+
161
+ /**
162
+ * convert keyed sources to their respective source code
163
+ * @param parameters
164
+ * @param stepIdToVariableMap
165
+ * @returns
166
+ */
167
+ export const covertKeyedSourceParamsToCode = (
168
+ parameters: KeyedSource<DataType>[],
169
+ stepIdToVariableMap: Record<string, string>,
170
+ ): string => {
171
+ if (!parameters.length) {
172
+ return '{}';
173
+ }
174
+ const value: string = parameters.reduce(
175
+ (accumulator: string, keyedSource: KeyedSource, index: number) => {
176
+ const [key, value]: [string, string] = convertKeyedSourceToCode(
177
+ keyedSource,
178
+ stepIdToVariableMap,
179
+ );
180
+ if (index > 0) {
181
+ accumulator += ',';
182
+ }
183
+
184
+ if (index === parameters.length - 1) {
185
+ return `${accumulator}${key} : ${value}}`;
186
+ }
187
+ return `${accumulator}${key} : ${value}`;
188
+ },
189
+ '{',
190
+ );
191
+
192
+ return value;
193
+ };
@@ -0,0 +1,495 @@
1
+ import { IStep, StepType } from '@useparagon/core';
2
+ import { ConditionWrapper } from '@useparagon/core/resolvers';
3
+ import { getCronOptions } from '@useparagon/core/triggers/cron';
4
+
5
+ import { WorkflowOutput } from '../../compilers/compiler.types';
6
+ import { stringifyObject } from '../../helpers/json';
7
+ import logger from '../../logger';
8
+ import { WorkflowDependencies } from '../codegen.types';
9
+ import {
10
+ convertSourceToCode,
11
+ covertKeyedSourceParamsToCode,
12
+ EMPTY_TOKENIZED_SOURCE,
13
+ } from './source.codegen';
14
+
15
+ type StepParse = {
16
+ imports: string[];
17
+ varDeclarations: string[];
18
+ stepChains: string;
19
+ };
20
+
21
+ /**
22
+ * get source code for workflow
23
+ * @param event
24
+ * @returns
25
+ */
26
+ export const workflowToCode = (
27
+ workflowBuild: WorkflowOutput,
28
+ dependencies: WorkflowDependencies,
29
+ ): string => {
30
+ const { id: workflowId, name: workflowName, stateMachine } = workflowBuild;
31
+ const startStepId: string | undefined =
32
+ stateMachine.sequenceMap[stateMachine.start].start;
33
+ if (!startStepId) {
34
+ logger.fatal(`Workflow ${workflowName} with no start step id.`);
35
+ return '';
36
+ }
37
+
38
+ const stepIdToVariableMap: Record<string, string> = {};
39
+ const imports: string[] = [
40
+ 'import { IContext} from "@useparagon/core/execution";',
41
+ 'import { Workflow, ActionStep, CronStep, DelayStep, EventStep, FunctionStep, ConditionalStep, FanOutStep, ResponseStep, RequestStep, IntegrationEnabledStep} from "@useparagon/core";',
42
+ 'import * as Operators from "@useparagon/core/operator";',
43
+ ];
44
+
45
+ const statements: string[] = [];
46
+
47
+ const stepCode: StepParse = getStepCodeParts(
48
+ startStepId,
49
+ stateMachine.stepMap,
50
+ stepIdToVariableMap,
51
+ dependencies,
52
+ );
53
+
54
+ // removing duplicate imports as well
55
+ imports.push(...new Set(stepCode.imports));
56
+ statements.push(...stepCode.varDeclarations);
57
+
58
+ const sourceCode: string = `
59
+ ${imports.join('\n')}
60
+
61
+ /**
62
+ * ${workflowName} Workflow impolementation
63
+ */
64
+ export default class extends Workflow<any> {
65
+ /**
66
+ * This property is maintained by Paragon. Do not edit this property.
67
+ */
68
+ readonly id: '${workflowId}'
69
+
70
+ /**
71
+ * defines the workflow
72
+ * @param integration
73
+ * @param context
74
+ * @param user
75
+ */
76
+ define(_integration: any, context: IContext) {
77
+
78
+ /**
79
+ * declare all steps here
80
+ */
81
+ ${statements.join('\n')}
82
+
83
+ /**
84
+ * chain steps correctly here
85
+ */
86
+ ${stepCode.stepChains}
87
+
88
+ /**
89
+ * pass all steps here so that paragon can keep track of changes
90
+ */
91
+ return this.register({ ${Object.values(stepIdToVariableMap).join(
92
+ ', ',
93
+ )} });
94
+ }
95
+ }
96
+ `;
97
+
98
+ return sourceCode;
99
+ };
100
+
101
+ /**
102
+ * get code parts for steps
103
+ * @param startStepId
104
+ * @param stepMap
105
+ * @param stepIdToVariableMap
106
+ * @returns
107
+ */
108
+ const getStepCodeParts = (
109
+ startStepId: string,
110
+ stepMap: Record<string, IStep>,
111
+ stepIdToVariableMap: Record<string, string>,
112
+ dependencies: WorkflowDependencies,
113
+ ): StepParse => {
114
+ const imports: string[] = [];
115
+ const varDeclarations: string[] = [];
116
+
117
+ let stepChains: string = '';
118
+
119
+ let currentStep: IStep | undefined = stepMap[startStepId];
120
+
121
+ let stepCount: number = 1;
122
+
123
+ const totalKeys: number = Object.values(stepIdToVariableMap).length;
124
+ const prefix: string = totalKeys ? `${totalKeys}_` : '';
125
+
126
+ const variableChains: string[] = [];
127
+
128
+ while (currentStep != undefined) {
129
+ const variableName: string = (currentStep._migrations?.['name'] ||
130
+ `step${prefix}${stepCount}`) as string;
131
+
132
+ stepIdToVariableMap[currentStep!.id] = variableName;
133
+
134
+ const stepCode = getStepCodePart(
135
+ currentStep!,
136
+ stepIdToVariableMap,
137
+ stepMap,
138
+ dependencies,
139
+ );
140
+
141
+ imports.push(...stepCode.imports);
142
+ varDeclarations.push(...stepCode.varDeclarations);
143
+
144
+ if (stepCode.stepChains.trim()) {
145
+ variableChains.push(`
146
+ ${variableName}.${stepCode.stepChains}
147
+ `);
148
+ } else {
149
+ variableChains.push(variableName);
150
+ }
151
+
152
+ const nextStepId = currentStep.next;
153
+ currentStep = undefined;
154
+
155
+ if (nextStepId) {
156
+ currentStep = stepMap[nextStepId];
157
+ }
158
+ stepCount += 1;
159
+ }
160
+
161
+ if (variableChains.length) {
162
+ stepChains = `
163
+ ${variableChains
164
+ .map((currentName: string, index: number) => {
165
+ if (index > 0) {
166
+ return `${currentName})`;
167
+ }
168
+ return currentName;
169
+ })
170
+ .join('.nextStep(')}
171
+
172
+ `;
173
+ }
174
+
175
+ return {
176
+ imports,
177
+ varDeclarations,
178
+ stepChains,
179
+ };
180
+ };
181
+
182
+ /**
183
+ * get statemenets and imorts statements for particular step
184
+ * @param step
185
+ * @returns
186
+ */
187
+ const getStepCodePart = (
188
+ step: IStep,
189
+ stepIdToVariableMap: Record<string, string>,
190
+ stepMap: Record<string, IStep>,
191
+ dependencies: WorkflowDependencies,
192
+ ): StepParse => {
193
+ const varDeclarations: string[] = [];
194
+ const imports: string[] = [];
195
+
196
+ let stepChains: string = '';
197
+
198
+ switch (step.type) {
199
+ case StepType.ACTION:
200
+ varDeclarations.push(`
201
+ const ${
202
+ stepIdToVariableMap[step.id]
203
+ }:ActionStep = new ActionStep({
204
+ actionType : ${step.parameters.actionType},
205
+ intent : ${step.parameters.intent},
206
+ parameters : ${covertKeyedSourceParamsToCode(
207
+ step.parameters.actionParameters,
208
+ stepIdToVariableMap,
209
+ )}
210
+ });
211
+ `);
212
+
213
+ break;
214
+ // case StepType.ACTION_TRIGGER:
215
+ // break;
216
+ case StepType.CRON:
217
+ varDeclarations.push(`
218
+ const ${
219
+ stepIdToVariableMap[step.id]
220
+ }:CronStep = new CronStep(${stringifyObject(
221
+ getCronOptions(step.parameters.schedule),
222
+ )});
223
+ `);
224
+ break;
225
+ case StepType.CUSTOM_INTEGRATION_REQUEST:
226
+ const requestBodyType: string = step.parameters.bodyType ?? 'json';
227
+ varDeclarations.push(`
228
+ const ${
229
+ stepIdToVariableMap[step.id]
230
+ }: RequestStep = new RequestStep({
231
+ path: ${convertSourceToCode(
232
+ step.parameters.url,
233
+ stepIdToVariableMap,
234
+ )},
235
+ params:${covertKeyedSourceParamsToCode(
236
+ step.parameters.params,
237
+ stepIdToVariableMap,
238
+ )},
239
+ headers:${covertKeyedSourceParamsToCode(
240
+ step.parameters.params,
241
+ stepIdToVariableMap,
242
+ )},
243
+ body:${
244
+ ['xml', 'raw'].includes(requestBodyType)
245
+ ? convertSourceToCode(
246
+ step.parameters.rawBody || EMPTY_TOKENIZED_SOURCE,
247
+ stepIdToVariableMap,
248
+ )
249
+ : covertKeyedSourceParamsToCode(
250
+ step.parameters.body,
251
+ stepIdToVariableMap,
252
+ )
253
+ },
254
+ ${step.parameters.pagination ? '' : ''}
255
+ });
256
+ `);
257
+ break;
258
+ case StepType.DELAY:
259
+ varDeclarations.push(`
260
+ const ${stepIdToVariableMap[step.id]}:DelayStep = new DelayStep({
261
+ unit: '${step.parameters.unit}',
262
+ value: ${convertSourceToCode(
263
+ step.parameters.value,
264
+ stepIdToVariableMap,
265
+ )},
266
+ });
267
+ `);
268
+ break;
269
+ case StepType.ENDPOINT:
270
+ varDeclarations.push(`
271
+ const ${stepIdToVariableMap[step.id]}:EndpointStep = new EndpointStep({
272
+ allowArbitraryPayload : ${step.parameters.allowArbitraryPayload},
273
+ ${
274
+ step.parameters.allowArbitraryPayload
275
+ ? ''
276
+ : `
277
+ paramValidations:${stringifyObject(
278
+ step.parameters.paramValidations,
279
+ )},
280
+ headerValidations:${stringifyObject(
281
+ step.parameters.headerValidations,
282
+ )},
283
+ bodyValidations:${stringifyObject(step.parameters.bodyValidations)}
284
+ `
285
+ }
286
+ });
287
+ `);
288
+ break;
289
+ case StepType.EVENT:
290
+ const eventName: string =
291
+ dependencies.events[step.parameters.eventId].name;
292
+
293
+ imports.push(...[`import event from '../../../events/${eventName}';`]);
294
+
295
+ varDeclarations.push(`
296
+ const ${
297
+ stepIdToVariableMap[step.id]
298
+ }:EventStep = new EventStep(event);
299
+ `);
300
+ break;
301
+ case StepType.FUNCTION:
302
+ const paramsCode: string = covertKeyedSourceParamsToCode(
303
+ step.parameters.parameters,
304
+ stepIdToVariableMap,
305
+ );
306
+
307
+ varDeclarations.push(`
308
+ const ${
309
+ stepIdToVariableMap[step.id]
310
+ }:FunctionStep = new FunctionStep({
311
+ code: ${step.parameters.code},
312
+ parameters: ${paramsCode}
313
+ });
314
+ `);
315
+ break;
316
+ case StepType.IFELSE:
317
+ const ifStepId: string | null | undefined = step.parameters.choices.find(
318
+ (choice) => choice.label === 'Yes',
319
+ )?.next;
320
+ const elseStepId: string | null | undefined =
321
+ step.parameters.choices.find((choice) => choice.label === 'No')?.next;
322
+
323
+ if (ifStepId) {
324
+ const {
325
+ imports: nextIterateImports,
326
+ varDeclarations: nextIterateVarDeclartions,
327
+ stepChains: nextIterationStepChains,
328
+ }: StepParse = getStepCodeParts(
329
+ ifStepId,
330
+ stepMap,
331
+ stepIdToVariableMap,
332
+ dependencies,
333
+ );
334
+
335
+ imports.push(...nextIterateImports);
336
+ varDeclarations.push(...nextIterateVarDeclartions);
337
+ stepChains += `whenTrue(${nextIterationStepChains})`;
338
+ }
339
+
340
+ if (elseStepId) {
341
+ const {
342
+ imports: nextIterateImports,
343
+ varDeclarations: nextIterateVarDeclartions,
344
+ stepChains: nextIterationStepChains,
345
+ }: StepParse = getStepCodeParts(
346
+ elseStepId,
347
+ stepMap,
348
+ stepIdToVariableMap,
349
+ dependencies,
350
+ );
351
+
352
+ imports.push(...nextIterateImports);
353
+ varDeclarations.push(...nextIterateVarDeclartions);
354
+
355
+ stepChains += ifStepId
356
+ ? `.whenFalse(${nextIterationStepChains})`
357
+ : `whenFalse(${nextIterationStepChains})`;
358
+ }
359
+
360
+ const ifCondition: ConditionWrapper | undefined =
361
+ step.parameters.choices.find(
362
+ (choice) => choice.label === 'Yes',
363
+ )?.conditionWrapper;
364
+
365
+ varDeclarations.push(`
366
+ const ${
367
+ stepIdToVariableMap[step.id]
368
+ }:ConditionalStep = new ConditionalStep({
369
+ if : ${
370
+ !ifCondition
371
+ ? 'undefined'
372
+ : convertSourceToCode(
373
+ {
374
+ type: 'CONDITION',
375
+ condition: ifCondition,
376
+ },
377
+ stepIdToVariableMap,
378
+ )
379
+ }
380
+ });
381
+ `);
382
+ break;
383
+ case StepType.INTEGRATION_ENABLED:
384
+ varDeclarations.push(`
385
+ const ${
386
+ stepIdToVariableMap[step.id]
387
+ }:IntegrationEnabledStep = new IntegrationEnabledStep({});
388
+ `);
389
+ break;
390
+ case StepType.MAP:
391
+ let nextStepId: string | null | undefined = step.parameters.nextToIterate;
392
+
393
+ if (nextStepId) {
394
+ const {
395
+ imports: nextIterateImports,
396
+ varDeclarations: nextIterateVarDeclartions,
397
+ stepChains: nextIterationStepChains,
398
+ }: StepParse = getStepCodeParts(
399
+ nextStepId,
400
+ stepMap,
401
+ stepIdToVariableMap,
402
+ dependencies,
403
+ );
404
+
405
+ imports.push(...nextIterateImports);
406
+ varDeclarations.push(...nextIterateVarDeclartions);
407
+
408
+ stepChains = `branch(${nextIterationStepChains})`;
409
+ }
410
+
411
+ varDeclarations.push(`
412
+ const ${
413
+ stepIdToVariableMap[step.id]
414
+ }:FanOutStep = new FanOutStep({
415
+ iterator : ${convertSourceToCode(
416
+ step.parameters.iterator,
417
+ stepIdToVariableMap,
418
+ )},
419
+ });
420
+ `);
421
+ break;
422
+ case StepType.RESPONSE:
423
+ const responseType: string = step.parameters.responseType ?? 'JSON';
424
+ varDeclarations.push(`
425
+ const ${
426
+ stepIdToVariableMap[step.id]
427
+ }: ResponseStep = new ResponseStep({
428
+ statusCode:${step.parameters.statusCode},
429
+ responseType:"${responseType}",
430
+ body: ${
431
+ responseType === 'FILE'
432
+ ? step.parameters.bodyFile
433
+ ? convertSourceToCode(
434
+ step.parameters.bodyFile || EMPTY_TOKENIZED_SOURCE,
435
+ stepIdToVariableMap,
436
+ )
437
+ : '""'
438
+ : covertKeyedSourceParamsToCode(
439
+ step.parameters.bodyData,
440
+ stepIdToVariableMap,
441
+ )
442
+ }
443
+ });
444
+ `);
445
+ break;
446
+ case StepType.REQUEST:
447
+ const bodyType: string = step.parameters.bodyType ?? 'json';
448
+ varDeclarations.push(`
449
+ const ${
450
+ stepIdToVariableMap[step.id]
451
+ }: RequestStep = new RequestStep({
452
+ url: ${convertSourceToCode(
453
+ step.parameters.url,
454
+ stepIdToVariableMap,
455
+ )},
456
+ method:"${step.parameters.httpMethod}",
457
+ params:${covertKeyedSourceParamsToCode(
458
+ step.parameters.params,
459
+ stepIdToVariableMap,
460
+ )},
461
+ headers:${covertKeyedSourceParamsToCode(
462
+ step.parameters.params,
463
+ stepIdToVariableMap,
464
+ )},
465
+ ${
466
+ step.parameters.httpMethod === 'GET'
467
+ ? ''
468
+ : `
469
+ body:${
470
+ ['xml', 'raw'].includes(bodyType)
471
+ ? convertSourceToCode(
472
+ step.parameters.rawBody || EMPTY_TOKENIZED_SOURCE,
473
+ stepIdToVariableMap,
474
+ )
475
+ : covertKeyedSourceParamsToCode(
476
+ step.parameters.body,
477
+ stepIdToVariableMap,
478
+ )
479
+ }`
480
+ }
481
+ });
482
+ `);
483
+ break;
484
+ // case StepType.UNSELECTED_TRIGGER:
485
+ // break;
486
+ default:
487
+ logger.fatal(`Invalid step type "${step['type']}`);
488
+ }
489
+
490
+ return {
491
+ imports,
492
+ varDeclarations,
493
+ stepChains,
494
+ };
495
+ };