@contrail/cli 1.0.0 → 1.0.5

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 (74) hide show
  1. package/.vscode/settings.json +5 -5
  2. package/bin/init +3 -3
  3. package/lib/{create-app → apps/create-app}/create.d.ts +1 -1
  4. package/lib/apps/create-app/create.js +59 -0
  5. package/lib/apps/create-app/setup-template.d.ts +1 -0
  6. package/lib/apps/create-app/setup-template.js +34 -0
  7. package/lib/{deploy-app → apps/deploy-app}/actions-deployer.d.ts +10 -10
  8. package/lib/{deploy-app → apps/deploy-app}/actions-deployer.js +97 -78
  9. package/lib/apps/deploy-app/app-deployer.d.ts +8 -0
  10. package/lib/{deploy-app → apps/deploy-app}/app-deployer.js +79 -58
  11. package/lib/apps/deploy-app/app-template-parsers.d.ts +7 -0
  12. package/lib/apps/deploy-app/app-template-parsers.js +37 -0
  13. package/lib/{deploy-app → apps/deploy-app}/deploy.d.ts +2 -2
  14. package/lib/apps/deploy-app/deploy.js +58 -0
  15. package/lib/apps/deploy-app/interfaces.d.ts +74 -0
  16. package/lib/apps/deploy-app/interfaces.js +4 -0
  17. package/lib/auth/auth.d.ts +9 -0
  18. package/lib/auth/auth.js +2 -0
  19. package/lib/auth/login.d.ts +2 -1
  20. package/lib/auth/login.js +113 -79
  21. package/lib/cli.d.ts +5 -5
  22. package/lib/cli.js +36 -30
  23. package/lib/common.d.ts +1 -0
  24. package/lib/common.js +5 -0
  25. package/lib/types/interfaces.d.ts +51 -0
  26. package/lib/{deploy-app → types}/interfaces.js +0 -0
  27. package/lib/types/option-set-uploader.d.ts +1 -0
  28. package/lib/types/option-set-uploader.js +75 -0
  29. package/lib/types/types-extractor.d.ts +1 -0
  30. package/lib/types/types-extractor.js +93 -0
  31. package/lib/types/types-uploader.d.ts +1 -0
  32. package/lib/types/types-uploader.js +171 -0
  33. package/package.json +63 -63
  34. package/publish.sh +5 -0
  35. package/src/apps/create-app/create.ts +73 -0
  36. package/src/apps/create-app/setup-template.ts +38 -0
  37. package/src/{deploy-app → apps/deploy-app}/actions-deployer.ts +122 -87
  38. package/src/{deploy-app → apps/deploy-app}/app-deployer.ts +86 -63
  39. package/src/apps/deploy-app/app-template-parsers.ts +43 -0
  40. package/src/apps/deploy-app/deploy.ts +67 -0
  41. package/src/apps/deploy-app/interfaces.ts +78 -0
  42. package/src/auth/auth.ts +10 -0
  43. package/src/auth/login.ts +127 -89
  44. package/src/cli.ts +38 -33
  45. package/src/common.ts +5 -0
  46. package/src/types/interfaces.ts +54 -0
  47. package/src/types/option-set-uploader.ts +83 -0
  48. package/src/types/types-extractor.ts +116 -0
  49. package/src/types/types-uploader.ts +198 -0
  50. package/templates/javascript/actions/example/action.js +37 -37
  51. package/templates/javascript/app.yml +54 -42
  52. package/templates/javascript/package.json +23 -23
  53. package/templates/typescript/actions/exampleAction.ts +38 -0
  54. package/templates/typescript/app.yml +56 -0
  55. package/templates/typescript/package.json +24 -0
  56. package/templates/typescript/tsconfig.json +31 -0
  57. package/tsconfig.json +30 -22
  58. package/tslint.json +57 -57
  59. package/lib/create-app/create.js +0 -48
  60. package/lib/create-app/setup-template.d.ts +0 -1
  61. package/lib/create-app/setup-template.js +0 -48
  62. package/lib/deploy-app/actions.d.ts +0 -5
  63. package/lib/deploy-app/actions.js +0 -17
  64. package/lib/deploy-app/app-declaration.d.ts +0 -20
  65. package/lib/deploy-app/app-declaration.js +0 -0
  66. package/lib/deploy-app/app-deployer.d.ts +0 -9
  67. package/lib/deploy-app/app.d.ts +0 -8
  68. package/lib/deploy-app/app.js +0 -56
  69. package/lib/deploy-app/deploy.js +0 -30
  70. package/lib/deploy-app/interfaces.d.ts +0 -34
  71. package/src/create-app/create.ts +0 -58
  72. package/src/create-app/setup-template.ts +0 -61
  73. package/src/deploy-app/deploy.ts +0 -34
  74. package/src/deploy-app/interfaces.ts +0 -35
