@teleporthq/teleport-shared 0.27.2 → 0.27.3-alpha.2

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 (48) hide show
  1. package/dist/cjs/constants/index.d.ts +3 -1
  2. package/dist/cjs/constants/index.d.ts.map +1 -1
  3. package/dist/cjs/constants/index.js +17 -1
  4. package/dist/cjs/constants/index.js.map +1 -1
  5. package/dist/cjs/index.d.ts +2 -1
  6. package/dist/cjs/index.d.ts.map +1 -1
  7. package/dist/cjs/index.js +3 -1
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  10. package/dist/cjs/utils/generic.d.ts +2 -0
  11. package/dist/cjs/utils/generic.d.ts.map +1 -0
  12. package/dist/cjs/utils/generic.js +61 -0
  13. package/dist/cjs/utils/generic.js.map +1 -0
  14. package/dist/cjs/utils/string-utils.d.ts +3 -0
  15. package/dist/cjs/utils/string-utils.d.ts.map +1 -1
  16. package/dist/cjs/utils/string-utils.js +9 -1
  17. package/dist/cjs/utils/string-utils.js.map +1 -1
  18. package/dist/cjs/utils/uidl-utils.d.ts +3 -1
  19. package/dist/cjs/utils/uidl-utils.d.ts.map +1 -1
  20. package/dist/cjs/utils/uidl-utils.js +254 -9
  21. package/dist/cjs/utils/uidl-utils.js.map +1 -1
  22. package/dist/esm/constants/index.d.ts +3 -1
  23. package/dist/esm/constants/index.d.ts.map +1 -1
  24. package/dist/esm/constants/index.js +16 -0
  25. package/dist/esm/constants/index.js.map +1 -1
  26. package/dist/esm/index.d.ts +2 -1
  27. package/dist/esm/index.d.ts.map +1 -1
  28. package/dist/esm/index.js +2 -1
  29. package/dist/esm/index.js.map +1 -1
  30. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  31. package/dist/esm/utils/generic.d.ts +2 -0
  32. package/dist/esm/utils/generic.d.ts.map +1 -0
  33. package/dist/esm/utils/generic.js +57 -0
  34. package/dist/esm/utils/generic.js.map +1 -0
  35. package/dist/esm/utils/string-utils.d.ts +3 -0
  36. package/dist/esm/utils/string-utils.d.ts.map +1 -1
  37. package/dist/esm/utils/string-utils.js +5 -0
  38. package/dist/esm/utils/string-utils.js.map +1 -1
  39. package/dist/esm/utils/uidl-utils.d.ts +3 -1
  40. package/dist/esm/utils/uidl-utils.d.ts.map +1 -1
  41. package/dist/esm/utils/uidl-utils.js +251 -8
  42. package/dist/esm/utils/uidl-utils.js.map +1 -1
  43. package/package.json +3 -3
  44. package/src/constants/index.ts +19 -1
  45. package/src/index.ts +2 -1
  46. package/src/utils/generic.ts +68 -0
  47. package/src/utils/string-utils.ts +5 -0
  48. package/src/utils/uidl-utils.ts +334 -17
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as Constants from './constants'
2
2
  import * as StringUtils from './utils/string-utils'
3
3
  import * as UIDLUtils from './utils/uidl-utils'
4
+ import * as GenericUtils from './utils/generic'
4
5
 
