@contrail/cli 1.0.15 → 1.0.16
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/types/csv-parser.d.ts +1 -0
- package/lib/types/csv-parser.js +51 -0
- package/lib/types/parse-types.js +21 -5
- package/package.json +3 -2
- package/src/types/csv-parser.ts +59 -0
- package/src/types/parse-types.ts +22 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getCSVFromFile(loadFileId: string): Promise<any[]>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import csvToJson from 'csvtojson';
|
|
2
|
+
import * as client from 'https';
|
|
3
|
+
import { Entities } from '@contrail/sdk';
|
|
4
|
+
export async function getCSVFromFile(loadFileId) {
|
|
5
|
+
let file;
|
|
6
|
+
try {
|
|
7
|
+
file = await new Entities().get({ entityName: 'file', id: loadFileId });
|
|
8
|
+
console.log('Got file: ', file);
|
|
9
|
+
}
|
|
10
|
+
catch (e) {
|
|
11
|
+
console.log(`Failed to get file: ${loadFileId}`, JSON.stringify(e));
|
|
12
|
+
throw new Error(`Failed to get file: ${loadFileId} - ` + JSON.stringify(e));
|
|
13
|
+
}
|
|
14
|
+
if (!file || !file.downloadUrl) {
|
|
15
|
+
console.log(`File: ${loadFileId} does not have a downloadUrl`);
|
|
16
|
+
throw new Error(`File: ${loadFileId} does not have a downloadUrl`);
|
|
17
|
+
}
|
|
18
|
+
// get csv
|
|
19
|
+
let response;
|
|
20
|
+
try {
|
|
21
|
+
response = await new Promise((resolve, reject) => {
|
|
22
|
+
client.get(file.downloadUrl, function (res) {
|
|
23
|
+
console.log('Status code:', res.statusCode);
|
|
24
|
+
console.log('Status text:', res.statusMessage);
|
|
25
|
+
let body = '';
|
|
26
|
+
res.on('data', (data) => {
|
|
27
|
+
body += data.toString();
|
|
28
|
+
});
|
|
29
|
+
res.on('end', () => {
|
|
30
|
+
console.log('Response body:');
|
|
31
|
+
//console.log(body);
|
|
32
|
+
resolve(body);
|
|
33
|
+
});
|
|
34
|
+
}).on('error', function (e) {
|
|
35
|
+
console.error(e);
|
|
36
|
+
reject(e);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
console.log(`Failed to get data from downloadUrl: ${file.downloadUrl}`, e);
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
if (!response) {
|
|
45
|
+
console.log('Response is empty');
|
|
46
|
+
throw new Error('Response is empty');
|
|
47
|
+
}
|
|
48
|
+
const csvJson = await csvToJson({ flatKeys: true }).fromString(response);
|
|
49
|
+
// console.log('csvJson', csvJson);
|
|
50
|
+
return csvJson;
|
|
51
|
+
}
|
package/lib/types/parse-types.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GetSuggestedTypes } from '@contrail/types';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
3
|
import fs from 'fs';
|
|
4
4
|
import { getSeparatorByPlatform } from '../common';
|
|
5
5
|
import * as csvToJson from 'csvtojson';
|
|
6
6
|
import { authUser } from '../auth/login';
|
|
7
|
+
import { getCSVFromFile } from './csv-parser';
|
|
7
8
|
const yaml = require('js-yaml');
|
|
8
9
|
export async function ParseTypesFromLocalFiles(args) {
|
|
9
10
|
try {
|
|
@@ -39,7 +40,7 @@ export async function ParseTypesFromFiles(args) {
|
|
|
39
40
|
}
|
|
40
41
|
console.log('%s Parsing Complete.', chalk.green.bold('COMPLETE'));
|
|
41
42
|
}
|
|
42
|
-
async function
|
|
43
|
+
async function loadDataFromLocalCSV(fileName) {
|
|
43
44
|
const CURRENT_DIR = process.cwd();
|
|
44
45
|
try {
|
|
45
46
|
const separator = getSeparatorByPlatform();
|
|
@@ -57,7 +58,7 @@ async function parseTypesFromLocalFiles(fileLocations) {
|
|
|
57
58
|
for (const fileLocation of fileLocations) {
|
|
58
59
|
try {
|
|
59
60
|
console.log('Getting data for fileLocation: ', fileLocation);
|
|
60
|
-
const csvData = await
|
|
61
|
+
const csvData = await loadDataFromLocalCSV(fileLocation);
|
|
61
62
|
console.log(`CSV import was successful. ${csvData === null || csvData === void 0 ? void 0 : csvData.length} rows found. Adding to total rows.`);
|
|
62
63
|
csvJSONData.push(...csvData);
|
|
63
64
|
}
|
|
@@ -67,13 +68,28 @@ async function parseTypesFromLocalFiles(fileLocations) {
|
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
console.log(`CSV imports were successful. ${csvJSONData === null || csvJSONData === void 0 ? void 0 : csvJSONData.length} total rows found.`);
|
|
70
|
-
const suggestions =
|
|
71
|
+
const suggestions = GetSuggestedTypes(csvJSONData);
|
|
71
72
|
saveData({ optionSets: suggestions.OptionSets }, 'parsed_optionSets.yml');
|
|
72
73
|
saveData({ typeProperties: suggestions.TypeProperties }, 'parsed_typeProperties.yml');
|
|
73
74
|
}
|
|
74
75
|
async function parseTypesFromFiles(fileIds) {
|
|
75
76
|
await authUser();
|
|
76
|
-
|
|
77
|
+
// get csv data
|
|
78
|
+
const csvJSONData = [];
|
|
79
|
+
for (const loadFileId of fileIds) {
|
|
80
|
+
try {
|
|
81
|
+
console.log('Getting data for fileId: ', loadFileId);
|
|
82
|
+
const csvJSON = await getCSVFromFile(loadFileId);
|
|
83
|
+
console.log(`CSV download was successful. ${csvJSON === null || csvJSON === void 0 ? void 0 : csvJSON.length} rows found. Adding to total rows`);
|
|
84
|
+
csvJSONData.push(...csvJSON);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
console.log('Error retrieving csv data', e);
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
console.log(`CSV downloads were successful. ${csvJSONData === null || csvJSONData === void 0 ? void 0 : csvJSONData.length} total rows found.`);
|
|
92
|
+
const suggestions = GetSuggestedTypes(csvJSONData);
|
|
77
93
|
saveData({ optionSets: suggestions.OptionSets }, 'parsed_optionSets.yml');
|
|
78
94
|
saveData({ typeProperties: suggestions.TypeProperties }, 'parsed_typeProperties.yml');
|
|
79
95
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrail/cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"description": "VibeIQ's Contrail Platform CLI.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"author": "",
|
|
24
24
|
"license": "ISC",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@contrail/sdk": "^1.0.
|
|
26
|
+
"@contrail/sdk": "^1.0.154",
|
|
27
|
+
"@contrail/types": "^2.0.61",
|
|
27
28
|
"@contrail/util": "^1.0.17",
|
|
28
29
|
"amazon-cognito-identity-js": "^5.2.3",
|
|
29
30
|
"arg": "^4.0.1",
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import csvToJson from 'csvtojson';
|
|
2
|
+
import * as client from 'https'
|
|
3
|
+
import { Entities } from '@contrail/sdk';
|
|
4
|
+
|
|
5
|
+
export async function getCSVFromFile(loadFileId: string): Promise<any[]> {
|
|
6
|
+
|
|
7
|
+
let file;
|
|
8
|
+
try {
|
|
9
|
+
file = await new Entities().get({ entityName: 'file', id: loadFileId });
|
|
10
|
+
console.log('Got file: ', file);
|
|
11
|
+
} catch (e) {
|
|
12
|
+
console.log(`Failed to get file: ${loadFileId}`, JSON.stringify(e));
|
|
13
|
+
throw new Error(`Failed to get file: ${loadFileId} - ` + JSON.stringify(e));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!file || !file.downloadUrl) {
|
|
17
|
+
console.log(`File: ${loadFileId} does not have a downloadUrl`);
|
|
18
|
+
throw new Error(`File: ${loadFileId} does not have a downloadUrl`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// get csv
|
|
22
|
+
let response;
|
|
23
|
+
try {
|
|
24
|
+
response = await new Promise((resolve, reject) => {
|
|
25
|
+
client.get(file.downloadUrl, function(res) {
|
|
26
|
+
console.log('Status code:', res.statusCode);
|
|
27
|
+
console.log('Status text:', res.statusMessage);
|
|
28
|
+
|
|
29
|
+
let body = '';
|
|
30
|
+
|
|
31
|
+
res.on('data', (data) => {
|
|
32
|
+
body += data.toString();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
res.on('end', () => {
|
|
36
|
+
console.log('Response body:');
|
|
37
|
+
//console.log(body);
|
|
38
|
+
resolve(body);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
}).on('error', function(e) {
|
|
42
|
+
console.error(e);
|
|
43
|
+
reject(e);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
} catch (e) {
|
|
47
|
+
console.log(`Failed to get data from downloadUrl: ${file.downloadUrl}`, e);
|
|
48
|
+
throw e;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!response) {
|
|
52
|
+
console.log('Response is empty');
|
|
53
|
+
throw new Error('Response is empty');
|
|
54
|
+
}
|
|
55
|
+
const csvJson = await csvToJson({flatKeys: true}).fromString(response);
|
|
56
|
+
// console.log('csvJson', csvJson);
|
|
57
|
+
|
|
58
|
+
return csvJson;
|
|
59
|
+
}
|
package/src/types/parse-types.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GetSuggestedTypes } from '@contrail/types';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
3
|
import fs from 'fs';
|
|
4
4
|
import { getSeparatorByPlatform } from '../common';
|
|
5
5
|
import * as csvToJson from 'csvtojson';
|
|
6
6
|
import { authUser } from '../auth/login';
|
|
7
|
+
import { getCSVFromFile } from './csv-parser';
|
|
7
8
|
|
|
8
9
|
const yaml = require('js-yaml');
|
|
9
10
|
|
|
@@ -49,7 +50,7 @@ export async function ParseTypesFromFiles(args) {
|
|
|
49
50
|
console.log('%s Parsing Complete.', chalk.green.bold('COMPLETE'));
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
async function
|
|
53
|
+
async function loadDataFromLocalCSV(fileName) {
|
|
53
54
|
const CURRENT_DIR = process.cwd();
|
|
54
55
|
try {
|
|
55
56
|
const separator = getSeparatorByPlatform();
|
|
@@ -70,7 +71,7 @@ async function parseTypesFromLocalFiles(fileLocations: string[]) {
|
|
|
70
71
|
for (const fileLocation of fileLocations) {
|
|
71
72
|
try {
|
|
72
73
|
console.log('Getting data for fileLocation: ', fileLocation);
|
|
73
|
-
const csvData = await
|
|
74
|
+
const csvData = await loadDataFromLocalCSV(fileLocation);
|
|
74
75
|
|
|
75
76
|
console.log(`CSV import was successful. ${csvData?.length} rows found. Adding to total rows.`);
|
|
76
77
|
csvJSONData.push(...csvData);
|
|
@@ -82,7 +83,7 @@ async function parseTypesFromLocalFiles(fileLocations: string[]) {
|
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
console.log(`CSV imports were successful. ${csvJSONData?.length} total rows found.`);
|
|
85
|
-
const suggestions =
|
|
86
|
+
const suggestions = GetSuggestedTypes(csvJSONData)
|
|
86
87
|
saveData({optionSets: suggestions.OptionSets}, 'parsed_optionSets.yml');
|
|
87
88
|
saveData({typeProperties: suggestions.TypeProperties}, 'parsed_typeProperties.yml');
|
|
88
89
|
}
|
|
@@ -90,7 +91,23 @@ async function parseTypesFromLocalFiles(fileLocations: string[]) {
|
|
|
90
91
|
async function parseTypesFromFiles(fileIds: string[]) {
|
|
91
92
|
await authUser();
|
|
92
93
|
|
|
93
|
-
|
|
94
|
+
// get csv data
|
|
95
|
+
const csvJSONData: any[] = [];
|
|
96
|
+
for (const loadFileId of fileIds) {
|
|
97
|
+
try {
|
|
98
|
+
console.log('Getting data for fileId: ', loadFileId);
|
|
99
|
+
const csvJSON = await getCSVFromFile(loadFileId);
|
|
100
|
+
console.log(`CSV download was successful. ${csvJSON?.length} rows found. Adding to total rows`);
|
|
101
|
+
csvJSONData.push(...csvJSON);
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
console.log('Error retrieving csv data', e);
|
|
105
|
+
throw e;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
console.log(`CSV downloads were successful. ${csvJSONData?.length} total rows found.`);
|
|
110
|
+
const suggestions = GetSuggestedTypes(csvJSONData)
|
|
94
111
|
|
|
95
112
|
saveData({optionSets: suggestions.OptionSets}, 'parsed_optionSets.yml');
|
|
96
113
|
saveData({typeProperties: suggestions.TypeProperties}, 'parsed_typeProperties.yml');
|