@@ -0,0 +1,171 @@
1
+ import { Entities, Request } from '@contrail/sdk';
2
+ import { uploadOptionSets } from './option-set-uploader';
3
+ import chalk from 'chalk';
4
+ import fs from 'fs';
5
+ import { authUser } from '../auth/login';
6
+ import { getSeparatorByPlatform } from '../common';
7
+ const yaml = require('js-yaml');
8
+ export async function uploadTypes(args) {
9
+ try {
10
+ const userConfig = await authUser();
11
+ console.log(`%s Getting all types for orgId: ${userConfig.orgId}`, chalk.blue.bold('START'));
12
+ console.log(`%s Uploading`, chalk.yellow.bold('OPTION SETS'));
13
+ await uploadOptionSets();
14
+ console.log(`%s Uploaded`, chalk.yellow.bold('OPTION SETS'));
15
+ console.log(`%s Uploading`, chalk.yellow.bold('TYPES'));
16
+ await uploadReducedTypes();
17
+ console.log(`%s Uploaded`, chalk.yellow.bold('TYPES'));
18
+ }
19
+ catch (e) {
20
+ console.log(`%s Types Upload Failed. ${e}`, chalk.red.bold('FAIL'));
21
+ return;
22
+ }
23
+ console.log('%s Types Upload Complete.', chalk.green.bold('COMPLETE'));
24
+ }
25
+ function loadReducedTypes() {
26
+ const TARGET_DIR = process.cwd();
27
+ try {
28
+ const separator = getSeparatorByPlatform();
29
+ const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
30
+ return doc.types;
31
+ }
32
+ catch (e) {
33
+ throw new Error(`ERROR PARSING YAML: ${e}`);
34
+ }
35
+ }
36
+ async function uploadReducedTypes() {
37
+ const types = loadReducedTypes();
38
+ const currentTypesWithoutProperties = await new Entities().get({
39
+ entityName: 'type',
40
+ });
41
+ const currentTypes = [];
42
+ for (const currentTypeWithoutProperties of currentTypesWithoutProperties) {
43
+ const typeWithProperties = await new Entities().get({
44
+ entityName: 'type',
45
+ id: currentTypeWithoutProperties.id,
46
+ relations: ['typeProperties'],
47
+ });
48
+ currentTypes.push(typeWithProperties);
49
+ }
50
+ const currentTypesBySlug = {};
51
+ for (const currentType of currentTypes) {
52
+ currentTypesBySlug[currentType.slug] = currentType;
53
+ }
54
+ const newTypesBySlug = {};
55
+ for (const type of types) {
56
+ newTypesBySlug[type.slug] = type;
57
+ }
58
+ for (const type of types) {
59
+ try {
60
+ const found = currentTypesBySlug[type.slug];
61
+ if (!found) {
62
+ if (!type.typePath.includes(':')) {
63
+ console.log('Cannot create a base type. Skipping: ', type.slug);
64
+ continue;
65
+ }
66
+ console.log('Creating Type: ', type.slug);
67
+ const createdType = await new Entities().create({
68
+ entityName: 'type',
69
+ object: {
70
+ slug: type.slug,
71
+ label: type.label,
72
+ typePath: type.typePath,
73
+ isArchived: type.isArchived,
74
+ },
75
+ });
76
+ await uploadReducedTypeProperties(createdType, [], type.typeProperties);
77
+ }
78
+ else {
79
+ console.log('Updating Type: ', type.slug);
80
+ const updatedType = await new Entities().update({
81
+ entityName: 'type',
82
+ id: found.id,
83
+ object: {
84
+ slug: type.slug,
85
+ label: type.label,
86
+ typePath: type.typePath,
87
+ isArchived: type.isArchived,
88
+ },
89
+ });
90
+ await uploadReducedTypeProperties(updatedType, found.typeProperties, type.typeProperties);
91
+ }
92
+ }
93
+ catch (err) {
94
+ console.log('Error Uploading Types: ', err);
95
+ }
96
+ }
97
+ for (const currentType of currentTypes) {
98
+ try {
99
+ const found = newTypesBySlug[currentType.slug];
100
+ if (!found) {
101
+ console.log('Deleting Type: ', currentType.slug);
102
+ await new Entities().delete({
103
+ entityName: 'type',
104
+ id: currentType.id,
105
+ });
106
+ }
107
+ }
108
+ catch (err) {
109
+ throw new Error(`Error Deleting Types: ${err}`);
110
+ }
111
+ }
112
+ }
113
+ async function uploadReducedTypeProperties(type, currentPropertyTypes, newPropertyTypes) {
114
+ const currentPropertyTypeBySlug = {};
115
+ for (const currentPropertyType of currentPropertyTypes) {
116
+ currentPropertyTypeBySlug[currentPropertyType.slug] = currentPropertyType;
117
+ }
118
+ const newPropertyTypesBySlug = {};
119
+ for (const newPropertyType of newPropertyTypes) {
120
+ newPropertyTypesBySlug[newPropertyType.slug] = newPropertyType;
121
+ }
122
+ for (const newPropertyType of newPropertyTypes) {
123
+ try {
124
+ const found = currentPropertyTypeBySlug[newPropertyType.slug];
125
+ if (!found && !newPropertyType.inherited) {
126
+ console.log('Creating Type-Property: ', newPropertyType.slug);
127
+ await Request.request(`/types/${type.id}/type-properties`, {
128
+ method: 'POST',
129
+ body: JSON.stringify({
130
+ isArchived: newPropertyType.isArchived,
131
+ propertyType: newPropertyType.propertyType,
132
+ slug: newPropertyType.slug,
133
+ label: newPropertyType.label,
134
+ editable: newPropertyType.editable,
135
+ core: newPropertyType.core,
136
+ typePropertyOptionSetId: newPropertyType.typePropertyOptionSetId,
137
+ formula: newPropertyType.formula,
138
+ referencedTypeRootSlug: newPropertyType.referencedTypeRootSlug,
139
+ defaultValue: newPropertyType.defaultValue,
140
+ propertyLevel: newPropertyType.propertyLevel,
141
+ }),
142
+ headers: {
143
+ 'Accept': 'application/json',
144
+ 'Content-Type': 'application/json',
145
+ },
146
+ });
147
+ }
148
+ }
149
+ catch (err) {
150
+ console.log('Error Creating Properties: ', err);
151
+ }
152
+ }
153
+ for (const currentPropertyType of currentPropertyTypes) {
154
+ try {
155
+ const found = newPropertyTypesBySlug[currentPropertyType.slug];
156
+ if (!found && !currentPropertyType.inherited) {
157
+ console.log('Deleting Type-Property: ', currentPropertyType.slug);
158
+ await Request.request(`/types/${type.id}/type-properties/${currentPropertyType.id}`, {
159
+ method: 'DELETE',
160
+ headers: {
161
+ 'Accept': 'application/json',
162
+ 'Content-Type': 'application/json',
163
+ },
164
+ });
165
+ }
166
+ }
167
+ catch (err) {
168
+ throw new Error(`Error Deleting Properties: ${err}`);
169
+ }
170
+ }
171
+ }
package/package.json CHANGED
@@ -1,63 +1,63 @@
1
- {
2
- "name": "@contrail/cli",
3
- "version": "1.0.0",
4
- "description": "VibeIQ's Contrail Platform CLI.",
5
- "main": "lib/index.js",
6
- "bin": {
7
- "@contrail/contrail-cli": "bin/init",
8
- "contrail": "bin/init"
9
- },
10
- "publishConfig": {
11
- "access": "public"
12
- },
13
- "keywords": [
14
- "cli",
15
- "contrail-cli"
16
- ],
17
- "scripts": {
18
- "build": "tsc",
19
- "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
20
- "lint": "tslint -p tsconfig.json",
21
- "test": "jest --watch"
22
- },
23
- "author": "",
24
- "license": "ISC",
25
- "dependencies": {
26
- "@contrail/sdk": "^1.0.91",
27
- "amazon-cognito-identity-js": "^5.2.3",
28
- "arg": "^4.0.1",
29
- "aws-sdk": "^2.1044.0",
30
- "chalk": "^4.0.0",
31
- "esm": "^3.2.25",
32
- "execa": "^5.0.0",
33
- "inquirer": "^8.2.0",
34
- "js-yaml": "^4.1.0",
35
- "listr": "^0.14.3",
36
- "ncp": "^2.0.0",
37
- "pkg-install": "^1.0.0"
38
- },
39
- "devDependencies": {
40
- "@types/jest": "^23.3.14",
41
- "jest": "^23.6.0",
42
- "prettier": "^1.19.1",
43
- "ts-jest": "^23.10.5",
44
- "tslint": "^5.11.0",
45
- "tslint-config-prettier": "^1.18.0",
46
- "typescript": "^3.0.1",
47
- "@types/node": "^14.6.4"
48
- },
49
- "jest": {
50
- "moduleFileExtensions": [
51
- "js",
52
- "json",
53
- "ts"
54
- ],
55
- "rootDir": "src",
56
- "testRegex": ".spec.ts$",
57
- "transform": {
58
- "^.+\\.(t|j)s$": "ts-jest"
59
- },
60
- "coverageDirectory": "../coverage",
61
- "testEnvironment": "node"
62
- }
63
- }
1
+ {
2
+ "name": "@contrail/cli",
3
+ "version": "1.0.5",
4
+ "description": "VibeIQ's Contrail Platform CLI.",
5
+ "main": "lib/index.js",
6
+ "bin": {
7
+ "@contrail/contrail-cli": "bin/init",
8
+ "contrail": "bin/init"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "keywords": [
14
+ "cli",
15
+ "contrail-cli"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
20
+ "lint": "tslint -p tsconfig.json",
21
+ "test": "jest --watch"
22
+ },
23
+ "author": "",
24
+ "license": "ISC",
25
+ "dependencies": {
26
+ "@contrail/sdk": "^1.0.91",
27
+ "amazon-cognito-identity-js": "^5.2.3",
28
+ "arg": "^4.0.1",
29
+ "aws-sdk": "^2.1044.0",
30
+ "chalk": "^4.0.0",
31
+ "esm": "^3.2.25",
32
+ "execa": "^5.0.0",
33
+ "inquirer": "^8.2.0",
34
+ "js-yaml": "^4.1.0",
35
+ "listr": "^0.14.3",
36
+ "ncp": "^2.0.0",
37
+ "pkg-install": "^1.0.0"
38
+ },
39
+ "devDependencies": {
40
+ "@types/jest": "^23.3.14",
41
+ "jest": "^23.6.0",
42
+ "prettier": "^1.19.1",
43
+ "ts-jest": "^23.10.5",
44
+ "tslint": "^5.11.0",
45
+ "tslint-config-prettier": "^1.18.0",
46
+ "typescript": "^3.0.1",
47
+ "@types/node": "^14.6.4"
48
+ },
49
+ "jest": {
50
+ "moduleFileExtensions": [
51
+ "js",
52
+ "json",
53
+ "ts"
54
+ ],
55
+ "rootDir": "src",
56
+ "testRegex": ".spec.ts$",
57
+ "transform": {
58
+ "^.+\\.(t|j)s$": "ts-jest"
59
+ },
60
+ "coverageDirectory": "../coverage",
61
+ "testEnvironment": "node"
62
+ }
63
+ }
package/publish.sh ADDED
@@ -0,0 +1,5 @@
1
+ rm -rf lib;
2
+ npm install;
3
+ npm run build;
4
+ npm version patch;
5
+ npm publish;
@@ -0,0 +1,73 @@
1
+ import arg from 'arg';
2
+ import inquirer from 'inquirer';
3
+ import chalk from 'chalk';
4
+ import { setUpTemplate } from './setup-template';
5
+
6
+ function parseArgumentsIntoOptions(rawArgs) {
7
+ const args = arg(
8
+ {
9
+ '--app-name': String,
10
+ '--template': String,
11
+ '-n': '--app-name',
12
+ '-t': '--template',
13
+ },
14
+ {
15
+ argv: rawArgs.slice(3),
16
+ },
17
+ );
18
+ return {
19
+ appName: args['--app-name'],
20
+ template: args['--template'],
21
+ };
22
+ }
23
+ async function promptForMissingOptions(options) {
24
+ const defaultTemplate = 'JavaScript';
25
+
26
+ const questions = [];
27
+ if (!options.appName) {
28
+ questions.push({
29
+ type: 'input',
30
+ name: 'appName',
31
+ validate: (val) => !!val,
32
+ message: 'What is the name of your app?',
33
+ });
34
+ }
35
+
36
+ if (!options.template) {
37
+ questions.push({
38
+ type: 'list',
39
+ name: 'template',
40
+ message: 'Which project template would you like to use?',
41
+ choices: ['JavaScript', 'TypeScript'],
42
+ default: defaultTemplate,
43
+ });
44
+ }
45
+
46
+ const answers = await inquirer.prompt(questions);
47
+ return {
48
+ ...options,
49
+ appName: options.appName || answers.appName,
50
+ template: options.template || answers.template,
51
+ };
52
+ }
53
+
54
+ export async function create(args) {
55
+
56
+ let options = parseArgumentsIntoOptions(args);
57
+ options = await promptForMissingOptions(options);
58
+ console.log('%s Setting up new new contrail application.', chalk.blue.bold('START'));
59
+
60
+ let error;
61
+ try {
62
+ await setUpTemplate(options);
63
+ }
64
+ catch (e) {
65
+ error = e;
66
+ }
67
+
68
+ if (error) {
69
+ console.log('%s Application initialization failed.', chalk.red.bold('FAIL'));
70
+ } else {
71
+ console.log('%s Application initialized successfully.', chalk.green.bold('COMPLETE'));
72
+ }
73
+ }
@@ -0,0 +1,38 @@
1
+
2
+ import ncp from 'ncp';
3
+ import path from 'path';
4
+ import { promisify } from 'util';
5
+ const Listr = require('listr');
6
+ import { projectInstall } from 'pkg-install';
7
+ const copy = promisify(ncp);
8
+
9
+ const TARGET_DIR = process.cwd();
10
+ const currentFileUrl = import .meta.url;
11
+
12
+ function copyTemplate(templateDir){
13
+ return copy(templateDir, TARGET_DIR, {
14
+ clobber: false,
15
+ });
16
+ }
17
+
18
+ export async function setUpTemplate(options) {
19
+ const templateDir = path.resolve(new URL(currentFileUrl).pathname, `../../../../templates/${options.template.toLocaleLowerCase()}`).replace(/\\/g, '\\').replace(/(.):\\(.):/g, '').replace(/%20/g, ' ');
20
+ try {
21
+ const tasks = new Listr([
22
+ {
23
+ title: 'Copy template files',
24
+ task: () => copyTemplate(templateDir),
25
+ },
26
+ {
27
+ title: 'Install dependencies',
28
+ task: () =>
29
+ projectInstall({
30
+ cwd: TARGET_DIR,
31
+ }),
32
+ },
33
+ ]);
34
+ await tasks.run();
35
+ } catch (e) {
36
+ throw new Error(`Error copying template: ${e}`);
37
+ }
38
+ }
@@ -1,87 +1,122 @@
1
- import { Entities } from '@contrail/sdk';
2
- import chalk from 'chalk';
3
- import { App, AppDeclartion, AppActionDeclaration, EventWorkflowActionDefinition } from './interfaces';
4
- import fs from 'fs';
5
-
6
- export class ActionsDeployer {
7
- async deployActions(appDeclaration: AppDeclartion, app: App) {
8
-
9
- // FETCH ALL ACTIONS FROM THE API.
10
- const existingActions = await this.getActionsForApp(app);
11
- console.log('existingActions: ', existingActions);
12
- // UPSERT / DELETE BASED ON ACTION IDENTIFIER
13
- console.log('%s \tDeploying actions.', chalk.yellow.bold('ACTION'));
14
- for (const actionKey of Object.keys(appDeclaration.actions)) {
15
- await this.deployAction(actionKey, appDeclaration.actions[actionKey], app, existingActions);
16
- }
17
- }
18
-
19
- async getActionsForApp(app: App){
20
- return await new Entities().get({ entityName: 'event-workflow-action-definition',
21
- criteria: { ownerKey: `app:${app.id}`}});
22
- }
23
-
24
- async deployAction(actionKey: string,
25
- actionDeclaration: AppActionDeclaration,
26
- app: App,
27
- existingActions: Array<any>){
28
- console.log(`%s \tDeploying action: ${actionKey}`, chalk.yellow.bold('ACTION'));
29
- const identifier = this.getActionDeclarationIdentifier(actionKey, app);
30
- const existingDefinition = existingActions.find(ad => ad.identifier === identifier);
31
- if (!existingDefinition) {
32
- await this.createNewActionDefinition(actionKey, actionDeclaration, app);
33
- } else {
34
- await this.updateActionDefinition(existingDefinition, actionDeclaration);
35
- }
36
- }
37
- private async createNewActionDefinition(actionKey: string,
38
- actionDeclaration: AppActionDeclaration,
39
- app: App) {
40
- const newObject: EventWorkflowActionDefinition = {
41
- identifier: this.getActionDeclarationIdentifier(actionKey, app),
42
- name: actionDeclaration.name,
43
- ownerKey: `app:${app.id}`,
44
- public: !app.private,
45
- };
46
- const functionString = await this.getFunction(actionDeclaration);
47
- if (functionString) {
48
- newObject.executeFunctionString = functionString;
49
- }
50
- const actionDefinition = await new Entities().create({
51
- entityName: 'event-workflow-action-definition',
52
- object: newObject });
53
- console.log(`%s Created new action defintion: ${actionKey}`, chalk.yellow.bold('APP'));
54
- return actionDefinition;
55
- }
56
- private async updateActionDefinition(existingActionDefinition: EventWorkflowActionDefinition,
57
- actionDeclaration: AppActionDeclaration) {
58
- const updates: EventWorkflowActionDefinition = {
59
- name: actionDeclaration.name,
60
- };
61
- const functionString = await this.getFunction(actionDeclaration);
62
- if (functionString) {
63
- updates.executeFunctionString = functionString;
64
- }
65
- const actionDefinition = await new Entities().update({
66
- entityName: 'event-workflow-action-definition',
67
- id: existingActionDefinition.id,
68
- object: updates });
69
- console.log(`%s Updated existingaction defintion: ${actionDefinition.identifier}`, chalk.yellow.bold('APP'));
70
- return actionDefinition;
71
- }
72
- getActionDeclarationIdentifier(actionKey, app: App) {
73
- return app.identifier + ':' + actionKey;
74
- }
75
-
76
- async getFunction(actionDeclaration: AppActionDeclaration): Promise<string> {
77
- if (!actionDeclaration.handler) {
78
- return;
79
- }
80
- const CURRENT_DIR = process.cwd();
81
- const functionLocaiton = CURRENT_DIR + '\\' + actionDeclaration.handler;
82
- console.log('getFunction: ', functionLocaiton);
83
- const content = fs.readFileSync(functionLocaiton);
84
- console.log('content: ', content.toString());
85
- return content.toString();
86
- }
87
- }
1
+ import { Entities } from '@contrail/sdk';
2
+ import chalk from 'chalk';
3
+ import { App, AppDeclaration, AppActionDeclaration, EventWorkflowActionDefinition } from './interfaces';
4
+ import { ConfigPropertiesParser } from './app-template-parsers';
5
+ import { getSeparatorByPlatform } from '../../common';
6
+
7
+ import fs from 'fs';
8
+
9
+ export class ActionsDeployer {
10
+ async deployActions(appDeclaration: AppDeclaration, app: App) {
11
+
12
+ // FETCH ALL ACTIONS FROM THE API.
13
+ const existingActions = await this.getActionsForApp(app);
14
+
15
+ // UPSERT / DELETE BASED ON ACTION IDENTIFIER
16
+ console.log('%s \tDeploying actions.', chalk.yellow.bold('ACTION'));
17
+
18
+ for (const actionKey of Object.keys(appDeclaration.actions)) {
19
+ await this.deployAction(actionKey, appDeclaration.actions[actionKey], app, existingActions);
20
+ }
21
+ }
22
+
23
+ async getActionsForApp(app: App) {
24
+ return await new Entities().get({
25
+ entityName: 'event-workflow-action-definition',
26
+ criteria: { ownerKey: `app:${app.id}` },
27
+ });
28
+ }
29
+
30
+ async deployAction(actionKey: string, actionDeclaration: AppActionDeclaration, app: App, existingActions: Array<any>) {
31
+ console.log(`%s \tDeploying action: ${actionKey}`, chalk.yellow.bold('ACTION'));
32
+
33
+ const identifier = this.getActionDeclarationIdentifier(actionKey, app);
34
+ const existingDefinition = existingActions.find(ad => ad.identifier === identifier);
35
+
36
+ if (!existingDefinition) {
37
+ await this.createNewActionDefinition(actionKey, actionDeclaration, app);
38
+ } else {
39
+ await this.updateActionDefinition(existingDefinition, actionDeclaration);
40
+ }
41
+ }
42
+
43
+ private async createNewActionDefinition(actionKey: string, actionDeclaration: AppActionDeclaration, app: App) {
44
+ let configProperties;
45
+ if (actionDeclaration?.actionConfiguration) {
46
+ configProperties = ConfigPropertiesParser.getConfigProperties(actionDeclaration.actionConfiguration);
47
+ }
48
+ const newObject: EventWorkflowActionDefinition = {
49
+ identifier: this.getActionDeclarationIdentifier(actionKey, app),
50
+ name: actionDeclaration.name,
51
+ ownerKey: `app:${app.id}`,
52
+ public: app.public, // TODO - an actions visibility is not tied to the visibility of the app
53
+ configProperties,
54
+ };
55
+
56
+ const functionString = await this.getFunction(actionDeclaration);
57
+ if (functionString) {
58
+ newObject.executeFunctionString = functionString;
59
+ }
60
+
61
+ const actionDefinition = await new Entities().create({
62
+ entityName: 'event-workflow-action-definition',
63
+ object: newObject,
64
+ });
65
+
66
+ console.log(`%s Created new action definition: ${actionKey}`, chalk.yellow.bold('APP'));
67
+
68
+ return actionDefinition;
69
+ }
70
+
71
+ private async updateActionDefinition(existingActionDefinition: EventWorkflowActionDefinition, actionDeclaration: AppActionDeclaration) {
72
+
73
+ let configProperties;
74
+ if (actionDeclaration?.actionConfiguration) {
75
+ configProperties = ConfigPropertiesParser.getConfigProperties(actionDeclaration.actionConfiguration);
76
+ }
77
+ else if (!actionDeclaration?.actionConfiguration && existingActionDefinition.configProperties) {
78
+ configProperties = null;
79
+ }
80
+
81
+ const updates: EventWorkflowActionDefinition = {
82
+ name: actionDeclaration.name,
83
+ // public: actionDeclaration.public, // TODO - an actions visibility is allowed to change
84
+ configProperties,
85
+ };
86
+
87
+ const functionString = await this.getFunction(actionDeclaration);
88
+ if (functionString) {
89
+ updates.executeFunctionString = functionString;
90
+ }
91
+
92
+ const actionDefinition = await new Entities().update({
93
+ entityName: 'event-workflow-action-definition',
94
+ id: existingActionDefinition.id,
95
+ object: updates,
96
+ });
97
+
98
+ console.log(`%s Updated existingaction defintion: ${actionDefinition.identifier}`, chalk.yellow.bold('APP'));
99
+
100
+ return actionDefinition;
101
+ }
102
+
103
+ getActionDeclarationIdentifier(actionKey, app: App) {
104
+ return app.identifier + ':' + actionKey;
105
+ }
106
+
107
+ async getFunction(actionDeclaration: AppActionDeclaration): Promise<string> {
108
+ if (!actionDeclaration.handler) {
109
+ return;
110
+ }
111
+
112
+ if (!actionDeclaration.handler.endsWith('.js')) {
113
+ throw new Error('Action handler must be a .js file.');
114
+ }
115
+ const CURRENT_DIR = process.cwd();
116
+ const separator = getSeparatorByPlatform();
117
+
118
+ const functionLocation = CURRENT_DIR + separator + actionDeclaration.handler;
119
+ const content = fs.readFileSync(functionLocation);
120
+ return content.toString();
121
+ }
122
+ }