5
- export { Constants, StringUtils, UIDLUtils }
6
+ export { Constants, StringUtils, UIDLUtils, GenericUtils }
@@ -0,0 +1,68 @@
1
+ export const generateLocalDependenciesPrefix = (fromPath: string[], toPath: string[]): string => {
2
+ /*
3
+ Remove common path elements from the beginning of the
4
+ components and pages full path (if any)
5
+
6
+ For example, having:
7
+ - fromPath = ['src', 'components']
8
+ - toPath = ['src', 'pages']
9
+
10
+ If we want to have an import statement that goes from the pages folder to the
11
+ components folder, we only need to go back one step, so we are removing
12
+ the first element from both the paths ('src') and build the dependencyPrefix accordingly
13
+ */
14
+ const [firstPath, secondPath] = removeCommonStartingPointsFromPaths([fromPath, toPath])
15
+
16
+ // We have to go back as many folders as there are defined in the pages path
17
+ let dependencyPrefix = '../'.repeat(firstPath.length)
18
+
19
+ // if 'fromPath' is parent for 'toPath', the path starts from './'
20
+ if (firstPath.length === 0) {
21
+ secondPath.unshift('.')
22
+ }
23
+
24
+ dependencyPrefix += secondPath
25
+ .map((folder) => {
26
+ return `${folder}/`
27
+ })
28
+ .join('')
29
+
30
+ return dependencyPrefix
31
+ }
32
+
33
+ const removeCommonStartingPointsFromPaths = (paths: string[][]): string[][] => {
34
+ const pathsClone: string[][] = JSON.parse(JSON.stringify(paths))
35
+
36
+ const shortestPathLength = Math.min(
37
+ ...pathsClone.map((path) => {
38
+ return path.length
39
+ })
40
+ )
41
+
42
+ let elementIndex = 0
43
+ let elementsFromIndexAreEqual = true
44
+
45
+ while (elementIndex < shortestPathLength && elementsFromIndexAreEqual) {
46
+ const firstPathElementsFromIndex = pathsClone.map((path: string[]) => {
47
+ return path[0]
48
+ })
49
+
50
+ if (elementsFromArrayAreEqual(firstPathElementsFromIndex)) {
51
+ // If the first elements from every path are equal, remove it
52
+ pathsClone.forEach((path) => {
53
+ path.shift()
54
+ })
55
+ } else {
56
+ elementsFromIndexAreEqual = false
57
+ }
58
+ elementIndex += 1
59
+ }
60
+
61
+ return pathsClone
62
+ }
63
+
64
+ const elementsFromArrayAreEqual = (arrayOfElements: string[]): boolean => {
65
+ return arrayOfElements.every((element: string) => {
66
+ return element === arrayOfElements[0]
67
+ })
68
+ }
@@ -5,6 +5,7 @@ export const dashCaseToCamelCase = (str: string): string =>
5
5
  str.replace(/[-_]+(.)?/g, (_, chr) => (chr ? chr.toUpperCase() : ''))
6
6
 
7
7
  export const capitalize = (str: string): string => str[0].toUpperCase() + str.slice(1)
8
+ export const camelize = (str: string): string => str[0].toLowerCase() + str.slice(1)
8
9
 
9
10
  export const dashCaseToUpperCamelCase = (str: string) => capitalize(dashCaseToCamelCase(str))
10
11
 
@@ -36,6 +37,10 @@ export const slugify = (str: string): string => {
36
37
  .replace(/&/g, '-and-') // Replace & with 'and'
37
38
  }
38
39
 
