@contrail/cli 1.0.53 → 1.0.55
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.
- package/lib/cli.js +59 -10
- package/lib/types/option-set-hierarchy-exporter.d.ts +1 -0
- package/lib/types/option-set-hierarchy-exporter.js +113 -0
- package/lib/types/option-set-hierarchy-parser.d.ts +2 -2
- package/lib/types/option-set-hierarchy-parser.js +9 -9
- package/lib/types/option-set-hierarchy-uploader.d.ts +1 -0
- package/lib/types/option-set-hierarchy-uploader.js +110 -0
- package/lib/types/option-set-uploader.js +1 -1
- package/lib/types/rule-set-uploader.d.ts +1 -0
- package/lib/types/rule-set-uploader.js +175 -0
- package/lib/types/type-property-policy-uploader.d.ts +1 -0
- package/lib/types/type-property-policy-uploader.js +173 -0
- package/lib/types/types-extractor.d.ts +51 -2
- package/lib/types/types-extractor.js +204 -48
- package/lib/types/types-properties-merger.js +14 -8
- package/lib/types/types-uploader.js +1 -1
- package/lib/types/usage-info-uploader.d.ts +1 -0
- package/lib/types/usage-info-uploader.js +128 -0
- package/package.json +2 -1
- package/src/cli.ts +59 -10
- package/src/types/option-set-hierarchy-exporter.ts +125 -0
- package/src/types/option-set-hierarchy-parser.ts +9 -9
- package/src/types/option-set-hierarchy-uploader.ts +119 -0
- package/src/types/option-set-uploader.ts +2 -4
- package/src/types/rule-set-uploader.ts +190 -0
- package/src/types/type-property-policy-uploader.ts +188 -0
- package/src/types/types-extractor.ts +222 -54
- package/src/types/types-properties-merger.ts +14 -9
- package/src/types/types-uploader.ts +1 -1
- package/src/types/usage-info-uploader.ts +135 -0
- /package/src/colors/{color-example.csv → colors.csv} +0 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { getSeparatorByPlatform } from '../common';
|
|
4
|
+
import { authUser } from '../auth/login';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
import { getTypes } from './types-extractor';
|
|
7
|
+
const yaml = require('js-yaml');
|
|
8
|
+
export async function uploadUsageInfo(args) {
|
|
9
|
+
try {
|
|
10
|
+
await authUser();
|
|
11
|
+
console.log('%s Uploading', chalk.yellow.bold('USAGE INFO'));
|
|
12
|
+
const reducedUsageInfo = await loadUsageInfo();
|
|
13
|
+
await uploadUsageInfoObjects(reducedUsageInfo);
|
|
14
|
+
console.log('%s Uploaded', chalk.yellow.bold('USAGE INFO'));
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
console.log(`%s Usage Info Upload Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
console.log('%s Usage Info Upload Complete.', chalk.green.bold('COMPLETE'));
|
|
21
|
+
}
|
|
22
|
+
function loadUsageInfo() {
|
|
23
|
+
const TARGET_DIR = process.cwd();
|
|
24
|
+
try {
|
|
25
|
+
const separator = getSeparatorByPlatform();
|
|
26
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
|
|
27
|
+
return doc.reducedUsageInfo;
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async function uploadUsageInfoObjects(loadedUsageInfo) {
|
|
34
|
+
const typesWithProperties = await getTypes();
|
|
35
|
+
const typeProperties = typesWithProperties
|
|
36
|
+
.filter(t => !!t.typeProperties)
|
|
37
|
+
.map(t => t.typeProperties)
|
|
38
|
+
.reduce((a, b) => a.concat(b));
|
|
39
|
+
const currentUsageInfo = await new Entities().get({
|
|
40
|
+
entityName: 'type-property-usage-info',
|
|
41
|
+
});
|
|
42
|
+
const currentUsageInfosHashes = {};
|
|
43
|
+
for (const currentUI of currentUsageInfo) {
|
|
44
|
+
const foundType = typesWithProperties.find(t => t.id === currentUI.typeId);
|
|
45
|
+
const foundTypeProperty = typeProperties.find(tp => tp.id === currentUI.typePropertyId);
|
|
46
|
+
if (!foundType || !foundTypeProperty) {
|
|
47
|
+
console.log('skipping unhashable, invalid usage info:', currentUI);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const hash = `${foundType.slug}|${foundTypeProperty.slug}`;
|
|
51
|
+
currentUsageInfosHashes[hash] = currentUI;
|
|
52
|
+
}
|
|
53
|
+
const reducedUsageInfoHashes = {};
|
|
54
|
+
for (const loadedUI of loadedUsageInfo) {
|
|
55
|
+
const hash = `${loadedUI.typeSlug}|${loadedUI.typePropertySlug}`;
|
|
56
|
+
reducedUsageInfoHashes[hash] = loadedUI;
|
|
57
|
+
}
|
|
58
|
+
console.debug('current usage info:', currentUsageInfo);
|
|
59
|
+
console.debug('loaded usage info:', loadedUsageInfo);
|
|
60
|
+
for (const loadedUI of loadedUsageInfo) {
|
|
61
|
+
try {
|
|
62
|
+
const found = currentUsageInfosHashes[`${loadedUI.typeSlug}|${loadedUI.typePropertySlug}`];
|
|
63
|
+
if (!found) {
|
|
64
|
+
if (!transformReducedUI(loadedUI, typesWithProperties, typeProperties)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
console.debug('Creating: ', loadedUI);
|
|
68
|
+
await new Entities().create({
|
|
69
|
+
entityName: 'type-property-usage-info',
|
|
70
|
+
object: {
|
|
71
|
+
typePropertyId: loadedUI.typePropertyId,
|
|
72
|
+
typeId: loadedUI.typeId,
|
|
73
|
+
editable: loadedUI.editable,
|
|
74
|
+
used: loadedUI.used,
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
if (!transformReducedUI(loadedUI, typesWithProperties, typeProperties)) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
console.log('Updating: ', loadedUI);
|
|
83
|
+
await new Entities().update({
|
|
84
|
+
entityName: 'type-property-usage-info',
|
|
85
|
+
id: found.id,
|
|
86
|
+
object: {
|
|
87
|
+
editable: loadedUI.editable,
|
|
88
|
+
used: loadedUI.used,
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
throw new Error(`Error Uploading usage info: ${err}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
for (const [hash, currentUI] of Object.entries(currentUsageInfosHashes)) {
|
|
98
|
+
try {
|
|
99
|
+
const found = reducedUsageInfoHashes[hash];
|
|
100
|
+
if (!found) {
|
|
101
|
+
console.debug('Deleting: ', hash);
|
|
102
|
+
await new Entities().delete({
|
|
103
|
+
entityName: 'type-property-usage-info',
|
|
104
|
+
id: currentUI['id'],
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
throw new Error(`Error Deleting usage info: ${err}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// transforms the reduced Usage Info from name slugs to ids
|
|
114
|
+
function transformReducedUI(reducedUsageInfo, types, typeProperties) {
|
|
115
|
+
const foundType = types.find(t => t.slug === reducedUsageInfo.typeSlug);
|
|
116
|
+
if (!foundType) {
|
|
117
|
+
console.error('Could not find type with slug', reducedUsageInfo.typeSlug, 'while transforming', reducedUsageInfo);
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
reducedUsageInfo.typeId = foundType.id;
|
|
121
|
+
const foundTypeProperty = typeProperties.find(tp => tp.slug === reducedUsageInfo.typePropertySlug);
|
|
122
|
+
if (!foundTypeProperty) {
|
|
123
|
+
console.error('Could not find type property with slug', reducedUsageInfo.typePropertySlug, 'while transforming', reducedUsageInfo);
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
reducedUsageInfo.typePropertyId = foundTypeProperty.id;
|
|
127
|
+
return true;
|
|
128
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrail/cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.55",
|
|
4
4
|
"description": "VibeIQ's Contrail Platform CLI.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"execa": "^5.0.0",
|
|
38
38
|
"inquirer": "^8.2.0",
|
|
39
39
|
"js-yaml": "^4.1.0",
|
|
40
|
+
"json2csv": "^5.0.7",
|
|
40
41
|
"listr": "^0.14.3",
|
|
41
42
|
"lodash": "^4.17.21",
|
|
42
43
|
"ncp": "^2.0.0",
|
package/src/cli.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
import { create } from './apps/create-app/create';
|
|
3
3
|
import { deployApp, removeApp } from './apps/deploy-app/deploy';
|
|
4
|
-
import {
|
|
4
|
+
import { getAll } from './types/types-extractor';
|
|
5
5
|
import { uploadTypes } from './types/types-uploader';
|
|
6
6
|
import { UploadOptionSets } from './types/option-set-uploader';
|
|
7
7
|
import { ParseOptionSetsLocalFiles } from './types/option-set-parser';
|
|
8
|
-
import {
|
|
8
|
+
import { ParseOptionSetsHierarchyLocalFile, UploadOptionSetsHierarchyLocalFile } from './types/option-set-hierarchy-parser';
|
|
9
|
+
import { ExportOptionSetsHierarchies } from './types/option-set-hierarchy-exporter';
|
|
9
10
|
import { MergeOptionSets } from './types/option-set-merger';
|
|
10
11
|
import { MergeTypeProperties } from './types/types-properties-merger';
|
|
11
12
|
import { ParseTypesFromFiles, ParseTypesFromLocalFiles, ParseTypesFromTemplateFromLocalFiles, ParseTypesFromTemplateAndDataFromLocalFiles } from './types/parse-types';
|
|
@@ -18,6 +19,10 @@ import { cliInstallWorkflowTemplateDefinitions, listWorkflowTemplateDefinitions
|
|
|
18
19
|
import { configureProject } from './projects/project-uploader';
|
|
19
20
|
import { ListProjects, ExportProject } from './projects/project-exporter';
|
|
20
21
|
import { Load, UploadAndLoad } from './loader-process/loader-process';
|
|
22
|
+
import { uploadUsageInfo } from './types/usage-info-uploader';
|
|
23
|
+
import { uploadRuleSets } from './types/rule-set-uploader';
|
|
24
|
+
import { UploadOptionSetHierarchies } from './types/option-set-hierarchy-uploader';
|
|
25
|
+
import { uploadTypePropertyPolicies } from './types/type-property-policy-uploader';
|
|
21
26
|
|
|
22
27
|
const cliUsage = require('command-line-usage');
|
|
23
28
|
|
|
@@ -88,7 +93,7 @@ export async function cli(args) {
|
|
|
88
93
|
case 'types': {
|
|
89
94
|
switch (command) {
|
|
90
95
|
case 'getAll': {
|
|
91
|
-
|
|
96
|
+
getAll(args);
|
|
92
97
|
break;
|
|
93
98
|
}
|
|
94
99
|
case 'uploadTypes': {
|
|
@@ -103,12 +108,32 @@ export async function cli(args) {
|
|
|
103
108
|
ParseOptionSetsLocalFiles(args);
|
|
104
109
|
break;
|
|
105
110
|
}
|
|
106
|
-
case '
|
|
107
|
-
|
|
111
|
+
case 'parseOptionSetsHierarchyLocalFile': {
|
|
112
|
+
ParseOptionSetsHierarchyLocalFile(args);
|
|
108
113
|
break;
|
|
109
114
|
}
|
|
110
|
-
case '
|
|
111
|
-
|
|
115
|
+
case 'uploadOptionSetsHierarchyLocalFile': {
|
|
116
|
+
UploadOptionSetsHierarchyLocalFile(args);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case 'exportOptionSetsHierarchies': {
|
|
120
|
+
ExportOptionSetsHierarchies();
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case 'uploadOptionSetsHierarchies': {
|
|
124
|
+
UploadOptionSetHierarchies(args);
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case 'uploadRuleSets': {
|
|
128
|
+
uploadRuleSets(args);
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case 'uploadUsageInfo': {
|
|
132
|
+
uploadUsageInfo(args);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
case 'uploadTypePropertyPolicies': {
|
|
136
|
+
uploadTypePropertyPolicies(args);
|
|
112
137
|
break;
|
|
113
138
|
}
|
|
114
139
|
case 'parseTypesFromLocalFiles': {
|
|
@@ -399,11 +424,35 @@ const helpSections = [
|
|
|
399
424
|
},
|
|
400
425
|
{
|
|
401
426
|
name: 'parseOptionSetsLocalFiles <File Paths>',
|
|
402
|
-
summary: 'Parse CSV files into OptionSets
|
|
427
|
+
summary: 'Parse CSV files into OptionSets.\n'
|
|
428
|
+
},
|
|
429
|
+
{
|
|
430
|
+
name: 'parseOptionSetsHierarchyLocalFile <File Path>',
|
|
431
|
+
summary: 'Parse CSV file into OptionSetHierarchy.'
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
name: 'uploadOptionSetsHierarchyLocalFile <File Path> <name>',
|
|
435
|
+
summary: 'upload CSV file into OptionSetHierarchy.'
|
|
436
|
+
},
|
|
437
|
+
{
|
|
438
|
+
name: 'exportOptionSetsHierarchies',
|
|
439
|
+
summary: 'export OptionSetHierarchies into CSVs.\n'
|
|
440
|
+
},
|
|
441
|
+
{
|
|
442
|
+
name: 'uploadOptionSetsHierarchies',
|
|
443
|
+
summary: 'Upload an extracted option set hierarchies.'
|
|
444
|
+
},
|
|
445
|
+
{
|
|
446
|
+
name: 'uploadRuleSets',
|
|
447
|
+
summary: 'Upload an extracted rule set.'
|
|
448
|
+
},
|
|
449
|
+
{
|
|
450
|
+
name: 'uploadUsageInfo',
|
|
451
|
+
summary: 'Upload extracted usage info.'
|
|
403
452
|
},
|
|
404
453
|
{
|
|
405
|
-
name: '
|
|
406
|
-
summary: '
|
|
454
|
+
name: 'uploadTypePropertyPolicies',
|
|
455
|
+
summary: 'Upload type property policies.'
|
|
407
456
|
},
|
|
408
457
|
{
|
|
409
458
|
name: 'uploadOptionSetsHierarchyLocalFiles <File Paths> <name>',
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { getSeparatorByPlatform } from '../common';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import { Entities } from '@contrail/sdk';
|
|
5
|
+
import { authUser } from '../auth/login';
|
|
6
|
+
import { ObjectUtil, StringUtil } from '@contrail/util';
|
|
7
|
+
const { Parser } = require('json2csv');
|
|
8
|
+
|
|
9
|
+
export async function ExportOptionSetsHierarchies() {
|
|
10
|
+
try {
|
|
11
|
+
await authUser();
|
|
12
|
+
|
|
13
|
+
console.log('%s Exporting local file', chalk.yellow.bold('OPTION SET HIERARCHY EXPORT'));
|
|
14
|
+
await exportOptionSetHierarchies();
|
|
15
|
+
console.log('%s Exported', chalk.yellow.bold('OPTION SET HIERARCHY EXPORT'));
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
console.log(`%s EXPORT Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
console.log('%s EXPORT Complete.', chalk.green.bold('COMPLETE'));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function exportOptionSetHierarchies() {
|
|
27
|
+
const allOptionSetHierarchies = await new Entities().get({
|
|
28
|
+
entityName: 'option-set-hierarchy',
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const currentOptionSets = await new Entities().get({
|
|
32
|
+
entityName: 'type-property-option-set',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
for (const optionSetHierarchy of allOptionSetHierarchies) {
|
|
36
|
+
const levelNameInOrder = [];
|
|
37
|
+
for (const optionSetId of optionSetHierarchy.hierarchy) {
|
|
38
|
+
const os = currentOptionSets.find(os => os.id === optionSetId);
|
|
39
|
+
levelNameInOrder.push(StringUtil.convertToCamelCase(os.name));
|
|
40
|
+
}
|
|
41
|
+
transformAndSave(optionSetHierarchy, levelNameInOrder);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function transformAndSave(optionSetHierarchy, levelNameInOrder) {
|
|
46
|
+
const finalObjects = [];
|
|
47
|
+
transform(optionSetHierarchy.options, levelNameInOrder, [], finalObjects);
|
|
48
|
+
saveData(optionSetHierarchy.name, finalObjects, levelNameInOrder);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
Transform will take in a set if options, the names left to process, the partly completed objects, and the list to place the final objects.
|
|
53
|
+
On the first pass the list of names will be full and the list of partial objects will be empty. On this pass we create objects
|
|
54
|
+
with one field, the name of the field is the first name in levelNameInOrder. These objects are added to the workingObjects list
|
|
55
|
+
for the next run. On the following (recursive) runs there will be one less name in the levelNameInOrder (we remove the name we just processed)
|
|
56
|
+
and there will be objects to clone and add to in the workingObjects list. When all the objects have been created (There are no names
|
|
57
|
+
left in the levelNameInOrder list), all the objects are added to the finalObjects list.
|
|
58
|
+
|
|
59
|
+
Initial Input:
|
|
60
|
+
options: {value: "BTP", options: [{value: "The Great"}, {value: "The Best"}]}
|
|
61
|
+
levelNameInOrder: ["name", "title"]
|
|
62
|
+
workingObjects: []
|
|
63
|
+
finalObjects: []
|
|
64
|
+
|
|
65
|
+
Level 2 Input 1:
|
|
66
|
+
options: [{value: "The Great"}]
|
|
67
|
+
levelNameInOrder: ["title"]
|
|
68
|
+
workingObjects: [{name: "BTP"}]
|
|
69
|
+
finalObjects: []
|
|
70
|
+
|
|
71
|
+
Level 2 Input 2:
|
|
72
|
+
options: [{value: "The Best"}]
|
|
73
|
+
levelNameInOrder: ["title"]
|
|
74
|
+
workingObjects: [{name: "BTP"}]
|
|
75
|
+
finalObjects: []
|
|
76
|
+
|
|
77
|
+
Final Objects:
|
|
78
|
+
[{name: "BTP", title: "The Great"}, {name: "BTP", title: "The Best"}]
|
|
79
|
+
*/
|
|
80
|
+
function transform(options, levelNameInOrder, workingObjects, finalObjects) {
|
|
81
|
+
for (const option of options) {
|
|
82
|
+
const localObjects = [];
|
|
83
|
+
if (workingObjects.length === 0) {
|
|
84
|
+
localObjects.push({[levelNameInOrder[0]]: option.value});
|
|
85
|
+
if (levelNameInOrder.length <= 1) {
|
|
86
|
+
finalObjects.push(...localObjects);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
transform(option.options, levelNameInOrder.slice(1,), localObjects, finalObjects);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
for (const workingObject of workingObjects) {
|
|
94
|
+
const clone = ObjectUtil.cloneDeep(workingObject);
|
|
95
|
+
clone[levelNameInOrder[0]] = option.value;
|
|
96
|
+
localObjects.push(clone);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (levelNameInOrder.length <= 1) {
|
|
100
|
+
finalObjects.push(...localObjects);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
transform(option.options, levelNameInOrder.slice(1,), localObjects, finalObjects);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function saveData(name, data, fieldNames) {
|
|
111
|
+
const CURRENT_DIR = process.cwd();
|
|
112
|
+
const separator = getSeparatorByPlatform();
|
|
113
|
+
const opts = { fieldNames };
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const parser = new Parser(opts);
|
|
117
|
+
const csv = parser.parse(data);
|
|
118
|
+
fs.mkdirSync(CURRENT_DIR + separator + 'option-set-hierarchies', { recursive: true });
|
|
119
|
+
fs.writeFileSync(CURRENT_DIR + separator + 'option-set-hierarchies' + separator + name + '.csv', csv, {
|
|
120
|
+
flag: 'w',
|
|
121
|
+
});
|
|
122
|
+
} catch (err) {
|
|
123
|
+
console.error(err);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -6,7 +6,7 @@ import { Entities } from '@contrail/sdk';
|
|
|
6
6
|
import { StringUtil } from '@contrail/util';
|
|
7
7
|
import { authUser } from '../auth/login';
|
|
8
8
|
|
|
9
|
-
export async function
|
|
9
|
+
export async function ParseOptionSetsHierarchyLocalFile(args) {
|
|
10
10
|
try {
|
|
11
11
|
await authUser();
|
|
12
12
|
|
|
@@ -17,7 +17,7 @@ export async function ParseOptionSetsHierarchyLocalFiles(args) {
|
|
|
17
17
|
const fileLocation = args[3];
|
|
18
18
|
console.log('Using File: ', fileLocation);
|
|
19
19
|
|
|
20
|
-
await
|
|
20
|
+
await parseAndSaveOptionSetHierarchiesLocalFile(fileLocation);
|
|
21
21
|
console.log('%s Parsed', chalk.yellow.bold('OPTION SET HIERARCHY PARSE'));
|
|
22
22
|
|
|
23
23
|
}
|
|
@@ -29,7 +29,7 @@ export async function ParseOptionSetsHierarchyLocalFiles(args) {
|
|
|
29
29
|
console.log('%s PARSE Complete.', chalk.green.bold('COMPLETE'));
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
export async function
|
|
32
|
+
export async function UploadOptionSetsHierarchyLocalFile(args) {
|
|
33
33
|
try {
|
|
34
34
|
await authUser();
|
|
35
35
|
|
|
@@ -42,7 +42,7 @@ export async function UploadOptionSetsHierarchyLocalFiles(args) {
|
|
|
42
42
|
console.log('Using File: ', fileLocation);
|
|
43
43
|
console.log('Using Name: ', name);
|
|
44
44
|
|
|
45
|
-
await
|
|
45
|
+
await uploadOptionSetHierarchiesLocalFile(fileLocation, name);
|
|
46
46
|
console.log('%s Uploaded', chalk.yellow.bold('OPTION SET HIERARCHY UPLOAD'));
|
|
47
47
|
|
|
48
48
|
}
|
|
@@ -68,7 +68,7 @@ async function loadOptionSetHierarchyCSV(fileLocation) {
|
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
async function
|
|
71
|
+
async function parseOptionSetHierarchiesLocalFile(fileLocation) {
|
|
72
72
|
const uploadedOptionHierarchyData = await loadOptionSetHierarchyCSV(fileLocation);
|
|
73
73
|
|
|
74
74
|
const optionSetHierarchy = {
|
|
@@ -112,13 +112,13 @@ async function parseOptionSetHierarchiesLocalFiles(fileLocation) {
|
|
|
112
112
|
return optionSetHierarchy;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
async function
|
|
116
|
-
const optionSetHierarchy = await
|
|
115
|
+
async function parseAndSaveOptionSetHierarchiesLocalFile(fileLocation) {
|
|
116
|
+
const optionSetHierarchy = await parseOptionSetHierarchiesLocalFile(fileLocation);
|
|
117
117
|
saveData(optionSetHierarchy.options);
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
async function
|
|
121
|
-
const optionSetHierarchy = await
|
|
120
|
+
async function uploadOptionSetHierarchiesLocalFile(fileLocation, name) {
|
|
121
|
+
const optionSetHierarchy = await parseOptionSetHierarchiesLocalFile(fileLocation);
|
|
122
122
|
optionSetHierarchy.name = name;
|
|
123
123
|
|
|
124
124
|
const currentOptionSetHierarchies = await new Entities().get({
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { getSeparatorByPlatform } from '../common';
|
|
4
|
+
import { authUser } from '../auth/login';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
|
|
7
|
+
const yaml = require('js-yaml');
|
|
8
|
+
|
|
9
|
+
export async function UploadOptionSetHierarchies(args) {
|
|
10
|
+
try {
|
|
11
|
+
await authUser();
|
|
12
|
+
|
|
13
|
+
console.log('%s Uploading', chalk.yellow.bold('OPTION SET HIERARCHIES'));
|
|
14
|
+
const optionSetHierarchies = await loadOptionSetHierarchies();
|
|
15
|
+
await uploadOptionSetHierarchies(optionSetHierarchies);
|
|
16
|
+
console.log('%s Uploaded', chalk.yellow.bold('OPTION SET HIERARCHIES'));
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
console.log(`%s Option set hierarchy Upload Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
console.log('%s Option set hierarchy Upload Complete.', chalk.green.bold('COMPLETE'));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function loadOptionSetHierarchies() {
|
|
28
|
+
const TARGET_DIR = process.cwd();
|
|
29
|
+
try {
|
|
30
|
+
const separator = getSeparatorByPlatform();
|
|
31
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
|
|
32
|
+
return doc.reducedOptionSetHierarchies;
|
|
33
|
+
} catch (e) {
|
|
34
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function uploadOptionSetHierarchies(loadedOSHs) {
|
|
39
|
+
|
|
40
|
+
const optionSets = await new Entities().get({
|
|
41
|
+
entityName: 'type-property-option-set',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const currentOSHs = await new Entities().get({
|
|
45
|
+
entityName: 'option-set-hierarchy',
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const optionSetsByName = {};
|
|
49
|
+
for (const optionSet of optionSets) {
|
|
50
|
+
optionSetsByName[optionSet.name] = optionSet;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const currentOSHByName = {};
|
|
54
|
+
for (const currentOSH of currentOSHs) {
|
|
55
|
+
currentOSHByName[currentOSH.name] = currentOSH;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const loadedOSHByName = {};
|
|
59
|
+
for (const loadedOSH of loadedOSHs) {
|
|
60
|
+
loadedOSHByName[loadedOSH.name] = loadedOSH;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
console.debug('current OSH:', currentOSHs);
|
|
64
|
+
console.debug('loaded OSH:', loadedOSHs);
|
|
65
|
+
|
|
66
|
+
for (const osh of loadedOSHs) {
|
|
67
|
+
try {
|
|
68
|
+
const found = currentOSHByName[osh.name];
|
|
69
|
+
if (!found) {
|
|
70
|
+
if (!transformOSHNameToID(osh.hierarchy, optionSets)) { continue; }
|
|
71
|
+
console.log('Creating: ', osh);
|
|
72
|
+
await new Entities().create({
|
|
73
|
+
entityName: 'option-set-hierarchy',
|
|
74
|
+
object: osh
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
if (!transformOSHNameToID(osh.hierarchy, optionSets)) { continue; }
|
|
78
|
+
console.log('Updating: ', osh);
|
|
79
|
+
await new Entities().update({
|
|
80
|
+
entityName: 'option-set-hierarchy',
|
|
81
|
+
id: found.id,
|
|
82
|
+
object: osh,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
} catch (err) {
|
|
86
|
+
throw new Error(`Error Uploading option set hierarchy: ${err}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
for (const currentOSH of currentOSHs) {
|
|
91
|
+
try {
|
|
92
|
+
const found = loadedOSHByName[currentOSH.name];
|
|
93
|
+
if (!found) {
|
|
94
|
+
console.log('Deleting: ', currentOSH.name);
|
|
95
|
+
await new Entities().delete({
|
|
96
|
+
entityName: 'option-set-hierarchy',
|
|
97
|
+
id: currentOSH.id,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
} catch (err) {
|
|
101
|
+
throw new Error(`Error Deleting Option set hierarchy: ${err}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// transforms the passed hierarchy from names to ids, and returns true if operation succeeded.
|
|
108
|
+
function transformOSHNameToID(hierarchy, optionSets) {
|
|
109
|
+
console.log('passed hierarhy:', hierarchy);
|
|
110
|
+
for (let i = 0; i < hierarchy.length; i++) {
|
|
111
|
+
const os = optionSets.find(os => os.name === hierarchy[i]);
|
|
112
|
+
if (!os) {
|
|
113
|
+
console.error('could not find option set with name', hierarchy[i], ' at position', i, 'skipping...');
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
hierarchy[i] = os.id;
|
|
117
|
+
}
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
@@ -30,7 +30,7 @@ function loadOptionSets(){
|
|
|
30
30
|
try {
|
|
31
31
|
const separator = getSeparatorByPlatform();
|
|
32
32
|
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'option_sets.yml', 'utf8'));
|
|
33
|
-
return doc.
|
|
33
|
+
return doc.reducedOptionSets;
|
|
34
34
|
} catch (e) {
|
|
35
35
|
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
36
36
|
}
|
|
@@ -70,9 +70,7 @@ async function uploadOptionSets() {
|
|
|
70
70
|
isArchived: optionSet.isArchived,
|
|
71
71
|
},
|
|
72
72
|
});
|
|
73
|
-
}
|
|
74
|
-
else
|
|
75
|
-
{
|
|
73
|
+
} else {
|
|
76
74
|
console.log('Updating: ', optionSet.name);
|
|
77
75
|
await new Entities().update({
|
|
78
76
|
entityName: 'type-property-option-set',
|