@contrail/cli 1.0.67 → 1.0.69

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 (170) hide show
  1. package/.eslintrc.js +53 -53
  2. package/.vscode/settings.json +5 -5
  3. package/README.md +5 -5
  4. package/bin/init +3 -3
  5. package/lib/api/apps.d.ts +13 -13
  6. package/lib/api/apps.js +85 -85
  7. package/lib/api/event-workflow-action-definition.d.ts +1 -1
  8. package/lib/api/event-workflow-action-definition.js +7 -7
  9. package/lib/api/event-workflow-template-definition.d.ts +6 -6
  10. package/lib/api/event-workflow-template-definition.js +20 -20
  11. package/lib/api/event-workflow-templates.d.ts +7 -7
  12. package/lib/api/event-workflow-templates.js +25 -25
  13. package/lib/apps/create-app/create.d.ts +1 -1
  14. package/lib/apps/create-app/create.js +60 -59
  15. package/lib/apps/create-app/setup-template.d.ts +1 -1
  16. package/lib/apps/create-app/setup-template.js +34 -34
  17. package/lib/apps/deploy-app/actions-deployer.d.ts +11 -11
  18. package/lib/apps/deploy-app/actions-deployer.js +114 -114
  19. package/lib/apps/deploy-app/app-deployer.d.ts +8 -8
  20. package/lib/apps/deploy-app/app-deployer.js +93 -93
  21. package/lib/apps/deploy-app/app-template-parsers.d.ts +7 -7
  22. package/lib/apps/deploy-app/app-template-parsers.js +38 -38
  23. package/lib/apps/deploy-app/deploy.d.ts +2 -2
  24. package/lib/apps/deploy-app/deploy.js +58 -58
  25. package/lib/apps/install-app/app-installer.d.ts +2 -2
  26. package/lib/apps/install-app/app-installer.js +120 -120
  27. package/lib/apps/upgrade-app/app-upgrader.d.ts +3 -3
  28. package/lib/apps/upgrade-app/app-upgrader.js +126 -126
  29. package/lib/auth/auth.d.ts +9 -9
  30. package/lib/auth/auth.js +2 -2
  31. package/lib/auth/login.d.ts +2 -2
  32. package/lib/auth/login.js +141 -141
  33. package/lib/cli.d.ts +5 -5
  34. package/lib/cli.js +524 -481
  35. package/lib/colors/color-loader.d.ts +2 -2
  36. package/lib/colors/color-loader.js +96 -96
  37. package/lib/common.d.ts +1 -1
  38. package/lib/common.js +5 -5
  39. package/lib/config/config.d.ts +6 -6
  40. package/lib/config/config.js +70 -70
  41. package/lib/config/configCommands.d.ts +3 -3
  42. package/lib/config/configCommands.js +34 -34
  43. package/lib/interfaces.d.ts +191 -191
  44. package/lib/interfaces.js +25 -25
  45. package/lib/loader-process/loader-process.d.ts +2 -2
  46. package/lib/loader-process/loader-process.js +120 -120
  47. package/lib/projects/interfaces.d.ts +77 -77
  48. package/lib/projects/interfaces.js +23 -23
  49. package/lib/projects/project-exporter.d.ts +2 -2
  50. package/lib/projects/project-exporter.js +187 -187
  51. package/lib/projects/project-uploader.d.ts +1 -1
  52. package/lib/projects/project-uploader.js +105 -105
  53. package/lib/types/allUploader.d.ts +1 -1
  54. package/lib/types/allUploader.js +24 -24
  55. package/lib/types/csv-parser.d.ts +1 -1
  56. package/lib/types/csv-parser.js +51 -51
  57. package/lib/types/interfaces.d.ts +55 -55
  58. package/lib/types/interfaces.js +1 -1
  59. package/lib/types/option-set-hierarchy-exporter.d.ts +1 -1
  60. package/lib/types/option-set-hierarchy-exporter.js +113 -113
  61. package/lib/types/option-set-hierarchy-parser.d.ts +2 -2
  62. package/lib/types/option-set-hierarchy-parser.js +140 -140
  63. package/lib/types/option-set-hierarchy-uploader.d.ts +1 -1
  64. package/lib/types/option-set-hierarchy-uploader.js +113 -113
  65. package/lib/types/option-set-merger.d.ts +1 -1
  66. package/lib/types/option-set-merger.js +105 -102
  67. package/lib/types/option-set-parser.d.ts +3 -1
  68. package/lib/types/option-set-parser.js +140 -50
  69. package/lib/types/option-set-uploader.d.ts +1 -1
  70. package/lib/types/option-set-uploader.js +98 -98
  71. package/lib/types/parse-types.d.ts +4 -4
  72. package/lib/types/parse-types.js +189 -189
  73. package/lib/types/rule-set-uploader.d.ts +1 -1
  74. package/lib/types/rule-set-uploader.js +172 -172
  75. package/lib/types/type-property-policy-uploader.d.ts +1 -1
  76. package/lib/types/type-property-policy-uploader.js +178 -178
  77. package/lib/types/types-extractor.d.ts +27 -27
  78. package/lib/types/types-extractor.js +318 -318
  79. package/lib/types/types-properties-merger.d.ts +1 -1
  80. package/lib/types/types-properties-merger.js +75 -75
  81. package/lib/types/types-uploader.d.ts +1 -1
  82. package/lib/types/types-uploader.js +239 -239
  83. package/lib/types/usage-info-uploader.d.ts +1 -1
  84. package/lib/types/usage-info-uploader.js +132 -132
  85. package/lib/users/user-exporter.d.ts +1 -0
  86. package/lib/users/user-exporter.js +41 -0
  87. package/lib/users/user-uploader.d.ts +1 -0
  88. package/lib/users/user-uploader.js +45 -0
  89. package/lib/workflow-template-definitions/workflow-template-definition-commands.d.ts +2 -2
  90. package/lib/workflow-template-definitions/workflow-template-definition-commands.js +10 -10
  91. package/lib/workflow-template-definitions/workflow-template-definition-deployer.d.ts +12 -12
  92. package/lib/workflow-template-definitions/workflow-template-definition-deployer.js +151 -151
  93. package/lib/workflow-templates/local-workflow-template-deployer.d.ts +11 -11
  94. package/lib/workflow-templates/local-workflow-template-deployer.js +102 -102
  95. package/lib/workflow-templates/workflow-template-commands.d.ts +4 -4
  96. package/lib/workflow-templates/workflow-template-commands.js +51 -51
  97. package/lib/workflow-templates/workflow-template-deployer.d.ts +4 -4
  98. package/lib/workflow-templates/workflow-template-deployer.js +26 -26
  99. package/package.json +72 -72
  100. package/publish.bat +5 -0
  101. package/publish.sh +4 -4
  102. package/src/api/apps.ts +96 -96
  103. package/src/api/event-workflow-action-definition.ts +7 -7
  104. package/src/api/event-workflow-template-definition.ts +22 -22
  105. package/src/api/event-workflow-templates.ts +28 -28
  106. package/src/apps/create-app/create.ts +74 -73
  107. package/src/apps/create-app/setup-template.ts +37 -37
  108. package/src/apps/deploy-app/actions-deployer.ts +140 -140
  109. package/src/apps/deploy-app/app-deployer.ts +105 -105
  110. package/src/apps/deploy-app/app-template-parsers.ts +43 -43
  111. package/src/apps/deploy-app/deploy.ts +65 -65
  112. package/src/apps/install-app/app-installer.ts +125 -125
  113. package/src/apps/upgrade-app/app-upgrader.ts +131 -131
  114. package/src/auth/auth.ts +9 -9
  115. package/src/auth/login.ts +155 -155
  116. package/src/cli.ts +540 -496
  117. package/src/colors/color-loader.ts +112 -112
  118. package/src/colors/colors.csv +866 -866
  119. package/src/common.ts +4 -4
  120. package/src/config/config.ts +74 -74
  121. package/src/config/configCommands.ts +37 -37
  122. package/src/interfaces.ts +200 -200
  123. package/src/loader-process/loader-process.ts +151 -151
  124. package/src/projects/interfaces.ts +72 -72
  125. package/src/projects/project-exporter.ts +224 -224
  126. package/src/projects/project-uploader.ts +130 -130
  127. package/src/types/allUploader.ts +28 -28
  128. package/src/types/csv-parser.ts +58 -58
  129. package/src/types/interfaces.ts +58 -58
  130. package/src/types/option-set-hierarchy-exporter.ts +124 -124
  131. package/src/types/option-set-hierarchy-parser.ts +168 -168
  132. package/src/types/option-set-hierarchy-uploader.ts +121 -121
  133. package/src/types/option-set-merger.ts +123 -122
  134. package/src/types/option-set-parser.ts +165 -59
  135. package/src/types/option-set-uploader.ts +107 -107
  136. package/src/types/parse-types.ts +226 -226
  137. package/src/types/rule-set-uploader.ts +186 -186
  138. package/src/types/type-property-policy-uploader.ts +191 -191
  139. package/src/types/types-extractor.ts +368 -368
  140. package/src/types/types-properties-merger.ts +89 -89
  141. package/src/types/types-uploader.ts +271 -271
  142. package/src/types/usage-info-uploader.ts +138 -138
  143. package/src/users/user-exporter.ts +52 -0
  144. package/src/users/user-uploader.ts +58 -0
  145. package/src/workflow-template-definitions/workflow-template-definition-commands.ts +12 -12
  146. package/src/workflow-template-definitions/workflow-template-definition-deployer.ts +184 -184
  147. package/src/workflow-templates/local-workflow-template-deployer.ts +125 -125
  148. package/src/workflow-templates/workflow-template-commands.ts +59 -59
  149. package/src/workflow-templates/workflow-template-deployer.ts +29 -29
  150. package/templates/docker_deploy.bat +1 -0
  151. package/templates/javascript/actions/example/action.js +37 -37
  152. package/templates/javascript/app.yml +52 -52
  153. package/templates/javascript/package.json +23 -23
  154. package/templates/typescript/Dockerfile +15 -14
  155. package/templates/typescript/README.md +10 -10
  156. package/templates/typescript/app.yml +50 -54
  157. package/templates/typescript/package-lock.json +14 -14
  158. package/templates/typescript/package.json +54 -52
  159. package/templates/typescript/src/actions/logItem.ts +26 -0
  160. package/templates/typescript/src/framework/action-dispatcher.ts +86 -0
  161. package/templates/typescript/src/framework/app-config.ts +27 -0
  162. package/templates/typescript/src/framework/handler.ts +11 -0
  163. package/templates/typescript/src/framework/index.ts +3 -0
  164. package/templates/typescript/src/framework/interfaces.ts +32 -0
  165. package/templates/typescript/src/framework/logger.ts +21 -0
  166. package/templates/typescript/{actions → src/framework}/mock-server.ts +7 -0
  167. package/templates/typescript/tsconfig.json +28 -27
  168. package/tsconfig.json +31 -30
  169. package/templates/typescript/actions/handler.ts +0 -62
  170. package/templates/typescript/actions/utils/logger.ts +0 -15