40
+ export const createStateOrPropStoringValue = (value: string) => camelize(dashCaseToCamelCase(value))
41
+ export const createStateStoringFunction = (value: string) =>
42
+ `set${capitalize(dashCaseToUpperCamelCase(value))}`
43
+
39
44
  export const addSpacesToEachLine = (spaces: string, str: string) => {
40
45
  // indent the first line
41
46
  const respaced = spaces + str
@@ -20,9 +20,12 @@ import {
20
20
  UIDLStyleSheetContent,
21
21
  UIDLComponentStyleReference,
22
22
  UIDLRootComponent,
23
+ ProjectContext,
24
+ Resource,
23
25
  GeneratorOptions,
24
26
  } from '@teleporthq/teleport-types'
25
27
  import { basename } from 'path'
28
+ import { StringUtils } from '..'
26
29
 
27
30
  export const extractRoutes = (rootComponent: UIDLRootComponent) => {
28
31
  // Assuming root element starts with a UIDLElementNode
@@ -141,7 +144,7 @@ export const prefixAssetsPath = (
141
144
  - asset is missing in the project packer
142
145
  - It's not a asset and so we don't need to provide any mapping for it
143
146
 
144
- Note: We need to check for decoded asset name as well as for some special characters such as katakana / kanjis / hiraganas
147
+ Note: We need to check for decoded asset name as well as for some special characters such as katakana / kanjis / hiraganas
145
148
  the src / url leading to the asset can be encoded and we need to check the decoded version against the asset mapping
146
149
  */
147
150
 
@@ -222,6 +225,29 @@ export const traverseNodes = (
222
225
  }
223
226
  break
224
227
 
228
+ case 'cms-list':
229
+ traverseNodes(node.content.nodes.success, fn)
230
+ if (node.content.nodes.error) {
231
+ traverseNodes(node.content.nodes.error, fn)
232
+ }
233
+ if (node.content.nodes.loading) {
234
+ traverseNodes(node.content.nodes.loading, fn)
235
+ }
236
+ if (node.content.nodes.empty) {
237
+ traverseNodes(node.content.nodes.empty, fn)
238
+ }
239
+ break
240
+
241
+ case 'cms-item':
242
+ traverseNodes(node.content.nodes.success, fn)
243
+ if (node.content.nodes.error) {
244
+ traverseNodes(node.content.nodes.error, fn)
245
+ }
246
+ if (node.content.nodes.loading) {
247
+ traverseNodes(node.content.nodes.loading, fn)
248
+ }
249
+ break
250
+
225
251
  case 'repeat':
226
252
  traverseNodes(node.content.node, fn, node)
227
253
  traverseNodes(node.content.dataSource, fn, node)
@@ -247,7 +273,72 @@ export const traverseNodes = (
247
273
 
248
274
  default:
249
275
  throw new Error(
250
- `traverseNodes was given an unsupported node type ${JSON.stringify(node, null, 2)}`
276
+ `traverseNodes was given an unsupported node type: ${JSON.stringify(node, null, 2)}`
277
+ )
278
+ }
279
+ }
280
+
281
+ export const traverseResources = (
282
+ node: UIDLNode,
283
+ fn: (node: Resource, parentNode: UIDLNode) => void
284
+ ) => {
285
+ switch (node.type) {
286
+ case 'element':
287
+ const { children } = node.content
288
+
289
+ if (children) {
290
+ children.forEach((child) => {
291
+ traverseResources(child, fn)
292
+ })
293
+ }
294
+ break
295
+
296
+ case 'cms-list':
297
+ traverseResources(node.content.nodes.success, fn)
298
+ if (node.content.nodes.error) {
299
+ traverseResources(node.content.nodes.error, fn)
300
+ }
301
+ if (node.content.nodes.loading) {
302
+ traverseResources(node.content.nodes.loading, fn)
303
+ }
304
+ if (node.content.nodes.empty) {
305
+ traverseResources(node.content.nodes.empty, fn)
306
+ }
307
+ break
308
+
309
+ case 'cms-item':
310
+ traverseResources(node.content.nodes.success, fn)
311
+ if (node.content.nodes.error) {
312
+ traverseResources(node.content.nodes.error, fn)
313
+ }
314
+ if (node.content.nodes.loading) {
315
+ traverseResources(node.content.nodes.loading, fn)
316
+ }
317
+ break
318
+
319
+ case 'repeat':
320
+ traverseResources(node.content.node, fn)
321
+ break
322
+
323
+ case 'conditional':
324
+ traverseResources(node.content.node, fn)
325
+ break
326
+
327
+ case 'slot':
328
+ if (node.content.fallback) {
329
+ traverseResources(node.content.fallback, fn)
330
+ }
331
+ break
332
+
333
+ case 'static':
334
+ case 'dynamic':
335
+ case 'import':
336
+ case 'raw':
337
+ break
338
+
339
+ default:
340
+ throw new Error(
341
+ `traverseResources was given an unsupported node type: ${JSON.stringify(node, null, 2)}`
251
342
  )
252
343
  }
253
344
  }
@@ -275,6 +366,29 @@ export const traverseElements = (node: UIDLNode, fn: (element: UIDLElement) => v
275
366
  }
276
367
  break
277
368
 
369
+ case 'cms-list':
370
+ traverseElements(node.content.nodes.success, fn)
371
+ if (node.content.nodes.error) {
372
+ traverseElements(node.content.nodes.error, fn)
373
+ }
374
+ if (node.content.nodes.loading) {
375
+ traverseElements(node.content.nodes.loading, fn)
376
+ }
377
+ if (node.content.nodes.empty) {
378
+ traverseElements(node.content.nodes.empty, fn)
379
+ }
380
+ break
381
+
382
+ case 'cms-item':
383
+ traverseElements(node.content.nodes.success, fn)
384
+ if (node.content.nodes.error) {
385
+ traverseElements(node.content.nodes.error, fn)
386
+ }
387
+ if (node.content.nodes.loading) {
388
+ traverseElements(node.content.nodes.loading, fn)
389
+ }
390
+ break
391
+
278
392
  case 'repeat':
279
393
  traverseElements(node.content.node, fn)
280
394
  break
@@ -296,7 +410,11 @@ export const traverseElements = (node: UIDLNode, fn: (element: UIDLElement) => v
296
410
 
297
411
  default:
298
412
  throw new Error(
299
- `traverseElements was given an unsupported node type ${JSON.stringify(node, null, 2)}`
413
+ `traverseElements was given an unsupported node type : ${node.type}, ${JSON.stringify(
414
+ node,
415
+ null,
416
+ 2
417
+ )}`
300
418
  )
301
419
  }
302
420
  }
@@ -312,6 +430,29 @@ export const traverseRepeats = (node: UIDLNode, fn: (element: UIDLRepeatContent)
312
430
 
313
431
  break
314
432
 
433
+ case 'cms-list':
434
+ traverseRepeats(node.content.nodes.success, fn)
435
+ if (node.content.nodes.error) {
436
+ traverseRepeats(node.content.nodes.error, fn)
437
+ }
438
+ if (node.content.nodes.loading) {
439
+ traverseRepeats(node.content.nodes.loading, fn)
440
+ }
441
+ if (node.content.nodes.empty) {
442
+ traverseRepeats(node.content.nodes.empty, fn)
443
+ }
444
+ break
445
+
446
+ case 'cms-item':
447
+ traverseRepeats(node.content.nodes.success, fn)
448
+ if (node.content.nodes.error) {
449
+ traverseRepeats(node.content.nodes.error, fn)
450
+ }
451
+ if (node.content.nodes.loading) {
452
+ traverseRepeats(node.content.nodes.loading, fn)
453
+ }
454
+ break
455
+
315
456
  case 'repeat':
316
457
  fn(node.content)
317
458
 
@@ -335,7 +476,11 @@ export const traverseRepeats = (node: UIDLNode, fn: (element: UIDLRepeatContent)
335
476
 
336
477
  default:
337
478
  throw new Error(
338
- `traverseRepeats was given an unsupported node type ${JSON.stringify(node, null, 2)}`
479
+ `traverseRepeats was given an unsupported node type: ${node.type} - ${JSON.stringify(
480
+ node,
481
+ null,
482
+ 2
483
+ )}`
339
484
  )
340
485
  }
341
486
  }
@@ -465,7 +610,7 @@ export const transformStringAssignmentToJson = (
465
610
  type: 'dynamic',
466
611
  content: {
467
612
  referenceType,
468
- id: path,
613
+ id: StringUtils.createStateOrPropStoringValue(path),
469
614
  },
470
615
  }
471
616
  }
