@contrail/cli 1.0.49 → 1.0.51

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 CHANGED
@@ -4,6 +4,7 @@ import { getTypes } from './types/types-extractor';
4
4
  import { uploadTypes } from './types/types-uploader';
5
5
  import { UploadOptionSets } from './types/option-set-uploader';
6
6
  import { ParseOptionSetsLocalFiles } from './types/option-set-parser';
7
+ import { ParseOptionSetsHierarchyLocalFiles, UploadOptionSetsHierarchyLocalFiles } from './types/option-set-hierarchy-parser';
7
8
  import { MergeOptionSets } from './types/option-set-merger';
8
9
  import { MergeTypeProperties } from './types/types-properties-merger';
9
10
  import { ParseTypesFromFiles, ParseTypesFromLocalFiles, ParseTypesFromTemplateFromLocalFiles, ParseTypesFromTemplateAndDataFromLocalFiles } from './types/parse-types';
@@ -91,6 +92,14 @@ export async function cli(args) {
91
92
  ParseOptionSetsLocalFiles(args);
92
93
  break;
93
94
  }
95
+ case 'parseOptionSetsHierarchyLocalFiles': {
96
+ ParseOptionSetsHierarchyLocalFiles(args);
97
+ break;
98
+ }
99
+ case 'uploadOptionSetsHierarchyLocalFiles': {
100
+ UploadOptionSetsHierarchyLocalFiles(args);
101
+ break;
102
+ }
94
103
  case 'parseTypesFromLocalFiles': {
95
104
  ParseTypesFromLocalFiles(args);
96
105
  break;
@@ -358,7 +367,15 @@ const helpSections = [
358
367
  },
359
368
  {
360
369
  name: 'parseOptionSetsLocalFiles <File Paths>',
361
- summary: 'Parse CSV files into OptionSets'
370
+ summary: 'Parse CSV files into OptionSets.'
371
+ },
372
+ {
373
+ name: 'parseOptionSetsHierarchyLocalFiles <File Paths>',
374
+ summary: 'Parse CSV files into OptionSetHierarchies.'
375
+ },
376
+ {
377
+ name: 'uploadOptionSetsHierarchyLocalFiles <File Paths> <name>',
378
+ summary: 'upload CSV files into OptionSetHierarchies.'
362
379
  },
363
380
  {
364
381
  name: 'parseTypesFromLocalFiles <File Paths>',
@@ -76,7 +76,7 @@ async function getLocalUploadConfig(configFileName) {
76
76
  async function uploadFile(incomingFileName) {
77
77
  const csvContent = await getLocalFile(incomingFileName);
78
78
  const entities = new Entities();
79
- const fileName = `${incomingFileName.slice(0, incomingFileName.length - 5)}-Upload-${new Date().getTime()}.csv`;
79
+ const fileName = `${incomingFileName.slice(0, incomingFileName.length - 4)}-Upload-${new Date().getTime()}.csv`;
80
80
  const file = await entities.create({
81
81
  entityName: 'file',
82
82
  object: { contentType: 'text/csv', fileName: fileName, ttl: 24 * 60 * 60 * 1000 }
@@ -0,0 +1,2 @@
1
+ export declare function ParseOptionSetsHierarchyLocalFiles(args: any): Promise<void>;
2
+ export declare function UploadOptionSetsHierarchyLocalFiles(args: any): Promise<void>;
@@ -0,0 +1,140 @@
1
+ import fs from 'fs';
2
+ import { getSeparatorByPlatform } from '../common';
3
+ import chalk from 'chalk';
4
+ const csv = require('csvtojson');
5
+ import { Entities } from '@contrail/sdk';
6
+ import { StringUtil } from '@contrail/util';
7
+ import { authUser } from '../auth/login';
8
+ export async function ParseOptionSetsHierarchyLocalFiles(args) {
9
+ try {
10
+ await authUser();
11
+ if ((args === null || args === void 0 ? void 0 : args.length) < 4) {
12
+ throw new Error('File name must be included.');
13
+ }
14
+ console.log('%s Importing local file', chalk.yellow.bold('OPTION SET HIERARCHY PARSE'));
15
+ const fileLocation = args[3];
16
+ console.log('Using File: ', fileLocation);
17
+ await parseAndSaveOptionSetHierarchiesLocalFiles(fileLocation);
18
+ console.log('%s Parsed', chalk.yellow.bold('OPTION SET HIERARCHY PARSE'));
19
+ }
20
+ catch (e) {
21
+ console.log(`%s PARSE Failed. ${e}`, chalk.red.bold('FAIL'));
22
+ return;
23
+ }
24
+ console.log('%s PARSE Complete.', chalk.green.bold('COMPLETE'));
25
+ }
26
+ export async function UploadOptionSetsHierarchyLocalFiles(args) {
27
+ try {
28
+ await authUser();
29
+ if ((args === null || args === void 0 ? void 0 : args.length) < 4) {
30
+ throw new Error('File name must be included.');
31
+ }
32
+ console.log('%s Importing local file', chalk.yellow.bold('OPTION SET HIERARCHY UPLOAD'));
33
+ const fileLocation = args[3];
34
+ const name = args[4];
35
+ console.log('Using File: ', fileLocation);
36
+ console.log('Using Name: ', name);
37
+ await uploadOptionSetHierarchiesLocalFiles(fileLocation, name);
38
+ console.log('%s Uploaded', chalk.yellow.bold('OPTION SET HIERARCHY UPLOAD'));
39
+ }
40
+ catch (e) {
41
+ console.log(`%s UPLOAD Failed. ${e}`, chalk.red.bold('FAIL'));
42
+ return;
43
+ }
44
+ console.log('%s UPLOAD Complete.', chalk.green.bold('COMPLETE'));
45
+ }
46
+ async function loadOptionSetHierarchyCSV(fileLocation) {
47
+ const TARGET_DIR = process.cwd();
48
+ try {
49
+ const separator = getSeparatorByPlatform();
50
+ const fullPath = TARGET_DIR + separator + fileLocation;
51
+ console.log('Getting file from path: ', fullPath);
52
+ return await csv().fromFile(fullPath);
53
+ }
54
+ catch (e) {
55
+ throw new Error(`ERROR PARSING CSV: ${e}`);
56
+ }
57
+ }
58
+ async function parseOptionSetHierarchiesLocalFiles(fileLocation) {
59
+ const uploadedOptionHierarchyData = await loadOptionSetHierarchyCSV(fileLocation);
60
+ const optionSetHierarchy = {
61
+ hierarchy: [],
62
+ maxDepth: 0,
63
+ name: 'No Name',
64
+ options: []
65
+ };
66
+ if (!((uploadedOptionHierarchyData === null || uploadedOptionHierarchyData === void 0 ? void 0 : uploadedOptionHierarchyData.length) > 0)) {
67
+ return optionSetHierarchy;
68
+ }
69
+ const keysInOrder = Object.keys(uploadedOptionHierarchyData[0]);
70
+ const currentOptionSets = await new Entities().get({
71
+ entityName: 'type-property-option-set',
72
+ });
73
+ const currentOptionSetsByName = {};
74
+ for (const currentOptionSet of currentOptionSets) {
75
+ currentOptionSetsByName[StringUtil.convertToCamelCase(currentOptionSet.name)] = currentOptionSet;
76
+ }
77
+ const optionSetIds = [];
78
+ for (const key of keysInOrder) {
79
+ if (currentOptionSetsByName[key]) {
80
+ optionSetIds.push(currentOptionSetsByName[key].id);
81
+ }
82
+ else {
83
+ throw new Error(`No OptionSet found for ${key}`);
84
+ }
85
+ }
86
+ optionSetHierarchy.maxDepth = keysInOrder.length;
87
+ optionSetHierarchy.hierarchy = optionSetIds;
88
+ for (const row of uploadedOptionHierarchyData) {
89
+ addRowToOptions(optionSetHierarchy, keysInOrder, row);
90
+ }
91
+ return optionSetHierarchy;
92
+ }
93
+ async function parseAndSaveOptionSetHierarchiesLocalFiles(fileLocation) {
94
+ const optionSetHierarchy = await parseOptionSetHierarchiesLocalFiles(fileLocation);
95
+ saveData(optionSetHierarchy.options);
96
+ }
97
+ async function uploadOptionSetHierarchiesLocalFiles(fileLocation, name) {
98
+ const optionSetHierarchy = await parseOptionSetHierarchiesLocalFiles(fileLocation);
99
+ optionSetHierarchy.name = name;
100
+ const currentOptionSetHierarchies = await new Entities().get({
101
+ entityName: 'option-set-hierarchy',
102
+ });
103
+ const existing = currentOptionSetHierarchies.find(osh => osh.name === name);
104
+ if (existing) {
105
+ console.log('Updating: ', optionSetHierarchy.name);
106
+ await new Entities().update({
107
+ entityName: 'option-set-hierarchy',
108
+ id: existing.id,
109
+ object: optionSetHierarchy,
110
+ });
111
+ }
112
+ else {
113
+ console.log('Creating: ', optionSetHierarchy.name);
114
+ await new Entities().create({
115
+ entityName: 'option-set-hierarchy',
116
+ object: optionSetHierarchy,
117
+ });
118
+ }
119
+ }
120
+ function addRowToOptions(optionSetHierarchy, keysInOrder, row) {
121
+ let options = optionSetHierarchy.options;
122
+ for (let i = 0; i < keysInOrder.length; i++) {
123
+ const optionValues = options.map(op => op.value);
124
+ const value = row[keysInOrder[i]];
125
+ if (!optionValues.includes(value)) {
126
+ const obj = i === (keysInOrder.length - 1) ? { value: value } : { value: value, options: [] };
127
+ options.push(obj);
128
+ options = obj.options;
129
+ }
130
+ else {
131
+ const optionObj = options.find(op => op.value === value);
132
+ options = optionObj.options;
133
+ }
134
+ }
135
+ }
136
+ function saveData(data) {
137
+ const CURRENT_DIR = process.cwd();
138
+ const separator = getSeparatorByPlatform();
139
+ fs.writeFileSync(CURRENT_DIR + separator + 'parsed_local_optionSetHierarchy.json', JSON.stringify(data), 'utf8');
140
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/cli",
3
- "version": "1.0.49",
3
+ "version": "1.0.51",
4
4
  "description": "VibeIQ's Contrail Platform CLI.",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
package/src/cli.ts CHANGED
@@ -5,6 +5,7 @@ import { getTypes } 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 { ParseOptionSetsHierarchyLocalFiles, UploadOptionSetsHierarchyLocalFiles } from './types/option-set-hierarchy-parser';
8
9
  import { MergeOptionSets } from './types/option-set-merger';
9
10
  import { MergeTypeProperties } from './types/types-properties-merger';
10
11
  import { ParseTypesFromFiles, ParseTypesFromLocalFiles, ParseTypesFromTemplateFromLocalFiles, ParseTypesFromTemplateAndDataFromLocalFiles } from './types/parse-types';
@@ -101,6 +102,14 @@ export async function cli(args) {
101
102
  ParseOptionSetsLocalFiles(args);
102
103
  break;
103
104
  }
105
+ case 'parseOptionSetsHierarchyLocalFiles': {
106
+ ParseOptionSetsHierarchyLocalFiles(args);
107
+ break;
108
+ }
109
+ case 'uploadOptionSetsHierarchyLocalFiles': {
110
+ UploadOptionSetsHierarchyLocalFiles(args);
111
+ break;
112
+ }
104
113
  case 'parseTypesFromLocalFiles': {
105
114
  ParseTypesFromLocalFiles(args);
106
115
  break;
@@ -373,7 +382,15 @@ const helpSections = [
373
382
  },
374
383
  {
375
384
  name: 'parseOptionSetsLocalFiles <File Paths>',
376
- summary: 'Parse CSV files into OptionSets'
385
+ summary: 'Parse CSV files into OptionSets.'
386
+ },
387
+ {
388
+ name: 'parseOptionSetsHierarchyLocalFiles <File Paths>',
389
+ summary: 'Parse CSV files into OptionSetHierarchies.'
390
+ },
391
+ {
392
+ name: 'uploadOptionSetsHierarchyLocalFiles <File Paths> <name>',
393
+ summary: 'upload CSV files into OptionSetHierarchies.'
377
394
  },
378
395
  {
379
396
  name: 'parseTypesFromLocalFiles <File Paths>',
@@ -92,7 +92,7 @@ async function uploadFile(incomingFileName) {
92
92
  const csvContent = await getLocalFile(incomingFileName);
93
93
 
94
94
  const entities = new Entities();
95
- const fileName = `${incomingFileName.slice(0, incomingFileName.length - 5)}-Upload-${new Date().getTime()}.csv`;
95
+ const fileName = `${incomingFileName.slice(0, incomingFileName.length - 4)}-Upload-${new Date().getTime()}.csv`;
96
96
  const file = await entities.create({
97
97
  entityName: 'file',
98
98
  object: { contentType: 'text/csv', fileName: fileName, ttl: 24 * 60 * 60 * 1000 }
@@ -0,0 +1,169 @@
1
+ import fs from 'fs';
2
+ import { getSeparatorByPlatform } from '../common';
3
+ import chalk from 'chalk';
4
+ const csv = require('csvtojson');
5
+ import { Entities } from '@contrail/sdk';
6
+ import { StringUtil } from '@contrail/util';
7
+ import { authUser } from '../auth/login';
8
+
9
+ export async function ParseOptionSetsHierarchyLocalFiles(args) {
10
+ try {
11
+ await authUser();
12
+
13
+ if (args?.length < 4) {
14
+ throw new Error('File name must be included.');
15
+ }
16
+ console.log('%s Importing local file', chalk.yellow.bold('OPTION SET HIERARCHY PARSE'));
17
+ const fileLocation = args[3];
18
+ console.log('Using File: ', fileLocation);
19
+
20
+ await parseAndSaveOptionSetHierarchiesLocalFiles(fileLocation);
21
+ console.log('%s Parsed', chalk.yellow.bold('OPTION SET HIERARCHY PARSE'));
22
+
23
+ }
24
+ catch (e) {
25
+ console.log(`%s PARSE Failed. ${e}`, chalk.red.bold('FAIL'));
26
+ return;
27
+ }
28
+
29
+ console.log('%s PARSE Complete.', chalk.green.bold('COMPLETE'));
30
+ }
31
+
32
+ export async function UploadOptionSetsHierarchyLocalFiles(args) {
33
+ try {
34
+ await authUser();
35
+
36
+ if (args?.length < 4) {
37
+ throw new Error('File name must be included.');
38
+ }
39
+ console.log('%s Importing local file', chalk.yellow.bold('OPTION SET HIERARCHY UPLOAD'));
40
+ const fileLocation = args[3];
41
+ const name = args[4];
42
+ console.log('Using File: ', fileLocation);
43
+ console.log('Using Name: ', name);
44
+
45
+ await uploadOptionSetHierarchiesLocalFiles(fileLocation, name);
46
+ console.log('%s Uploaded', chalk.yellow.bold('OPTION SET HIERARCHY UPLOAD'));
47
+
48
+ }
49
+ catch (e) {
50
+ console.log(`%s UPLOAD Failed. ${e}`, chalk.red.bold('FAIL'));
51
+ return;
52
+ }
53
+
54
+ console.log('%s UPLOAD Complete.', chalk.green.bold('COMPLETE'));
55
+ }
56
+
57
+ async function loadOptionSetHierarchyCSV(fileLocation) {
58
+ const TARGET_DIR = process.cwd();
59
+ try {
60
+ const separator = getSeparatorByPlatform();
61
+
62
+ const fullPath = TARGET_DIR + separator + fileLocation;
63
+ console.log('Getting file from path: ', fullPath);
64
+
65
+ return await csv().fromFile(fullPath);
66
+ } catch (e) {
67
+ throw new Error(`ERROR PARSING CSV: ${e}`);
68
+ }
69
+ }
70
+
71
+ async function parseOptionSetHierarchiesLocalFiles(fileLocation) {
72
+ const uploadedOptionHierarchyData = await loadOptionSetHierarchyCSV(fileLocation);
73
+
74
+ const optionSetHierarchy = {
75
+ hierarchy:[],
76
+ maxDepth: 0,
77
+ name: 'No Name',
78
+ options: []
79
+ };
80
+
81
+ if (!(uploadedOptionHierarchyData?.length > 0)) {
82
+ return optionSetHierarchy;
83
+ }
84
+
85
+ const keysInOrder = Object.keys(uploadedOptionHierarchyData[0]);
86
+ const currentOptionSets = await new Entities().get({
87
+ entityName: 'type-property-option-set',
88
+ });
89
+
90
+ const currentOptionSetsByName = {};
91
+ for (const currentOptionSet of currentOptionSets) {
92
+ currentOptionSetsByName[StringUtil.convertToCamelCase(currentOptionSet.name)] = currentOptionSet;
93
+ }
94
+
95
+ const optionSetIds = [];
96
+ for(const key of keysInOrder) {
97
+ if (currentOptionSetsByName[key]) {
98
+ optionSetIds.push(currentOptionSetsByName[key].id);
99
+ }
100
+ else {
101
+ throw new Error(`No OptionSet found for ${key}`);
102
+ }
103
+ }
104
+
105
+ optionSetHierarchy.maxDepth = keysInOrder.length;
106
+ optionSetHierarchy.hierarchy = optionSetIds;
107
+
108
+ for (const row of uploadedOptionHierarchyData) {
109
+ addRowToOptions(optionSetHierarchy, keysInOrder, row);
110
+ }
111
+
112
+ return optionSetHierarchy;
113
+ }
114
+
115
+ async function parseAndSaveOptionSetHierarchiesLocalFiles(fileLocation) {
116
+ const optionSetHierarchy = await parseOptionSetHierarchiesLocalFiles(fileLocation);
117
+ saveData(optionSetHierarchy.options);
118
+ }
119
+
120
+ async function uploadOptionSetHierarchiesLocalFiles(fileLocation, name) {
121
+ const optionSetHierarchy = await parseOptionSetHierarchiesLocalFiles(fileLocation);
122
+ optionSetHierarchy.name = name;
123
+
124
+ const currentOptionSetHierarchies = await new Entities().get({
125
+ entityName: 'option-set-hierarchy',
126
+ });
127
+
128
+ const existing = currentOptionSetHierarchies.find(osh => osh.name === name);
129
+
130
+ if (existing) {
131
+ console.log('Updating: ', optionSetHierarchy.name);
132
+ await new Entities().update({
133
+ entityName: 'option-set-hierarchy',
134
+ id: existing.id,
135
+ object: optionSetHierarchy,
136
+ });
137
+ }
138
+ else {
139
+ console.log('Creating: ', optionSetHierarchy.name);
140
+ await new Entities().create({
141
+ entityName: 'option-set-hierarchy',
142
+ object: optionSetHierarchy,
143
+ });
144
+ }
145
+ }
146
+
147
+ function addRowToOptions(optionSetHierarchy, keysInOrder, row) {
148
+ let options: any[] = optionSetHierarchy.options;
149
+
150
+ for (let i = 0; i < keysInOrder.length; i++) {
151
+ const optionValues = options.map(op => op.value);
152
+ const value = row[keysInOrder[i]];
153
+ if (!optionValues.includes(value)) {
154
+ const obj = i === (keysInOrder.length - 1) ? {value: value} : {value: value, options: []};
155
+ options.push(obj);
156
+ options = obj.options;
157
+ }
158
+ else {
159
+ const optionObj = options.find(op => op.value === value);
160
+ options = optionObj.options;
161
+ }
162
+ }
163
+ }
164
+
165
+ function saveData(data) {
166
+ const CURRENT_DIR = process.cwd();
167
+ const separator = getSeparatorByPlatform();
168
+ fs.writeFileSync(CURRENT_DIR + separator + 'parsed_local_optionSetHierarchy.json', JSON.stringify(data), 'utf8');
169
+ }