@@ -1,369 +1,369 @@
1
- import { Entities } from '@contrail/sdk';
2
- import { ReducedType, ReducedTypeProperty } from './interfaces';
3
- import chalk from 'chalk';
4
- import fs from 'fs';
5
- import { authUser } from '../auth/login';
6
- import * as path from 'path';
7
- import pLimit from 'p-limit';
8
- const limit = pLimit(30);
9
-
10
- const yaml = require('js-yaml');
11
- interface StoreFunction
12
- {
13
- (data: ReducedDataHolder, dir: string): void;
14
- }
15
-
16
- export interface DataHolder {
17
- types: any[],
18
- optionSets: any[],
19
- optionSetHierarchies: any[],
20
- ruleSets: any[],
21
- ruleSetLinks: any[],
22
- usageInfo: any[],
23
- typePropertyPolicies: any[],
24
- typePropertyPolicyLinks: any[],
25
- }
26
-
27
- export interface ReducedDataHolder {
28
- reducedTypes: any[],
29
- reducedOptionSets: any[],
30
- reducedOptionSetHierarchies: any[],
31
- reducedRuleSets: any[],
32
- reducedRuleSetLinks: any[],
33
- reducedUsageInfo: any[],
34
- reducedTypePropertyPolicies: any[],
35
- reducedTypePropertyPolicyLinks: any[],
36
- }
37
-
38
- export async function getAll(args) {
39
- try {
40
- const userConfig = await authUser();
41
- console.log(`%s Getting all types for orgId: ${userConfig.orgId}`, chalk.blue.bold('START'));
42
-
43
- // make directory to house exports
44
- const dir = `./contrail-export-${Date.now()}`;
45
- fs.mkdirSync(dir);
46
-
47
- const data: DataHolder = {
48
- types: await getTypes(),
49
- optionSets: await getOptionSets(),
50
- optionSetHierarchies: await getOptionSetHierarchies(),
51
- ruleSets: await getRuleSets(),
52
- ruleSetLinks: await getRuleSetLinks(),
53
- usageInfo: await getUsageInfo(),
54
- typePropertyPolicies: await getTypePropertyPolicies(),
55
- typePropertyPolicyLinks: await getTypePropertyPolicyLinks(),
56
- };
57
- const reducedData = reduceData(data);
58
- saveData(reducedData, dir);
59
-
60
- console.log('%s Retrieved', chalk.yellow.bold('TYPES'));
61
- }
62
- catch (e) {
63
- console.log(`%s Types Retrieval Failed. ${e}`, chalk.red.bold('FAIL'));
64
- return;
65
- }
66
-
67
- console.log('%s Types Retrieval Complete.', chalk.green.bold('COMPLETE'));
68
- }
69
-
70
- /**
71
- * stores reduced option sets and returns full option sets */
72
- export async function getOptionSets() {
73
- console.log('%s Retrieving', chalk.yellow.bold('OPTION SETS'));
74
- const optionSets = await new Entities().get({
75
- entityName: 'type-property-option-set',
76
- });
77
- console.log('%s Retrieved', chalk.yellow.bold('OPTION SETS'));
78
- return optionSets;
79
- }
80
-
81
- export async function getTypes() {
82
- console.log('%s Retrieving', chalk.yellow.bold('TYPES'));
83
- const entitiesClient = new Entities();
84
- const types = await entitiesClient.get({
85
- entityName: 'type',
86
- });
87
- console.log(`${chalk.yellow.bold('TYPES')} Found ${types.length} types`);
88
- const typesWithProperties = [];
89
- const promises = [];
90
- for (const type of types) {
91
- const promise = limit(async () => {
92
- const t = await entitiesClient.get({
93
- entityName: 'type',
94
- id: type.id,
95
- relations: ['typeProperties'],
96
- });
97
- typesWithProperties.push(t);
98
- });
99
- promises.push(promise);
100
- }
101
- await Promise.all(promises);
102
- return typesWithProperties;
103
- }
104
-
105
- async function getOptionSetHierarchies() {
106
- const entitiesClient = new Entities();
107
- return await entitiesClient.get({
108
- entityName: 'option-set-hierarchy',
109
- });
110
- }
111
-
112
- async function getRuleSets() {
113
- const entitiesClient = new Entities();
114
- return await entitiesClient.get({
115
- entityName: 'type-rule-set',
116
- });
117
- }
118
-
119
- async function getRuleSetLinks() {
120
- const entitiesClient = new Entities();
121
- return await entitiesClient.get({
122
- entityName: 'type-rule-set-link',
123
- });
124
- }
125
-
126
- async function getUsageInfo() {
127
- const entitiesClient = new Entities();
128
- return await entitiesClient.get({
129
- entityName: 'type-property-usage-info',
130
- });
131
- }
132
-
133
- async function getTypePropertyPolicies() {
134
- const entitiesClient = new Entities();
135
- return await entitiesClient.get({
136
- entityName: 'type-property-policy',
137
- });
138
- }
139
- async function getTypePropertyPolicyLinks() {
140
- const entitiesClient = new Entities();
141
- return await entitiesClient.get({
142
- entityName: 'type-property-policy-link',
143
- });
144
- }
145
-
146
- function reduceTypePropertyPolicies(data: DataHolder) {
147
- return data?.typePropertyPolicies?.map(x => {
148
- return {
149
- label: x.label,
150
- policy: {
151
- version: x.policy.version,
152
- statements: x.policy.statements,
153
- }
154
- };
155
- });
156
- }
157
-
158
- function reduceTypePropertyPolicyLinks(data: DataHolder) {
159
- const typeProperties = data?.types?.map(x => x.typeProperties).reduce((x, y) => x.concat(y));
160
- return data?.typePropertyPolicyLinks?.map(x => {
161
- return {
162
- typePropertySlug: typeProperties.find(y => y.id === x.typePropertyId)?.slug,
163
- typePropertyPolicyLabel: data.typePropertyPolicies.find(y => y.id === x.typePropertyPolicyId)?.label,
164
- };
165
- });
166
- }
167
-
168
- function reduceUsageInfo(data: DataHolder) {
169
- const typeProperties = data?.types?.map(x => x.typeProperties).reduce((x, y) => x.concat(y));
170
- return data?.usageInfo?.map(x => {
171
- return {
172
- used: x.used,
173
- editable: x.editable,
174
- formulaFunction: x.formulaFunction,
175
- validationFunction: x.validationFunction,
176
- numberFormat: x.numberFormat,
177
- defaultValue: x.defaultValue,
178
- label: x.label,
179
- typeSlug: data.types.find(y => y.id === x.typeId)?.slug,
180
- typePropertySlug: typeProperties.find(y => y.id === x.typePropertyId)?.slug,
181
- };
182
- });
183
- }
184
-
185
- function reduceRuleSetLinks(data: DataHolder) {
186
- const reducedRuleSetLinks = [];
187
- if (!data?.ruleSetLinks.length) {
188
- return [];
189
- }
190
- for (const ruleSetLink of data.ruleSetLinks) {
191
- const ruleSet = data?.ruleSets?.find(x => x.id === ruleSetLink.typeRuleSetId);
192
- const type = data?.types?.find(x => x.id === ruleSetLink.typeId);
193
-
194
- if (!ruleSet || !type) {
195
- console.log('%s Skipping bad rule set link', ruleSetLink.id, chalk.red.bold('TYPES'));
196
- }
197
- reducedRuleSetLinks.push({
198
- ruleSet: ruleSet.name,
199
- type: type.slug
200
- });
201
- }
202
- return reducedRuleSetLinks;
203
- }
204
-
205
- function reduceRuleSets(data: DataHolder) {
206
- return data?.ruleSets?.map(x => {
207
- return {
208
- ruleSet: x.ruleSet,
209
- name: x.name,
210
- };
211
- });
212
- }
213
-
214
- function reduceOptionSets(data: DataHolder) {
215
- return data.optionSets?.map(optionSet => {
216
- return {
217
- name: optionSet.name,
218
- isArchived: optionSet.isArchived,
219
- optionSet: optionSet.optionSet,
220
- };
221
- });
222
- }
223
-
224
- function reduceOptionSetHierarchies(data: DataHolder) {
225
- return data.optionSetHierarchies?.map(x => {
226
- const hierarchyByName = x.hierarchy.map(
227
- optionSetId => data.optionSets?.find(x => x.id === optionSetId)?.name
228
- );
229
- return {
230
- hierarchy: hierarchyByName,
231
- options: x.options,
232
- name: x.name
233
- };
234
- });
235
- }
236
-
237
- export function reduceTypes(dataHolder: DataHolder) {
238
- console.log('Found types: ', dataHolder.types.length);
239
-
240
- let reducedTypes = [];
241
- const optionSetNameById = {};
242
- for (const optionSet of dataHolder.optionSets) {
243
- optionSetNameById[optionSet.id] = optionSet.name;
244
- }
245
- for (const type of dataHolder.types) {
246
- const reducedTypeProperties = [];
247
- for (const typeProperty of type.typeProperties) {
248
- const reducedTypeProperty: ReducedTypeProperty = {
249
- slug: typeProperty.slug,
250
- id: typeProperty.id,
251
- isArchived: typeProperty.isArchived,
252
- propertyType: typeProperty.propertyType,
253
- label: typeProperty.label,
254
- editable: typeProperty.editable,
255
- searchable: typeProperty.searchable,
256
- core: typeProperty.core,
257
- typePropertyOptionSetName: optionSetNameById[typeProperty.typePropertyOptionSetId],
258
- formula: typeProperty.formula,
259
- referencedTypeRootSlug: typeProperty.referencedTypeRootSlug,
260
- defaultValue: typeProperty.defaultValue,
261
- propertyLevel: typeProperty.propertyLevel,
262
- inherited: typeProperty.inherited,
263
- formulaFunction: typeProperty.formulaFunction,
264
- validationFunction: typeProperty.validationFunction,
265
- numberFormat: typeProperty.numberFormat,
266
- };
267
-
268
- reducedTypeProperties.push(reducedTypeProperty);
269
- }
270
-
271
- const reducedType: ReducedType = {
272
- typePath: type.typePath,
273
- slug: type.slug,
274
- label: type.label,
275
- isArchived: type.isArchived,
276
- typeProperties: reducedTypeProperties,
277
- };
278
-
279
- reducedTypes.push(reducedType);
280
- }
281
-
282
- reducedTypes = reducedTypes.sort((one, two) => (one.typePath < two.typePath ? -1 : 1));
283
- return reducedTypes;
284
- }
285
-
286
- export function reduceData(data: DataHolder): ReducedDataHolder {
287
- return {
288
- reducedTypes: reduceTypes(data),
289
- reducedOptionSets: reduceOptionSets(data),
290
- reducedOptionSetHierarchies: reduceOptionSetHierarchies(data),
291
- reducedRuleSets: reduceRuleSets(data),
292
- reducedRuleSetLinks: reduceRuleSetLinks(data),
293
- reducedUsageInfo: reduceUsageInfo(data),
294
- reducedTypePropertyPolicies: reduceTypePropertyPolicies(data),
295
- reducedTypePropertyPolicyLinks: reduceTypePropertyPolicyLinks(data),
296
- };
297
- }
298
-
299
- const saveTypesFunction: StoreFunction = function (data: ReducedDataHolder, dir: string) {
300
- const roots = {};
301
- for(const type of data.reducedTypes) {
302
- const typePathParts = type.typePath.split(':');
303
- if(!typePathParts) {
304
- console.warn('skipping bad type with no path', type);
305
- }
306
- const root = typePathParts[0];
307
- if(roots[root]) {
308
- roots[root].push(type);
309
- } else {
310
- roots[root] = [type];
311
- }
312
- }
313
- for(const [root, types] of Object.entries(roots)) {
314
- const CURRENT_DIR = process.cwd();
315
- const subDir = path.join(CURRENT_DIR, dir, 'types');
316
- if(!fs.existsSync(subDir)) {
317
- fs.mkdirSync(subDir);
318
- }
319
- const yamlStr = yaml.dump(types);
320
- const fileLabel = root;
321
- if(!fileLabel) {
322
- console.warn('skipping bad type root:', root);
323
- }
324
- fs.writeFileSync(path.join(subDir, `${removeSlash(fileLabel)}.yml`), yamlStr, 'utf8');
325
- }
326
-
327
- };
328
-
329
- function saveData(reducedData, dir: string) {
330
-
331
- const propertyToFolderMap = {
332
- reducedTypes: { filename: 'types', split: false, fileLabel: 'slug', saveFunction: saveTypesFunction},
333
- reducedOptionSets: { filename: 'option-sets', split: true, fileLabel: 'name' },
334
- reducedOptionSetHierarchies: { filename: 'option-set-hierarchies', split: true, fileLabel: 'name' },
335
- reducedRuleSets: { filename: 'rule-sets', split: true, fileLabel: 'name' },
336
- reducedRuleSetLinks: { filename: 'rule-set-links', split: false, fileLabel: '' },
337
- reducedUsageInfo: { filename: 'usage-info', split: false, fileLabel: '' },
338
- reducedTypePropertyPolicies: { filename: 'type-property-policies', split: true, fileLabel: 'label' },
339
- reducedTypePropertyPolicyLinks: { filename: 'type-property-policy-links', split: false, fileLabel: '' },
340
- };
341
- for(const [key, folder] of Object.entries(propertyToFolderMap)) {
342
- const CURRENT_DIR = process.cwd();
343
- if(folder['saveFunction']) {
344
- folder['saveFunction'](reducedData, dir);
345
- } else if(folder.split) {
346
- const subDir = path.join(CURRENT_DIR, dir, folder.filename);
347
- if(!fs.existsSync(subDir)) {
348
- fs.mkdirSync(subDir);
349
- }
350
- for(const item of reducedData[key]) {
351
- const yamlStr = yaml.dump(item);
352
- const fileLabel = item[folder.fileLabel];
353
- if(!fileLabel) {
354
- console.warn('skipping bad data:', item);
355
- }
356
- fs.writeFileSync(path.join(subDir, `${removeSlash(fileLabel)}.yml`), yamlStr, 'utf8');
357
- }
358
- } else {
359
- const yamlStr = yaml.dump(reducedData[key]);
360
- fs.writeFileSync(path.join(CURRENT_DIR, dir, `${folder.filename}.yml`), yamlStr, 'utf8');
361
- }
362
-
363
- }
364
- }
365
-
366
- function removeSlash(string) {
367
- const re = /\//gi;
368
- return string.replace(re, '-');
1
+ import { Entities } from '@contrail/sdk';
2
+ import { ReducedType, ReducedTypeProperty } from './interfaces';
3
+ import chalk from 'chalk';
4
+ import fs from 'fs';
5
+ import { authUser } from '../auth/login';
6
+ import * as path from 'path';
7
+ import pLimit from 'p-limit';
8
+ const limit = pLimit(30);
9
+
10
+ const yaml = require('js-yaml');
11
+ interface StoreFunction
12
+ {
13
+ (data: ReducedDataHolder, dir: string): void;
14
+ }
15
+
16
+ export interface DataHolder {
17
+ types: any[],
18
+ optionSets: any[],
19
+ optionSetHierarchies: any[],
20
+ ruleSets: any[],
21
+ ruleSetLinks: any[],
22
+ usageInfo: any[],
23
+ typePropertyPolicies: any[],
24
+ typePropertyPolicyLinks: any[],
25
+ }
26
+
27
+ export interface ReducedDataHolder {
28
+ reducedTypes: any[],
29
+ reducedOptionSets: any[],
30
+ reducedOptionSetHierarchies: any[],
31
+ reducedRuleSets: any[],
32
+ reducedRuleSetLinks: any[],
33
+ reducedUsageInfo: any[],
34
+ reducedTypePropertyPolicies: any[],
35
+ reducedTypePropertyPolicyLinks: any[],
36
+ }
37
+
38
+ export async function getAll(args) {
39
+ try {
40
+ const userConfig = await authUser();
41
+ console.log(`%s Getting all types for orgId: ${userConfig.orgId}`, chalk.blue.bold('START'));
42
+
43
+ // make directory to house exports
44
+ const dir = `./contrail-export-${Date.now()}`;
45
+ fs.mkdirSync(dir);
46
+
47
+ const data: DataHolder = {
48
+ types: await getTypes(),
49
+ optionSets: await getOptionSets(),
50
+ optionSetHierarchies: await getOptionSetHierarchies(),
51
+ ruleSets: await getRuleSets(),
52
+ ruleSetLinks: await getRuleSetLinks(),
53
+ usageInfo: await getUsageInfo(),
54
+ typePropertyPolicies: await getTypePropertyPolicies(),
55
+ typePropertyPolicyLinks: await getTypePropertyPolicyLinks(),
56
+ };
57
+ const reducedData = reduceData(data);
58
+ saveData(reducedData, dir);
59
+
60
+ console.log('%s Retrieved', chalk.yellow.bold('TYPES'));
61
+ }
62
+ catch (e) {
63
+ console.log(`%s Types Retrieval Failed. ${e}`, chalk.red.bold('FAIL'));
64
+ return;
65
+ }
66
+
67
+ console.log('%s Types Retrieval Complete.', chalk.green.bold('COMPLETE'));
68
+ }
69
+
70
+ /**
71
+ * stores reduced option sets and returns full option sets */
72
+ export async function getOptionSets() {
73
+ console.log('%s Retrieving', chalk.yellow.bold('OPTION SETS'));
74
+ const optionSets = await new Entities().get({
75
+ entityName: 'type-property-option-set',
76
+ });
77
+ console.log('%s Retrieved', chalk.yellow.bold('OPTION SETS'));
78
+ return optionSets;
79
+ }
80
+
81
+ export async function getTypes() {
82
+ console.log('%s Retrieving', chalk.yellow.bold('TYPES'));
83
+ const entitiesClient = new Entities();
84
+ const types = await entitiesClient.get({
85
+ entityName: 'type',
86
+ });
87
+ console.log(`${chalk.yellow.bold('TYPES')} Found ${types.length} types`);
88
+ const typesWithProperties = [];
89
+ const promises = [];
90
+ for (const type of types) {
91
+ const promise = limit(async () => {
92
+ const t = await entitiesClient.get({
93
+ entityName: 'type',
94
+ id: type.id,
95
+ relations: ['typeProperties'],
96
+ });
97
+ typesWithProperties.push(t);
98
+ });
99
+ promises.push(promise);
100
+ }
101
+ await Promise.all(promises);
102
+ return typesWithProperties;
103
+ }
104
+
105
+ async function getOptionSetHierarchies() {
106
+ const entitiesClient = new Entities();
107
+ return await entitiesClient.get({
108
+ entityName: 'option-set-hierarchy',
109
+ });
110
+ }
111
+
112
+ async function getRuleSets() {
113
+ const entitiesClient = new Entities();
114
+ return await entitiesClient.get({
115
+ entityName: 'type-rule-set',
116
+ });
117
+ }
118
+
119
+ async function getRuleSetLinks() {
120
+ const entitiesClient = new Entities();
121
+ return await entitiesClient.get({
122
+ entityName: 'type-rule-set-link',
123
+ });
124
+ }
125
+
126
+ async function getUsageInfo() {
127
+ const entitiesClient = new Entities();
128
+ return await entitiesClient.get({
129
+ entityName: 'type-property-usage-info',
130
+ });
131
+ }
132
+
133
+ async function getTypePropertyPolicies() {
134
+ const entitiesClient = new Entities();
135
+ return await entitiesClient.get({
136
+ entityName: 'type-property-policy',
137
+ });
138
+ }
139
+ async function getTypePropertyPolicyLinks() {
140
+ const entitiesClient = new Entities();
141
+ return await entitiesClient.get({
142
+ entityName: 'type-property-policy-link',
143
+ });
144
+ }
145
+
146
+ function reduceTypePropertyPolicies(data: DataHolder) {
147
+ return data?.typePropertyPolicies?.map(x => {
148
+ return {
149
+ label: x.label,
150
+ policy: {
151
+ version: x.policy.version,
152
+ statements: x.policy.statements,
153
+ }
154
+ };
155
+ });
156
+ }
157
+
158
+ function reduceTypePropertyPolicyLinks(data: DataHolder) {
159
+ const typeProperties = data?.types?.map(x => x.typeProperties).reduce((x, y) => x.concat(y));
160
+ return data?.typePropertyPolicyLinks?.map(x => {
161
+ return {
162
+ typePropertySlug: typeProperties.find(y => y.id === x.typePropertyId)?.slug,
163
+ typePropertyPolicyLabel: data.typePropertyPolicies.find(y => y.id === x.typePropertyPolicyId)?.label,
164
+ };
165
+ });
166
+ }
167
+
168
+ function reduceUsageInfo(data: DataHolder) {
169
+ const typeProperties = data?.types?.map(x => x.typeProperties).reduce((x, y) => x.concat(y));
170
+ return data?.usageInfo?.map(x => {
171
+ return {
172
+ used: x.used,
173
+ editable: x.editable,
174
+ formulaFunction: x.formulaFunction,
175
+ validationFunction: x.validationFunction,
176
+ numberFormat: x.numberFormat,
177
+ defaultValue: x.defaultValue,
178
+ label: x.label,
179
+ typeSlug: data.types.find(y => y.id === x.typeId)?.slug,
180
+ typePropertySlug: typeProperties.find(y => y.id === x.typePropertyId)?.slug,
181
+ };
182
+ });
183
+ }
184
+
185
+ function reduceRuleSetLinks(data: DataHolder) {
186
+ const reducedRuleSetLinks = [];
187
+ if (!data?.ruleSetLinks.length) {
188
+ return [];
189
+ }
190
+ for (const ruleSetLink of data.ruleSetLinks) {
191
+ const ruleSet = data?.ruleSets?.find(x => x.id === ruleSetLink.typeRuleSetId);
192
+ const type = data?.types?.find(x => x.id === ruleSetLink.typeId);
193
+
194
+ if (!ruleSet || !type) {
195
+ console.log('%s Skipping bad rule set link', ruleSetLink.id, chalk.red.bold('TYPES'));
196
+ }
197
+ reducedRuleSetLinks.push({
198
+ ruleSet: ruleSet.name,
199
+ type: type.slug
200
+ });
201
+ }
202
+ return reducedRuleSetLinks;
203
+ }
204
+
205
+ function reduceRuleSets(data: DataHolder) {
206
+ return data?.ruleSets?.map(x => {
207
+ return {
208
+ ruleSet: x.ruleSet,
209
+ name: x.name,
210
+ };
211
+ });
212
+ }
213
+
214
+ function reduceOptionSets(data: DataHolder) {
215
+ return data.optionSets?.map(optionSet => {
216
+ return {
217
+ name: optionSet.name,
218
+ isArchived: optionSet.isArchived,
219
+ optionSet: optionSet.optionSet,
220
+ };
221
+ });
222
+ }
223
+
224
+ function reduceOptionSetHierarchies(data: DataHolder) {
225
+ return data.optionSetHierarchies?.map(x => {
226
+ const hierarchyByName = x.hierarchy.map(
227
+ optionSetId => data.optionSets?.find(x => x.id === optionSetId)?.name
228
+ );
229
+ return {
230
+ hierarchy: hierarchyByName,
231
+ options: x.options,
232
+ name: x.name
233
+ };
234
+ });
235
+ }
236
+
237
+ export function reduceTypes(dataHolder: DataHolder) {
238
+ console.log('Found types: ', dataHolder.types.length);
239
+
240
+ let reducedTypes = [];
241
+ const optionSetNameById = {};
242
+ for (const optionSet of dataHolder.optionSets) {
243
+ optionSetNameById[optionSet.id] = optionSet.name;
244
+ }
245
+ for (const type of dataHolder.types) {
246
+ const reducedTypeProperties = [];
247
+ for (const typeProperty of type.typeProperties) {
248
+ const reducedTypeProperty: ReducedTypeProperty = {
249
+ slug: typeProperty.slug,
250
+ id: typeProperty.id,
251
+ isArchived: typeProperty.isArchived,
252
+ propertyType: typeProperty.propertyType,
253
+ label: typeProperty.label,
254
+ editable: typeProperty.editable,
255
+ searchable: typeProperty.searchable,
256
+ core: typeProperty.core,
257
+ typePropertyOptionSetName: optionSetNameById[typeProperty.typePropertyOptionSetId],
258
+ formula: typeProperty.formula,
259
+ referencedTypeRootSlug: typeProperty.referencedTypeRootSlug,
260
+ defaultValue: typeProperty.defaultValue,
261
+ propertyLevel: typeProperty.propertyLevel,
262
+ inherited: typeProperty.inherited,
263
+ formulaFunction: typeProperty.formulaFunction,
264
+ validationFunction: typeProperty.validationFunction,
265
+ numberFormat: typeProperty.numberFormat,
266
+ };
267
+
268
+ reducedTypeProperties.push(reducedTypeProperty);
269
+ }
270
+
271
+ const reducedType: ReducedType = {
272
+ typePath: type.typePath,
273
+ slug: type.slug,
274
+ label: type.label,
275
+ isArchived: type.isArchived,
276
+ typeProperties: reducedTypeProperties,
277
+ };
278
+
279
+ reducedTypes.push(reducedType);
280
+ }
281
+
282
+ reducedTypes = reducedTypes.sort((one, two) => (one.typePath < two.typePath ? -1 : 1));
283
+ return reducedTypes;
284
+ }
285
+
286
+ export function reduceData(data: DataHolder): ReducedDataHolder {
287
+ return {
288
+ reducedTypes: reduceTypes(data),
289
+ reducedOptionSets: reduceOptionSets(data),
290
+ reducedOptionSetHierarchies: reduceOptionSetHierarchies(data),
291
+ reducedRuleSets: reduceRuleSets(data),
292
+ reducedRuleSetLinks: reduceRuleSetLinks(data),
293
+ reducedUsageInfo: reduceUsageInfo(data),
294
+ reducedTypePropertyPolicies: reduceTypePropertyPolicies(data),
295
+ reducedTypePropertyPolicyLinks: reduceTypePropertyPolicyLinks(data),
296
+ };
297
+ }
298
+
299
+ const saveTypesFunction: StoreFunction = function (data: ReducedDataHolder, dir: string) {
300
+ const roots = {};
301
+ for(const type of data.reducedTypes) {
302
+ const typePathParts = type.typePath.split(':');
303
+ if(!typePathParts) {
304
+ console.warn('skipping bad type with no path', type);
305
+ }
306
+ const root = typePathParts[0];
307
+ if(roots[root]) {
308
+ roots[root].push(type);
309
+ } else {
310
+ roots[root] = [type];
311
+ }
312
+ }
313
+ for(const [root, types] of Object.entries(roots)) {
314
+ const CURRENT_DIR = process.cwd();
315
+ const subDir = path.join(CURRENT_DIR, dir, 'types');
316
+ if(!fs.existsSync(subDir)) {
317
+ fs.mkdirSync(subDir);
318
+ }
319
+ const yamlStr = yaml.dump(types);
320
+ const fileLabel = root;
321
+ if(!fileLabel) {
322
+ console.warn('skipping bad type root:', root);
323
+ }
324
+ fs.writeFileSync(path.join(subDir, `${removeSlash(fileLabel)}.yml`), yamlStr, 'utf8');
325
+ }
326
+
327
+ };
328
+
329
+ function saveData(reducedData, dir: string) {
330
+
331
+ const propertyToFolderMap = {
332
+ reducedTypes: { filename: 'types', split: false, fileLabel: 'slug', saveFunction: saveTypesFunction},
333
+ reducedOptionSets: { filename: 'option-sets', split: true, fileLabel: 'name' },
334
+ reducedOptionSetHierarchies: { filename: 'option-set-hierarchies', split: true, fileLabel: 'name' },
335
+ reducedRuleSets: { filename: 'rule-sets', split: true, fileLabel: 'name' },
336
+ reducedRuleSetLinks: { filename: 'rule-set-links', split: false, fileLabel: '' },
337
+ reducedUsageInfo: { filename: 'usage-info', split: false, fileLabel: '' },
338
+ reducedTypePropertyPolicies: { filename: 'type-property-policies', split: true, fileLabel: 'label' },
339
+ reducedTypePropertyPolicyLinks: { filename: 'type-property-policy-links', split: false, fileLabel: '' },
340
+ };
341
+ for(const [key, folder] of Object.entries(propertyToFolderMap)) {
342
+ const CURRENT_DIR = process.cwd();
343
+ if(folder['saveFunction']) {
344
+ folder['saveFunction'](reducedData, dir);
345
+ } else if(folder.split) {
346
+ const subDir = path.join(CURRENT_DIR, dir, folder.filename);
347
+ if(!fs.existsSync(subDir)) {
348
+ fs.mkdirSync(subDir);
349
+ }
350
+ for(const item of reducedData[key]) {
351
+ const yamlStr = yaml.dump(item);
352
+ const fileLabel = item[folder.fileLabel];
353
+ if(!fileLabel) {
354
+ console.warn('skipping bad data:', item);
355
+ }
356
+ fs.writeFileSync(path.join(subDir, `${removeSlash(fileLabel)}.yml`), yamlStr, 'utf8');
357
+ }
358
+ } else {
359
+ const yamlStr = yaml.dump(reducedData[key]);
360
+ fs.writeFileSync(path.join(CURRENT_DIR, dir, `${folder.filename}.yml`), yamlStr, 'utf8');
361
+ }
362
+
363
+ }
364
+ }
365
+
366
+ function removeSlash(string) {
367
+ const re = /\//gi;
368
+ return string.replace(re, '-');
369
369
  }