@@ -492,13 +637,27 @@ export const transformStylesAssignmentsToJson = (
492
637
 
493
638
  if (!Array.isArray(styleContentAtKey) && entityType === 'object') {
494
639
  // if this value is already properly declared, make sure it is not
495
- const { type } = styleContentAtKey as Record<string, unknown>
640
+ const { type, content } = styleContentAtKey as UIDLStaticValue | UIDLDynamicReference
496
641
 
497
- if (['dynamic', 'static'].indexOf(type as string) !== -1) {
498
- acc[key] = styleContentAtKey as UIDLStyleValue
642
+ if (type === 'static') {
643
+ acc[key] = styleContentAtKey as UIDLStaticValue
499
644
  return acc
500
645
  }
501
646
 
647
+ if (type === 'dynamic') {
648
+ if (['state', 'prop'].includes(content?.referenceType)) {
649
+ acc[key] = {
650
+ type,
651
+ content: {
652
+ ...content,
653
+ id: StringUtils.createStateOrPropStoringValue(content.id),
654
+ },
655
+ }
656
+ } else {
657
+ acc[key] = styleContentAtKey as UIDLDynamicReference
658
+ }
659
+ }
660
+
502
661
  return acc
503
662
  }
504
663
 
@@ -533,11 +692,37 @@ export const transformAttributesAssignmentsToJson = (
533
692
  if (!Array.isArray(attributeContent) && entityType === 'object') {
534
693
  // if this value is already properly declared, make sure it is not
535
694
  const { type } = attributeContent as Record<string, unknown>
536
- if (['dynamic', 'static', 'import', 'comp-style', 'raw'].indexOf(type as string) !== -1) {
695
+ if (['static', 'import', 'raw'].indexOf(type as string) !== -1) {
537
696
  acc[key] = attributeContent as UIDLAttributeValue
538
697
  return acc
539
698
  }
540
699
 
700
+ if (type === 'comp-style') {
701
+ acc[key] = {
702
+ type: 'comp-style',
703
+ content: StringUtils.createStateOrPropStoringValue(
704
+ (attributeContent as UIDLComponentStyleReference).content
705
+ ),
706
+ }
707
+ return acc
708
+ }
709
+
710
+ const { content } = attributeContent as UIDLDynamicReference
711
+ if (type === 'dynamic') {
712
+ if (['state', 'prop'].includes(content?.referenceType)) {
713
+ acc[key] = {
714
+ type,
715
+ content: {
716
+ ...content,
717
+ id: StringUtils.createStateOrPropStoringValue(content.id),
718
+ },
719
+ }
720
+ } else {
721
+ acc[key] = attributeContent as UIDLAttributeValue
722
+ }
723
+ return acc
724
+ }
725
+
541
726
  throw new Error(
542
727
  `transformAttributesAssignmentsToJson encountered a style value that is not supported ${JSON.stringify(
543
728
  attributeContent,
@@ -546,14 +731,6 @@ export const transformAttributesAssignmentsToJson = (
546
731
  )}`
547
732
  )
548
733
  }
549
-
550
- throw new Error(
551
- `transformAttributesAssignmentsToJson encountered a style value that is not supported ${JSON.stringify(
552
- attributeContent,
553
- null,
554
- 2
555
- )}`
556
- )
557
734
  }, newStyleObject)
558
735
 
559
736
  return newStyleObject
@@ -596,6 +773,29 @@ export const removeChildNodes = (
596
773
  removeChildNodes(node.content.node, criteria)
597
774
  break
598
775
 
776
+ case 'cms-list':
777
+ removeChildNodes(node.content.nodes.success, criteria)
778
+ if (node.content.nodes.error) {
779
+ removeChildNodes(node.content.nodes.error, criteria)
780
+ }
781
+ if (node.content.nodes.loading) {
782
+ removeChildNodes(node.content.nodes.loading, criteria)
783
+ }
784
+ if (node.content.nodes.empty) {
785
+ removeChildNodes(node.content.nodes.empty, criteria)
786
+ }
787
+ break
788
+
789
+ case 'cms-item':
790
+ removeChildNodes(node.content.nodes.success, criteria)
791
+ if (node.content.nodes.error) {
792
+ removeChildNodes(node.content.nodes.error, criteria)
793
+ }
794
+ if (node.content.nodes.loading) {
795
+ removeChildNodes(node.content.nodes.loading, criteria)
796
+ }
797
+ break
798
+
599
799
  case 'conditional':
600
800
  removeChildNodes(node.content.node, criteria)
601
801
  break
@@ -632,3 +832,120 @@ export const extractExternalDependencies = (dependencies: Record<string, UIDLDep
632
832
  return acc
633
833
  }, {})
634
834
  }
835
+
836
+ export const extractContextDependenciesFromNode = (
837
+ node: UIDLNode,
838
+ projectContexts: Record<string, ProjectContext>,
839
+ foundDependencies: Record<string, ProjectContext> = {}
840
+ ) => {
841
+ switch (node.type) {
842
+ case 'element':
843
+ if (node.content.attrs) {
844
+ Object.keys(node.content.attrs).forEach((key) => {
845
+ const attr = node.content.attrs[key]
846
+ if (attr.type === 'dynamic' && attr.content.referenceType === 'ctx') {
847
+ const contextName = attr.content.ctxId || attr.content.id
848
+ if (projectContexts[contextName]) {
849
+ foundDependencies[contextName] = projectContexts[contextName]
850
+ }
851
+ }
852
+ })
853
+ }
854
+ if (node.content.children) {
855
+ node.content.children.forEach((child) => {
856
+ extractContextDependenciesFromNode(child, projectContexts, foundDependencies)
857
+ })
858
+ }
859
+ break
860
+
861
+ case 'cms-list':
862
+ extractContextDependenciesFromNode(
863
+ node.content.nodes.success,
864
+ projectContexts,
865
+ foundDependencies
866
+ )
867
+ if (node.content.nodes.error) {
868
+ extractContextDependenciesFromNode(
869
+ node.content.nodes.error,
870
+ projectContexts,
871
+ foundDependencies
872
+ )
873
+ }
874
+ if (node.content.nodes.loading) {
875
+ extractContextDependenciesFromNode(
876
+ node.content.nodes.loading,
877
+ projectContexts,
878
+ foundDependencies
879
+ )
880
+ }
881
+ if (node.content.nodes.empty) {
882
+ extractContextDependenciesFromNode(
883
+ node.content.nodes.empty,
884
+ projectContexts,
885
+ foundDependencies
886
+ )
887
+ }
888
+ break
889
+
890
+ case 'cms-item':
891
+ extractContextDependenciesFromNode(
892
+ node.content.nodes.success,
893
+ projectContexts,
894
+ foundDependencies
895
+ )
896
+ if (node.content.nodes.error) {
897
+ extractContextDependenciesFromNode(
898
+ node.content.nodes.error,
899
+ projectContexts,
900
+ foundDependencies
901
+ )
902
+ }
903
+ if (node.content.nodes.loading) {
904
+ extractContextDependenciesFromNode(
905
+ node.content.nodes.loading,
906
+ projectContexts,
907
+ foundDependencies
908
+ )
909
+ }
910
+ break
911
+
912
+ case 'repeat':
913
+ extractContextDependenciesFromNode(node.content.node, projectContexts, foundDependencies)
914
+ break
915
+
916
+ case 'conditional':
917
+ extractContextDependenciesFromNode(node.content.node, projectContexts, foundDependencies)
918
+ break
919
+
920
+ case 'slot':
921
+ if (node.content.fallback) {
922
+ extractContextDependenciesFromNode(
923
+ node.content.fallback,
924
+ projectContexts,
925
+ foundDependencies
926
+ )
927
+ }
928
+ break
929
+
930
+ case 'static':
931
+ case 'raw':
932
+ break
933
+
934
+ case 'dynamic':
935
+ if (node.content.referenceType === 'ctx') {
936
+ foundDependencies[node.content.id] = projectContexts[node.content.id]
937
+ }
938
+ break
939
+
940
+ default:
941
+ throw new Error(
942
+ `extractContextDependenciesFromNode was given an unsupported node type ${JSON.stringify(
943
+ node,
944
+ null,
945
+ 2
946
+ )}`
947
+ )
948
+ }
949
+
950
+ return foundDependencies
951
+ }