contensis-cli 1.1.1-beta.0 → 1.1.1-beta.10

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.
@@ -6,7 +6,7 @@ import fetch from 'node-fetch';
6
6
  import path from 'path';
7
7
 
8
8
  import { Component, ContentType, Project } from 'contensis-core-api';
9
- import { Entry, Role } from 'contensis-management-api/lib/models';
9
+ import { Role } from 'contensis-management-api/lib/models';
10
10
  import {
11
11
  ContensisMigrationService,
12
12
  MigrateRequest,
@@ -24,7 +24,7 @@ import ContensisAuthService from './ContensisAuthService';
24
24
  import { LogMessages } from '~/localisation/en-GB';
25
25
  import { OutputFormat, OutputOptionsConstructorArg } from '~/models/CliService';
26
26
 
27
- import { readJsonFile } from '~/providers/file-provider';
27
+ import { readFileAsJSON } from '~/providers/file-provider';
28
28
  import SessionCacheProvider from '../providers/SessionCacheProvider';
29
29
  import CredentialProvider from '~/providers/CredentialProvider';
30
30
 
@@ -189,12 +189,12 @@ class ContensisCli {
189
189
  }
190
190
  }
191
191
 
192
- PrintEnvironments = () => {
192
+ PrintEnvironments = async () => {
193
193
  const { log, messages } = this;
194
194
  const { currentEnvironment, environments = {} } = this.cache;
195
195
  const envKeys = Object.keys(environments);
196
196
  log.success(messages.envs.found(envKeys.length));
197
- this.HandleFormattingAndOutput(envKeys, () => {
197
+ await this.HandleFormattingAndOutput(envKeys, () => {
198
198
  // print the envKeys to console
199
199
  for (const env of envKeys) {
200
200
  console.log(` - ${currentEnvironment === env ? '* ' : ''}${env}`);
@@ -297,9 +297,7 @@ class ContensisCli {
297
297
  }) => {
298
298
  const source: 'contensis' | 'file' = fromFile ? 'file' : 'contensis';
299
299
 
300
- const fileData = fromFile
301
- ? readJsonFile<(Entry | ContentType | Component)[]>(fromFile) || []
302
- : [];
300
+ const fileData = fromFile ? (await readFileAsJSON(fromFile)) || [] : [];
303
301
 
304
302
  if (typeof fileData === 'string')
305
303
  throw new Error(`Import file format must be of type JSON`);
@@ -565,7 +563,7 @@ class ContensisCli {
565
563
 
566
564
  if (Array.isArray(projects)) {
567
565
  // Print contensis version to console
568
- this.HandleFormattingAndOutput(contensis.contensisVersion, () =>
566
+ await this.HandleFormattingAndOutput(contensis.contensisVersion, () =>
569
567
  log.raw(log.highlightText(contensis.contensisVersion))
570
568
  );
571
569
  }
@@ -588,7 +586,7 @@ class ContensisCli {
588
586
  );
589
587
  if (token) {
590
588
  // Print bearer token to console
591
- this.HandleFormattingAndOutput(token, () =>
589
+ await this.HandleFormattingAndOutput(token, () =>
592
590
  log.raw(log.highlightText(token))
593
591
  );
594
592
  }
@@ -627,7 +625,7 @@ class ContensisCli {
627
625
  log.success(messages.projects.list());
628
626
  log.raw('');
629
627
 
630
- this.HandleFormattingAndOutput(projects, () => {
628
+ await this.HandleFormattingAndOutput(projects, () => {
631
629
  // print the projects to console
632
630
  for (const project of projects.sort((a, b) =>
633
631
  a.id.localeCompare(b.id)
@@ -681,7 +679,7 @@ class ContensisCli {
681
679
 
682
680
  if (foundProject) {
683
681
  log.raw('');
684
- this.HandleFormattingAndOutput(foundProject, log.object);
682
+ await this.HandleFormattingAndOutput(foundProject, log.object);
685
683
  }
686
684
 
687
685
  if (projectsErr) {
@@ -746,7 +744,7 @@ class ContensisCli {
746
744
 
747
745
  if (Array.isArray(apiKeys)) {
748
746
  log.success(messages.keys.list(currentEnv));
749
- this.HandleFormattingAndOutput(apiKeys, () => {
747
+ await this.HandleFormattingAndOutput(apiKeys, () => {
750
748
  // print the keys to console
751
749
  for (const {
752
750
  id,
@@ -835,7 +833,7 @@ class ContensisCli {
835
833
 
836
834
  if (!roles.length) log.help(messages.roles.noneExist());
837
835
 
838
- this.HandleFormattingAndOutput(roles, () => {
836
+ await this.HandleFormattingAndOutput(roles, () => {
839
837
  // print the roles to console
840
838
  for (const {
841
839
  id,
@@ -918,7 +916,7 @@ class ContensisCli {
918
916
 
919
917
  const role = findByIdOrName(roles, roleNameOrId);
920
918
 
921
- if (role) this.HandleFormattingAndOutput(role, log.object);
919
+ if (role) await this.HandleFormattingAndOutput(role, log.object);
922
920
  else log.error(messages.roles.failedGet(currentEnv, roleNameOrId));
923
921
  }
924
922
 
@@ -941,7 +939,7 @@ class ContensisCli {
941
939
  messages.roles.created(currentEnv, role.id || role.name || '')
942
940
  );
943
941
 
944
- this.HandleFormattingAndOutput(created, log.object);
942
+ await this.HandleFormattingAndOutput(created, log.object);
945
943
 
946
944
  log.help(messages.roles.tip());
947
945
  return role.id;
@@ -981,7 +979,7 @@ class ContensisCli {
981
979
  else {
982
980
  log.success(messages.roles.set());
983
981
 
984
- this.HandleFormattingAndOutput(updated, log.object);
982
+ await this.HandleFormattingAndOutput(updated, log.object);
985
983
  }
986
984
  } else {
987
985
  // Role does not exist
@@ -1045,7 +1043,7 @@ class ContensisCli {
1045
1043
  const stringFromLanguageObject = (o: { [lang: string]: string }) =>
1046
1044
  Object.values(o || {})?.[0];
1047
1045
 
1048
- this.HandleFormattingAndOutput(workflows, () => {
1046
+ await this.HandleFormattingAndOutput(workflows, () => {
1049
1047
  // print the workflows to console
1050
1048
  // log.object(workflows);
1051
1049
  for (const {
@@ -1108,7 +1106,8 @@ class ContensisCli {
1108
1106
 
1109
1107
  const workflow = findByIdOrName(workflows, workflowNameOrId);
1110
1108
 
1111
- if (workflow) this.HandleFormattingAndOutput(workflow, log.object);
1109
+ if (workflow)
1110
+ await this.HandleFormattingAndOutput(workflow, log.object);
1112
1111
  else
1113
1112
  log.error(messages.workflows.failedGet(currentEnv, workflowNameOrId));
1114
1113
  }
@@ -1130,7 +1129,7 @@ class ContensisCli {
1130
1129
  if (created) {
1131
1130
  log.success(messages.projects.created(currentEnv, project.id));
1132
1131
 
1133
- this.HandleFormattingAndOutput(created, () => {
1132
+ await this.HandleFormattingAndOutput(created, () => {
1134
1133
  // set the CLI project to the newly created project
1135
1134
  this.SetProject(project.id);
1136
1135
  // print all the projects to console
@@ -1158,7 +1157,7 @@ class ContensisCli {
1158
1157
  if (updated) {
1159
1158
  log.success(messages.projects.updated(currentEnv, currentProject));
1160
1159
 
1161
- this.HandleFormattingAndOutput(updated, log.object);
1160
+ await this.HandleFormattingAndOutput(updated, log.object);
1162
1161
  return updated.id;
1163
1162
  }
1164
1163
 
@@ -1218,7 +1217,7 @@ class ContensisCli {
1218
1217
 
1219
1218
  if (Array.isArray(returnModels)) {
1220
1219
  log.success(messages.models.list(currentProject));
1221
- this.HandleFormattingAndOutput(contentModelBackup, () => {
1220
+ await this.HandleFormattingAndOutput(contentModelBackup, () => {
1222
1221
  // print the content models to console
1223
1222
  for (const model of returnModels) {
1224
1223
  log.raw('');
@@ -1232,7 +1231,7 @@ class ContensisCli {
1232
1231
  );
1233
1232
  log.raw('');
1234
1233
  if (models?.length) {
1235
- this.HandleFormattingAndOutput(contentModelBackup, () => {
1234
+ await this.HandleFormattingAndOutput(contentModelBackup, () => {
1236
1235
  // print the content models to console
1237
1236
  for (const model of models) {
1238
1237
  const components = model.components?.length || 0;
@@ -1279,7 +1278,7 @@ class ContensisCli {
1279
1278
  const { currentProject, log, messages } = this;
1280
1279
 
1281
1280
  const fileData = fromFile
1282
- ? readJsonFile<(ContentType | Component)[]>(fromFile) || []
1281
+ ? (await readFileAsJSON<(ContentType | Component)[]>(fromFile)) || []
1283
1282
  : [];
1284
1283
  if (typeof fileData === 'string')
1285
1284
  throw new Error(`Import file format must be of type JSON`);
@@ -1302,7 +1301,7 @@ class ContensisCli {
1302
1301
 
1303
1302
  if (migrateErr) logError(migrateErr);
1304
1303
  else
1305
- this.HandleFormattingAndOutput(result, () => {
1304
+ await this.HandleFormattingAndOutput(result, () => {
1306
1305
  // print the results to console
1307
1306
  if (!commit) {
1308
1307
  log.raw(log.boldText(`\nContent types:`));
@@ -1342,7 +1341,7 @@ class ContensisCli {
1342
1341
 
1343
1342
  if (Array.isArray(contentTypes)) {
1344
1343
  log.success(messages.contenttypes.list(currentProject));
1345
- this.HandleFormattingAndOutput(contentTypes, () => {
1344
+ await this.HandleFormattingAndOutput(contentTypes, () => {
1346
1345
  // print the content types to console
1347
1346
  for (const contentType of contentTypes) {
1348
1347
  const fieldsLength = contentType.fields?.length || 0;
@@ -1373,7 +1372,7 @@ class ContensisCli {
1373
1372
  messages.contenttypes.get(currentProject, contentType.id)
1374
1373
  );
1375
1374
  // print the content type to console
1376
- this.HandleFormattingAndOutput(contentType, log.object);
1375
+ await this.HandleFormattingAndOutput(contentType, log.object);
1377
1376
  } else {
1378
1377
  log.error(
1379
1378
  messages.contenttypes.failedGet(currentProject, contentTypeId)
@@ -1409,7 +1408,7 @@ class ContensisCli {
1409
1408
  )
1410
1409
  );
1411
1410
  // print the results to console
1412
- this.HandleFormattingAndOutput(result, () =>
1411
+ await this.HandleFormattingAndOutput(result, () =>
1413
1412
  log.object(jsonFormatter(result))
1414
1413
  );
1415
1414
  }
@@ -1428,7 +1427,9 @@ class ContensisCli {
1428
1427
  ) => {
1429
1428
  const { currentProject, log, messages } = this;
1430
1429
 
1431
- let fileData = fromFile ? readJsonFile<ContentType[]>(fromFile) || [] : [];
1430
+ let fileData = fromFile
1431
+ ? (await readFileAsJSON<ContentType[]>(fromFile)) || []
1432
+ : [];
1432
1433
  if (typeof fileData === 'string')
1433
1434
  throw new Error(`Import file format must be of type JSON`);
1434
1435
 
@@ -1460,7 +1461,7 @@ class ContensisCli {
1460
1461
  )
1461
1462
  );
1462
1463
  // print the content type to console
1463
- this.HandleFormattingAndOutput(contentType, () => {});
1464
+ await this.HandleFormattingAndOutput(contentType, () => {});
1464
1465
  }
1465
1466
  }
1466
1467
  }
@@ -1476,7 +1477,9 @@ class ContensisCli {
1476
1477
  ) => {
1477
1478
  const { log } = this;
1478
1479
 
1479
- let fileData = fromFile ? readJsonFile<ContentType[]>(fromFile) || [] : [];
1480
+ let fileData = fromFile
1481
+ ? (await readFileAsJSON<ContentType[]>(fromFile)) || []
1482
+ : [];
1480
1483
  if (typeof fileData === 'string')
1481
1484
  throw new Error(`Import file format must be of type JSON`);
1482
1485
 
@@ -1495,7 +1498,7 @@ class ContensisCli {
1495
1498
  if (err) log.error(err.message, err);
1496
1499
  if (result)
1497
1500
  // print the content type to console
1498
- this.HandleFormattingAndOutput(result, () => {
1501
+ await this.HandleFormattingAndOutput(result, () => {
1499
1502
  log.success(
1500
1503
  `Queried models ${log.infoText(
1501
1504
  `"${result.query.modelIds?.join(', ')}"`
@@ -1523,7 +1526,7 @@ class ContensisCli {
1523
1526
  if (Array.isArray(components)) {
1524
1527
  log.success(messages.components.list(currentProject));
1525
1528
 
1526
- this.HandleFormattingAndOutput(components, () => {
1529
+ await this.HandleFormattingAndOutput(components, () => {
1527
1530
  // print the components to console
1528
1531
  for (const component of components) {
1529
1532
  const fieldsLength = component.fields?.length || 0;
@@ -1552,7 +1555,7 @@ class ContensisCli {
1552
1555
  if (component) {
1553
1556
  log.success(messages.components.get(currentProject, component.id));
1554
1557
  // print the component to console
1555
- this.HandleFormattingAndOutput(component, log.object);
1558
+ await this.HandleFormattingAndOutput(component, log.object);
1556
1559
  } else {
1557
1560
  log.error(messages.components.failedGet(currentProject, componentId));
1558
1561
  }
@@ -1589,7 +1592,7 @@ class ContensisCli {
1589
1592
  )
1590
1593
  );
1591
1594
  // print the results to console
1592
- this.HandleFormattingAndOutput(result, () =>
1595
+ await this.HandleFormattingAndOutput(result, () =>
1593
1596
  log.info(jsonFormatter(result))
1594
1597
  );
1595
1598
  }
@@ -1608,7 +1611,9 @@ class ContensisCli {
1608
1611
  ) => {
1609
1612
  const { currentProject, log, messages } = this;
1610
1613
 
1611
- let fileData = fromFile ? readJsonFile<Component[]>(fromFile) || [] : [];
1614
+ let fileData = fromFile
1615
+ ? (await readFileAsJSON<Component[]>(fromFile)) || []
1616
+ : [];
1612
1617
  if (typeof fileData === 'string')
1613
1618
  throw new Error(`Import file format must be of type JSON`);
1614
1619
 
@@ -1640,7 +1645,7 @@ class ContensisCli {
1640
1645
  )
1641
1646
  );
1642
1647
  // print the component to console
1643
- this.HandleFormattingAndOutput(component, () => {});
1648
+ await this.HandleFormattingAndOutput(component, () => {});
1644
1649
  }
1645
1650
  }
1646
1651
  }
@@ -1661,7 +1666,7 @@ class ContensisCli {
1661
1666
  }
1662
1667
  const [err, result] = await contensis.DeleteEntries();
1663
1668
  if (result)
1664
- this.HandleFormattingAndOutput(result, () => {
1669
+ await this.HandleFormattingAndOutput(result, () => {
1665
1670
  // print the migrateResult to console
1666
1671
  printEntriesMigrateResult(this, result, {
1667
1672
  action: 'delete',
@@ -1697,7 +1702,7 @@ class ContensisCli {
1697
1702
  if (contensis) {
1698
1703
  log.line();
1699
1704
  const entries = await contensis.GetEntries({ withDependents });
1700
- this.HandleFormattingAndOutput(entries, () =>
1705
+ await this.HandleFormattingAndOutput(entries, () =>
1701
1706
  // print the entries to console
1702
1707
  logEntitiesTable({
1703
1708
  entries,
@@ -1740,7 +1745,7 @@ class ContensisCli {
1740
1745
 
1741
1746
  if (err) logError(err);
1742
1747
  else
1743
- this.HandleFormattingAndOutput(result, () => {
1748
+ await this.HandleFormattingAndOutput(result, () => {
1744
1749
  // print the migrateResult to console
1745
1750
  printEntriesMigrateResult(this, result, {
1746
1751
  showAll: logOutput === 'all',
@@ -1795,7 +1800,7 @@ class ContensisCli {
1795
1800
 
1796
1801
  log.success(messages.nodes.get(currentProject, rootPath, depth));
1797
1802
 
1798
- this.HandleFormattingAndOutput(nodes, () => {
1803
+ await this.HandleFormattingAndOutput(nodes, () => {
1799
1804
  // print the nodes to console
1800
1805
  log.object({ ...root, children: undefined, language: undefined });
1801
1806
  printNodeTreeOutput(this, root);
@@ -1839,7 +1844,7 @@ class ContensisCli {
1839
1844
 
1840
1845
  if (err) log.raw(``);
1841
1846
  else
1842
- this.HandleFormattingAndOutput(result, () => {
1847
+ await this.HandleFormattingAndOutput(result, () => {
1843
1848
  // print the migrateResult to console
1844
1849
  printNodeTreeOutput(this, migrateTree, logOutput, logLimit);
1845
1850
  printNodesMigrateResult(this, result, {
@@ -1908,7 +1913,7 @@ class ContensisCli {
1908
1913
  }
1909
1914
  const [err, result] = await contensis.DeleteNodes();
1910
1915
  if (result) {
1911
- this.HandleFormattingAndOutput(result, () => {
1916
+ await this.HandleFormattingAndOutput(result, () => {
1912
1917
  // print the migrateResult to console
1913
1918
  printNodeTreeOutput(
1914
1919
  this,
@@ -1965,7 +1970,7 @@ class ContensisCli {
1965
1970
  log.success(messages.webhooks.list(currentEnv));
1966
1971
  if (!webhooks?.length) log.warning(messages.webhooks.noneExist());
1967
1972
  else {
1968
- this.HandleFormattingAndOutput(filteredResults, () => {
1973
+ await this.HandleFormattingAndOutput(filteredResults, () => {
1969
1974
  // print the keys to console
1970
1975
  for (const {
1971
1976
  id,
@@ -2043,7 +2048,7 @@ class ContensisCli {
2043
2048
  const [err, blocks] = await contensis.blocks.GetBlocks();
2044
2049
 
2045
2050
  if (Array.isArray(blocks)) {
2046
- this.HandleFormattingAndOutput(blocks, () => {
2051
+ await this.HandleFormattingAndOutput(blocks, () => {
2047
2052
  // print the blocks to console
2048
2053
  log.success(messages.blocks.list(currentEnv, env.currentProject));
2049
2054
  for (const {
@@ -2098,7 +2103,7 @@ class ContensisCli {
2098
2103
  );
2099
2104
 
2100
2105
  if (blocks) {
2101
- this.HandleFormattingAndOutput(blocks, () => {
2106
+ await this.HandleFormattingAndOutput(blocks, () => {
2102
2107
  // print the version detail to console
2103
2108
  log.success(
2104
2109
  messages.blocks.get(blockId, currentEnv, env.currentProject)
@@ -2159,7 +2164,7 @@ class ContensisCli {
2159
2164
  );
2160
2165
  }
2161
2166
  if (blockVersion) {
2162
- this.HandleFormattingAndOutput(blockVersion, () => {
2167
+ await this.HandleFormattingAndOutput(blockVersion, () => {
2163
2168
  // print the version detail to console
2164
2169
  printBlockVersion(this, blockVersion);
2165
2170
  });
@@ -2250,7 +2255,7 @@ class ContensisCli {
2250
2255
  );
2251
2256
 
2252
2257
  if (blockVersion) {
2253
- this.HandleFormattingAndOutput(blockVersion, () => {
2258
+ await this.HandleFormattingAndOutput(blockVersion, () => {
2254
2259
  // print the version detail to console
2255
2260
  log.success(
2256
2261
  messages.blocks.actionComplete(
@@ -2310,7 +2315,7 @@ class ContensisCli {
2310
2315
  logs.endsWith('\n') ? logs.slice(0, logs.length - 1) : logs;
2311
2316
  const renderLogs = removeTrailingNewline(blockLogs);
2312
2317
 
2313
- this.HandleFormattingAndOutput(renderLogs, () => {
2318
+ await this.HandleFormattingAndOutput(renderLogs, () => {
2314
2319
  // print the logs to console
2315
2320
  console.log(
2316
2321
  ` - ${blockId} ${branch} ${
@@ -2414,7 +2419,7 @@ class ContensisCli {
2414
2419
  ); // TODO: resolve any cast;
2415
2420
 
2416
2421
  if (Array.isArray(proxies)) {
2417
- this.HandleFormattingAndOutput(proxies, () => {
2422
+ await this.HandleFormattingAndOutput(proxies, () => {
2418
2423
  // print the proxies to console
2419
2424
  log.success(messages.proxies.list(currentEnv, env.currentProject));
2420
2425
  for (const { id, name, description, endpoints, version } of proxies) {
@@ -2453,7 +2458,7 @@ class ContensisCli {
2453
2458
  ); // TODO: resolve any cast
2454
2459
 
2455
2460
  if (Array.isArray(renderers)) {
2456
- this.HandleFormattingAndOutput(renderers, () => {
2461
+ await this.HandleFormattingAndOutput(renderers, () => {
2457
2462
  // print the renderers to console
2458
2463
  log.success(messages.renderers.list(currentEnv, env.currentProject));
2459
2464
  for (const {
@@ -2489,7 +2494,7 @@ class ContensisCli {
2489
2494
  }
2490
2495
  }
2491
2496
  };
2492
- HandleFormattingAndOutput = <T>(obj: T, logFn: (obj: T) => void) => {
2497
+ HandleFormattingAndOutput = async <T>(obj: T, logFn: (obj: T) => void) => {
2493
2498
  const { format, log, messages, output } = this;
2494
2499
  const fields = this.contensis?.payload.query?.fields;
2495
2500
 
@@ -2498,7 +2503,7 @@ class ContensisCli {
2498
2503
  logFn(obj);
2499
2504
  } else if (format === 'csv') {
2500
2505
  log.raw('');
2501
- log.raw(log.infoText(csvFormatter(limitFields(obj, fields))));
2506
+ log.raw(log.infoText(await csvFormatter(limitFields(obj, fields))));
2502
2507
  } else if (format === 'xml') {
2503
2508
  log.raw('');
2504
2509
  log.raw(log.infoText(xmlFormatter(limitFields(obj, fields))));
@@ -2512,7 +2517,7 @@ class ContensisCli {
2512
2517
  let writeString = '';
2513
2518
  const isText = !tryParse(obj) && typeof obj === 'string';
2514
2519
  if (format === 'csv') {
2515
- writeString = csvFormatter(limitFields(obj, fields));
2520
+ writeString = await csvFormatter(limitFields(obj, fields));
2516
2521
  } else if (format === 'xml') {
2517
2522
  writeString = xmlFormatter(limitFields(obj, fields));
2518
2523
  } else
package/src/shell.ts CHANGED
@@ -306,4 +306,4 @@ process.stdin.on('data', key => {
306
306
  }
307
307
  });
308
308
 
309
- // process.env.http_proxy = 'http://127.0.0.1:8888';
309
+ // // process.env.http_proxy = 'http://127.0.0.1:8888';
@@ -1,9 +1,10 @@
1
- import { Parser } from 'json2csv';
1
+ import { parse, stringify } from 'csv';
2
+ // import { parse, stringify } from 'csv/sync';
2
3
  import { flattenObject } from './json.formatter';
3
4
 
4
- export const csvFormatter = <T>(entries: T | T[]) => {
5
+ export const csvFormatter = async <T>(entries: T | T[]) => {
5
6
  // Flatten the passed in object
6
- const flatEntries = [];
7
+ const flatEntries = [] as any[];
7
8
  if (Array.isArray(entries))
8
9
  for (const entry of entries) {
9
10
  flatEntries.push(flattenObject(entry));
@@ -11,8 +12,76 @@ export const csvFormatter = <T>(entries: T | T[]) => {
11
12
  else flatEntries.push(flattenObject(entries));
12
13
 
13
14
  // Parse the flattened object to csv
14
- const json2csvParser = new Parser();
15
- const csv = json2csvParser.parse(flatEntries);
16
-
15
+ // const csv = stringify(flatEntries, { header: true });
16
+ const csv = await new Promise<string>((resolve, reject) => {
17
+ stringify(flatEntries, { header: true }, (err, data) => {
18
+ if (err) reject(err);
19
+ resolve(data);
20
+ });
21
+ });
17
22
  return csv;
18
23
  };
24
+
25
+ export const csvToJson = async <T = any>(data: string): Promise<T[]> => {
26
+ // return parse(data, {
27
+ // columns: true,
28
+ // skip_empty_lines: true,
29
+ // });
30
+ return new Promise((resolve, reject) => {
31
+ parse(
32
+ data,
33
+ {
34
+ columns: true,
35
+ skip_empty_lines: true,
36
+ },
37
+ (err, records) => {
38
+ if (err) reject(err);
39
+ resolve(records);
40
+ }
41
+ );
42
+ });
43
+ };
44
+
45
+ export const detectCsv = (
46
+ chunk: string,
47
+ opts?: { delimiters?: string[]; newlines?: string[] }
48
+ ) => {
49
+ opts = opts || {};
50
+ if (Buffer.isBuffer(chunk)) chunk = chunk + '';
51
+ const delimiters = opts.delimiters || [',', ';', '\t', '|'];
52
+ const newlines = opts.newlines || ['\n', '\r'];
53
+
54
+ const lines = chunk.split(/[\n\r]+/g);
55
+
56
+ const delimiter = determineMost(lines[0], delimiters);
57
+ const newline = determineMost(chunk, newlines);
58
+
59
+ if (!delimiter) return null;
60
+
61
+ return {
62
+ delimiter: delimiter,
63
+ newline: newline,
64
+ };
65
+ };
66
+
67
+ const determineMost = (chunk: string, items: string[]) => {
68
+ const itemCount = {} as any;
69
+ let ignoreString = false;
70
+ let maxValue = 0;
71
+ let maxChar;
72
+ let currValue;
73
+ items.forEach(item => {
74
+ itemCount[item] = 0;
75
+ });
76
+ for (var i = 0; i < chunk.length; i++) {
77
+ if (chunk[i] === '"') ignoreString = !ignoreString;
78
+ else if (!ignoreString && chunk[i] in itemCount) {
79
+ currValue = ++itemCount[chunk[i]];
80
+ if (currValue > maxValue) {
81
+ maxValue = currValue;
82
+ maxChar = chunk[i];
83
+ }
84
+ }
85
+ }
86
+ return maxChar;
87
+ };
@@ -9,6 +9,10 @@ export const jsonFormatter = <T>(obj: T, fields?: string[]) =>
9
9
  // nested object and the keys are presented like "sys.version.versionNo": "1.0"
10
10
  export const flattenObject = (obj: any) => flatten(cleaner(obj, ['workflow']));
11
11
 
12
+ // Unflatten a JSON object such as an entry so the arrays and
13
+ // nested objects are reconstructed - the opposite of flattenObject
14
+ export const unflattenObject = (obj: any) => unflatten(obj);
15
+
12
16
  // Will limit and sort an object's keys by an array of supplied fields
13
17
  export const limitFields = (obj: any, fields?: string[]): any => {
14
18
  if (!fields) return obj;
@@ -14,7 +14,13 @@ export const xmlFormatter = <T>(entries: T | T[]) => {
14
14
 
15
15
  return xml;
16
16
  } catch (ex) {
17
- Logger.error(`Problem building XML from entries`, ex);
17
+ Logger.error(`Problem building XML from json data`, ex);
18
18
  return '';
19
19
  }
20
20
  };
21
+
22
+ export const xmlToJson = async <T>(data: string) => {
23
+ const json = await xml2js.parseStringPromise(data, { explicitArray: false });
24
+
25
+ return json.Items?.Entry || json;
26
+ };
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const LIB_VERSION = "1.1.1-beta.0";
1
+ export const LIB_VERSION = "1.1.1-beta.10";