@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,68 @@
1
+ import axios, { AxiosError, AxiosResponse } from 'axios';
2
+
3
+ import { IAuth, IProfile, IProfileAuth } from '../../config';
4
+ import logger from '../../logger';
5
+
6
+ import { ILoginResponse } from './login.types';
7
+
8
+ type FormattedAxiosError = {
9
+ message: string;
10
+ code?: string;
11
+ status?: number;
12
+ statusText?: string;
13
+ };
14
+
15
+ /**
16
+ * logs into a profile
17
+ *
18
+ * @param auth
19
+ * @param profile
20
+ */
21
+ export async function login(
22
+ auth: IAuth,
23
+ profile: IProfile,
24
+ ): Promise<IProfileAuth> {
25
+ const baseURL: string | undefined = profile.services?.zeus;
26
+ if (!baseURL) {
27
+ throw new Error('No url defined for Zeus!');
28
+ }
29
+
30
+ try {
31
+ const { username, password } = auth;
32
+ if (!username) {
33
+ throw new Error('No username configured for profile.');
34
+ } else if (!password) {
35
+ throw new Error('No password configured for profile.');
36
+ }
37
+
38
+ const response: AxiosResponse<ILoginResponse> = await axios.post(
39
+ '/auth/login/email',
40
+ {
41
+ username,
42
+ password,
43
+ },
44
+ {
45
+ baseURL,
46
+ headers: {
47
+ 'Content-Type': 'application/json',
48
+ },
49
+ },
50
+ );
51
+
52
+ return {
53
+ ...auth,
54
+ profile: profile.name,
55
+ token: response.data.accessToken,
56
+ };
57
+ } catch (e) {
58
+ const typedError: AxiosError = e as AxiosError;
59
+ const formattedError: FormattedAxiosError = {
60
+ code: typedError.code,
61
+ message: typedError.message,
62
+ status: typedError.response?.status,
63
+ statusText: typedError.response?.statusText,
64
+ };
65
+ logger.warn('Failed to login.', formattedError);
66
+ throw new Error(formattedError.message);
67
+ }
68
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * interface for compilation of file
3
+ */
4
+ export interface ICompiler<Options, Output> {
5
+ /**
6
+ * compile the file
7
+ */
8
+ compile(fileName: string, options: Options): Promise<Output>;
9
+ }
@@ -0,0 +1,52 @@
1
+ import { IIntegrationConfig, StateMachine } from '@useparagon/core';
2
+ import { IEvent } from '@useparagon/core/event';
3
+
4
+ /**
5
+ * workflow build output
6
+ */
7
+ export type WorkflowOutput = {
8
+ id: string;
9
+ name: string;
10
+ stateMachine: StateMachine;
11
+ };
12
+
13
+ /**
14
+ * integration output
15
+ */
16
+ export type IntegrationOutput = {
17
+ // can be skip for default integration
18
+ config?: IIntegrationConfig;
19
+
20
+ /**
21
+ * map of workflowId->workflowoutput
22
+ */
23
+ workflows: Record<string, WorkflowOutput>;
24
+ };
25
+
26
+ /**
27
+ * project build output
28
+ */
29
+ export type BuildOutput = {
30
+ /**
31
+ * map of eventId->event entity
32
+ */
33
+ events: Record<string, IEvent>;
34
+
35
+ /**
36
+ * map of integrationName->integration output
37
+ */
38
+ integrations: Record<string, IntegrationOutput>;
39
+ };
40
+
41
+ /**
42
+ * integration depdencies
43
+ */
44
+ export type ProjectDependencies = {
45
+ events: Record<string, IEvent>;
46
+ };
47
+
48
+ export type WorkflowDependencies = ProjectDependencies & {
49
+ integration: {
50
+ name: string;
51
+ };
52
+ };
@@ -0,0 +1,32 @@
1
+ import logger from '../logger';
2
+
3
+ /**
4
+ * it will extract key {integration , workflow ,event} name from file path
5
+ * @param filePath
6
+ * @returns
7
+ */
8
+ export const extractKeyFromPath = (filePath: string): string => {
9
+ const names: string[] = filePath.split('/');
10
+ const fileName: string = names[names.length - 1];
11
+
12
+ //as .js is file extension
13
+ if (fileName.endsWith('.js')) {
14
+ return fileName.slice(0, -3);
15
+ }
16
+
17
+ return fileName;
18
+ };
19
+
20
+ /**
21
+ * require module + exit process
22
+ * @param filePath
23
+ * @returns
24
+ */
25
+ export const requireSafe = <T>(filePath: string): T => {
26
+ try {
27
+ return require(filePath).default as T;
28
+ } catch {
29
+ logger.error(`Could not found ${filePath}.`);
30
+ process.exit(1);
31
+ }
32
+ };
@@ -0,0 +1,33 @@
1
+ import path from 'path';
2
+
3
+ import { IEvent } from '@useparagon/core/event';
4
+
5
+ import spinners from '../helpers/spinners';
6
+ import { requireSafe } from './compiler.utils';
7
+
8
+ export class EventCompiler {
9
+ /**
10
+ * get event's payload
11
+ * @param options
12
+ * @returns
13
+ */
14
+ async compile(options: {
15
+ projectRoot: string;
16
+ event: string;
17
+ }): Promise<IEvent> {
18
+ spinners.add(`compile-event:${options.event}`, {
19
+ text: `building event ${options.event}`,
20
+ indent: 2,
21
+ });
22
+
23
+ const event: IEvent = requireSafe(
24
+ path.join(options.projectRoot, 'events', `${options.event}.js`),
25
+ );
26
+
27
+ spinners.succeed(`compile-event:${options.event}`, {
28
+ text: `built event ${options.event}`,
29
+ indent: 2,
30
+ });
31
+ return event;
32
+ }
33
+ }
@@ -0,0 +1,84 @@
1
+ import { glob } from 'glob';
2
+ import path from 'path';
3
+
4
+ import { IIntegrationConfig } from '@useparagon/core';
5
+
6
+ import spinners from '../helpers/spinners';
7
+ import logger from '../logger';
8
+ import { IntegrationOutput, WorkflowOutput } from './compiler.types';
9
+ import { extractKeyFromPath } from './compiler.utils';
10
+ import { WorkflowCompiler } from './workflow.compiler';
11
+
12
+ export class IntegrationCompiler {
13
+ private workflowCompiler: WorkflowCompiler;
14
+
15
+ constructor() {
16
+ this.workflowCompiler = new WorkflowCompiler();
17
+ }
18
+
19
+ /**
20
+ * get integration's build output
21
+ * @param options
22
+ * @returns
23
+ */
24
+ async compile(options: {
25
+ integration: string;
26
+ projectRoot: string;
27
+ }): Promise<IntegrationOutput> {
28
+ const { integration, projectRoot } = options;
29
+
30
+ spinners.add(`compile-integration:${integration}`, {
31
+ text: `building integration ${integration}`,
32
+ indent: 2,
33
+ });
34
+
35
+ const workflowKeys: string[] = (
36
+ await glob(
37
+ path.join(projectRoot, 'integrations', integration, `workflows/*.js`),
38
+ {
39
+ nodir: true,
40
+ },
41
+ )
42
+ ).map(extractKeyFromPath);
43
+
44
+ const isCustomIntegration: boolean = integration.startsWith('custom.');
45
+ let config: IIntegrationConfig | undefined;
46
+
47
+ try {
48
+ config = require(path.join(
49
+ projectRoot,
50
+ 'integrations',
51
+ integration,
52
+ 'config.js',
53
+ ));
54
+ } catch {
55
+ if (isCustomIntegration) {
56
+ logger.fatal(`Config file not found for ${integration}.`);
57
+ }
58
+ }
59
+
60
+ const compiledWorkflowEntries: Array<[string, WorkflowOutput]> =
61
+ await Promise.all(
62
+ workflowKeys.map(
63
+ async (workflowName: string): Promise<[string, WorkflowOutput]> => {
64
+ const result: WorkflowOutput = await this.workflowCompiler.compile({
65
+ projectRoot,
66
+ workflow: workflowName,
67
+ integration,
68
+ });
69
+ return [result.id, result];
70
+ },
71
+ ),
72
+ );
73
+
74
+ spinners.succeed(`compile-integration:${integration}`, {
75
+ text: `built integration ${integration}`,
76
+ indent: 2,
77
+ });
78
+
79
+ return {
80
+ config,
81
+ workflows: Object.fromEntries(compiledWorkflowEntries),
82
+ };
83
+ }
84
+ }
@@ -0,0 +1,86 @@
1
+ import { glob } from 'glob';
2
+ import path from 'path';
3
+
4
+ import { IEvent } from '@useparagon/core/event';
5
+
6
+ import logger from '../logger';
7
+ import { EventCompiler } from './event.compiler';
8
+ import { IntegrationCompiler } from './integration.compiler';
9
+ import { BuildOutput, IntegrationOutput } from './compiler.types';
10
+ import { extractKeyFromPath } from './compiler.utils';
11
+
12
+ export class ProjectCompiler {
13
+ private eventCompiler: EventCompiler;
14
+ private integrationCompiler: IntegrationCompiler;
15
+ constructor() {
16
+ this.eventCompiler = new EventCompiler();
17
+ this.integrationCompiler = new IntegrationCompiler();
18
+ }
19
+
20
+ /**
21
+ * compile project
22
+ * @param options
23
+ * @returns
24
+ */
25
+ async compile(options: {
26
+ projectRoot: string;
27
+ integrations: string[];
28
+ }): Promise<BuildOutput> {
29
+ const { integrations, projectRoot } = options;
30
+
31
+ const userIntegrations: string[] = (
32
+ await glob(path.join(projectRoot, 'integrations/*/'))
33
+ ).map(extractKeyFromPath);
34
+
35
+ //filter integrations that are not presnent in project
36
+ const notPresentIntegrations: string[] = userIntegrations.filter(
37
+ (integration: string): boolean => integrations.includes(integration),
38
+ );
39
+
40
+ if (integrations.length && !notPresentIntegrations.length) {
41
+ logger.error(
42
+ `Could not found Integration with key "${notPresentIntegrations.join(
43
+ ',',
44
+ )}`,
45
+ );
46
+ process.exit(1);
47
+ }
48
+
49
+ const userEvents: string[] = (
50
+ await glob(path.join(projectRoot, 'events/*.js'))
51
+ ).map(extractKeyFromPath);
52
+
53
+ const eventEntries: Array<[string, IEvent]> = await Promise.all(
54
+ userEvents.map(async (event: string): Promise<[string, IEvent]> => {
55
+ const result = await this.eventCompiler.compile({ projectRoot, event });
56
+ return [result.id!, result];
57
+ }),
58
+ );
59
+
60
+ const events: Record<string, IEvent> = Object.fromEntries(eventEntries);
61
+
62
+ const integrationEntries: Array<[string, IntegrationOutput]> =
63
+ await Promise.all(
64
+ userIntegrations
65
+ .filter((integration: string): boolean =>
66
+ integrations.length ? integrations.includes(integration) : true,
67
+ )
68
+ .map(
69
+ async (
70
+ integration: string,
71
+ ): Promise<[string, IntegrationOutput]> => [
72
+ integration,
73
+ await this.integrationCompiler.compile({
74
+ integration,
75
+ projectRoot,
76
+ }),
77
+ ],
78
+ ),
79
+ );
80
+
81
+ return {
82
+ events,
83
+ integrations: Object.fromEntries(integrationEntries),
84
+ };
85
+ }
86
+ }
@@ -0,0 +1,116 @@
1
+ import { join } from 'path';
2
+ import fs from 'fs-extra';
3
+ import * as ts from 'typescript';
4
+
5
+ import logger from '../logger';
6
+
7
+ //https://github.com/microsoft/TypeScript-wiki/blob/main/Using-the-Compiler-API.md#a-minimal-compiler
8
+ export class TsCompiler {
9
+ private tsBinary: typeof ts | undefined;
10
+
11
+ /**
12
+ * run typescript compiler
13
+ * @param tsConfigPath
14
+ */
15
+ public run({ configPath }: { configPath?: string }) {
16
+ const tsBinary: typeof ts = this.getTsBinary();
17
+
18
+ const tsConfigPath: string = configPath ?? this.getDefaultTsconfigPath();
19
+
20
+ const formatHost: ts.FormatDiagnosticsHost = {
21
+ getCanonicalFileName: (path: string) => path,
22
+ getCurrentDirectory: tsBinary.sys.getCurrentDirectory,
23
+ getNewLine: () => tsBinary.sys.newLine,
24
+ };
25
+
26
+ const parsedCmd: ts.ParsedCommandLine | undefined =
27
+ tsBinary.getParsedCommandLineOfConfigFile(
28
+ tsConfigPath,
29
+ undefined,
30
+ tsBinary.sys as unknown as ts.ParseConfigFileHost,
31
+ );
32
+
33
+ const { options, fileNames, projectReferences } = parsedCmd!;
34
+
35
+ const createProgram =
36
+ tsBinary.createIncrementalProgram || tsBinary.createProgram;
37
+ const program = createProgram.call(ts, {
38
+ rootNames: fileNames,
39
+ projectReferences,
40
+ options,
41
+ });
42
+
43
+ const emitResult = program.emit();
44
+
45
+ const diagnostics = tsBinary
46
+ .getPreEmitDiagnostics(program as unknown as ts.Program)
47
+ .concat(emitResult.diagnostics);
48
+
49
+ if (diagnostics.length > 0) {
50
+ console.error(
51
+ tsBinary.formatDiagnosticsWithColorAndContext(diagnostics, formatHost),
52
+ );
53
+ console.info(
54
+ `Found ${diagnostics.length} error(s).` + tsBinary.sys.newLine,
55
+ );
56
+ }
57
+
58
+ if (diagnostics.length) {
59
+ process.exit(1);
60
+ }
61
+ }
62
+
63
+ /**
64
+ * return tsconfig path
65
+ */
66
+ private getDefaultTsconfigPath(): string {
67
+ const tsConfigFileName: string = fs.existsSync(
68
+ join(process.cwd(), 'tsconfig.build.json'),
69
+ )
70
+ ? 'tsconfig.build.json'
71
+ : 'tsconfig.json';
72
+
73
+ return join(process.cwd(), tsConfigFileName);
74
+ }
75
+
76
+ /**
77
+ * get host project's typescript binary
78
+ * @returns
79
+ */
80
+ private getTsBinary(): typeof ts {
81
+ if (this.tsBinary) {
82
+ return this.tsBinary;
83
+ }
84
+
85
+ try {
86
+ const tsBinaryPath = require.resolve('typescript', {
87
+ paths: [process.cwd()],
88
+ });
89
+ this.tsBinary = require(tsBinaryPath) as typeof ts;
90
+
91
+ return this.tsBinary!;
92
+ } catch {
93
+ logger.error(
94
+ 'TypeScript could not be found! Please, install "typescript" package.',
95
+ );
96
+ process.exit(1);
97
+ }
98
+ }
99
+
100
+ /**
101
+ * convert typescript code to js
102
+ * @param code
103
+ * @returns
104
+ */
105
+ public transpileToJSCode(code: string): string {
106
+ const result = ts.transpileModule(code, {
107
+ compilerOptions: { module: ts.ModuleKind.ES2015 },
108
+ });
109
+
110
+ return result.outputText;
111
+ }
112
+ }
113
+
114
+ const tsCompiler = new TsCompiler();
115
+
116
+ export default tsCompiler;
@@ -0,0 +1,69 @@
1
+ import path from 'path';
2
+
3
+ import {
4
+ IExecutableWorkflow,
5
+ IIntegrationConfig,
6
+ StateMachine,
7
+ } from '@useparagon/core';
8
+ import { Context } from '@useparagon/core/execution';
9
+
10
+ import spinners from '../helpers/spinners';
11
+ import { WorkflowOutput } from './compiler.types';
12
+ import { requireSafe } from './compiler.utils';
13
+
14
+ interface Constructable<T> {
15
+ new (...args: any): T;
16
+ }
17
+
18
+ export class WorkflowCompiler {
19
+ private context: Context;
20
+
21
+ constructor() {
22
+ this.context = new Context();
23
+ }
24
+
25
+ /**
26
+ * build workflow
27
+ * @param options
28
+ * @returns
29
+ */
30
+ async compile(options: {
31
+ integration: string;
32
+ workflow: string;
33
+ projectRoot: string;
34
+ }): Promise<WorkflowOutput> {
35
+ spinners.add(`compile-workflow:${options.workflow}`, {
36
+ text: `building workflow ${options.workflow}`,
37
+ indent: 4,
38
+ });
39
+
40
+ const workflowPath: string = path.join(
41
+ options.projectRoot,
42
+ 'integrations',
43
+ options.integration,
44
+ 'workflows',
45
+ `${options.workflow}.js`,
46
+ );
47
+ const Workflow: Constructable<IExecutableWorkflow<IIntegrationConfig>> =
48
+ requireSafe(workflowPath);
49
+ const workflowInstance: IExecutableWorkflow<IIntegrationConfig> =
50
+ new Workflow();
51
+
52
+ const stateMachine: StateMachine = await workflowInstance.define(
53
+ {} as IIntegrationConfig,
54
+ this.context,
55
+ {},
56
+ );
57
+
58
+ spinners.succeed(`compile-workflow:${options.workflow}`, {
59
+ text: `built workflow ${options.workflow}`,
60
+ indent: 4,
61
+ });
62
+
63
+ return {
64
+ id: workflowInstance.id,
65
+ stateMachine,
66
+ name: options.workflow,
67
+ };
68
+ }
69
+ }
@@ -0,0 +1,27 @@
1
+ import * as os from 'node:os';
2
+ import * as path from 'node:path';
3
+
4
+ import { IProjectConfig } from './config.types';
5
+
6
+ /**
7
+ * the location of the system-wide config directory
8
+ */
9
+ export const SYSTEM_CONFIG_DIR_PATH: string = path.resolve(
10
+ os.homedir(),
11
+ '.paragon',
12
+ );
13
+
14
+ /**
15
+ * default configuration for a project
16
+ */
17
+ export const DEFAULT_PROJECT_CONFIG: IProjectConfig = {
18
+ /**
19
+ * used to configure a different location for storing credential info
20
+ */
21
+ config_path: SYSTEM_CONFIG_DIR_PATH,
22
+ };
23
+
24
+ /**
25
+ * the name of the default profile
26
+ */
27
+ export const DEFAULT_PROFILE: string = 'default';
@@ -0,0 +1,112 @@
1
+ /**
2
+ * special files stored at the system level
3
+ */
4
+ export enum SystemFile {
5
+ /**
6
+ * stores credentials for interacting with Paragon
7
+ */
8
+ CREDENTIALS = 'CREDENTIALS',
9
+
10
+ /**
11
+ * stores information on different environments that can be interacted with
12
+ */
13
+ PROFILES = 'PROFILES',
14
+ }
15
+
16
+ /**
17
+ * stores the path to each system file
18
+ */
19
+ export const SystemFileToPathMap: Record<SystemFile, string> = {
20
+ [SystemFile.CREDENTIALS]: 'credentials.json',
21
+ [SystemFile.PROFILES]: 'profiles.json',
22
+ };
23
+
24
+ /**
25
+ * credentials for logging into an environment
26
+ */
27
+ export interface IAuth {
28
+ /**
29
+ * the username for logging in
30
+ */
31
+ username: string;
32
+
33
+ /**
34
+ * the password for logging in
35
+ */
36
+ password: string;
37
+ }
38
+
39
+ /**
40
+ * credentials for logging into an environment with the token and associated profile
41
+ */
42
+ export interface IProfileAuth extends IAuth {
43
+ /**
44
+ * the profile in which the authorization information is for
45
+ */
46
+ profile: string;
47
+
48
+ /**
49
+ * the auth token for authenticating requests to a profile
50
+ * saved by the cli after a successful login
51
+ */
52
+ token: string;
53
+ }
54
+
55
+ /**
56
+ * configuration for authentication to all the profiles on the machine
57
+ */
58
+ export interface IAuthConfig extends Record<string, IAuth> {}
59
+
60
+ /**
61
+ * APIs
62
+ */
63
+ export enum Service {
64
+ HERMES = 'hermes',
65
+ ZEUS = 'zeus',
66
+ }
67
+
68
+ /**
69
+ * configuration for an environment
70
+ */
71
+ export interface IProfile {
72
+ /**
73
+ * specifies that this profile is the default profile
74
+ */
75
+ default?: boolean;
76
+
77
+ /**
78
+ * the name of the profile
79
+ */
80
+ name: string;
81
+
82
+ /**
83
+ * environment variables
84
+ */
85
+ environment?: Record<string, string>;
86
+
87
+ /**
88
+ * urls to access services
89
+ */
90
+ services?: Record<Service, string>;
91
+ }
92
+
93
+ /**
94
+ * configuration for all the profiles configured on the machine
95
+ */
96
+ export interface IProfileConfig extends Record<string, IProfile> {}
97
+
98
+ /**
99
+ * configuration for the current project
100
+ */
101
+ export interface IProjectConfig {
102
+ /**
103
+ * used to configure a different location for storing credential info
104
+ * defaults to `<HOME_DIRECTORY>/.paragon`, e.g. `~/.paragon`
105
+ */
106
+ config_path?: string;
107
+
108
+ /**
109
+ * the profile to use
110
+ */
111
+ profile?: string;
112
+ }