@likec4/generators 1.31.0 → 1.32.1

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 (40) hide show
  1. package/dist/d2/generate-d2.d.ts +3 -2
  2. package/dist/d2/generate-d2.js +2 -1
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.js +1 -0
  5. package/dist/mmd/generate-mmd.d.ts +3 -2
  6. package/dist/mmd/generate-mmd.js +2 -1
  7. package/dist/model/generate-aux.d.ts +2 -0
  8. package/dist/model/generate-aux.js +66 -0
  9. package/dist/model/generate-likec4-model.d.ts +2 -2
  10. package/dist/model/generate-likec4-model.js +8 -10
  11. package/dist/puml/generate-puml.d.ts +2 -0
  12. package/dist/puml/generate-puml.js +176 -0
  13. package/dist/puml/index.d.ts +1 -0
  14. package/dist/puml/index.js +1 -0
  15. package/dist/react/generate-react-types.d.ts +2 -2
  16. package/dist/react/generate-react-types.js +14 -73
  17. package/dist/views-data-ts/generate-views-data.d.ts +9 -0
  18. package/package.json +7 -7
  19. package/src/__mocks__/data.ts +83 -83
  20. package/src/d2/__snapshots__/generate-d2.spec.ts.snap +78 -0
  21. package/src/d2/generate-d2.spec.ts +91 -4
  22. package/src/d2/generate-d2.ts +20 -15
  23. package/src/index.ts +1 -0
  24. package/src/mmd/generate-mmd.spec.ts +12 -4
  25. package/src/mmd/generate-mmd.ts +25 -20
  26. package/src/model/__snapshots__/aux.generate-valid-code.snap +56 -0
  27. package/src/model/__snapshots__/{likec4-model.snap → likec4.computed-model.snap} +342 -165
  28. package/src/model/__snapshots__/likec4.parsed-model.snap +671 -0
  29. package/src/model/generate-aux.spec.ts +65 -0
  30. package/src/model/generate-aux.ts +72 -0
  31. package/src/model/generate-likec4-model.spec.ts +34 -8
  32. package/src/model/generate-likec4-model.ts +13 -14
  33. package/src/puml/__snapshots__/generate-puml.spec.ts.snap +184 -0
  34. package/src/puml/generate-puml.spec.ts +26 -0
  35. package/src/puml/generate-puml.ts +264 -0
  36. package/src/puml/index.ts +1 -0
  37. package/src/react/__snapshots__/valid-code.snap +111 -0
  38. package/src/react/generate-react-types.spec.ts +67 -0
  39. package/src/react/generate-react-types.ts +16 -77
  40. package/src/views-data-ts/generate-views-data.ts +18 -9
@@ -0,0 +1,111 @@
1
+ /* prettier-ignore-start */
2
+ /* eslint-disable */
3
+
4
+ /******************************************************************************
5
+ * This file was generated
6
+ * DO NOT EDIT MANUALLY!
7
+ ******************************************************************************/
8
+
9
+ import type { PropsWithChildren } from 'react'
10
+ import type { JSX } from 'react/jsx-runtime'
11
+ import type { LikeC4Model } from '@likec4/core/model'
12
+ import type { DiagramView } from '@likec4/core/types'
13
+ import type {
14
+ LikeC4ViewProps as GenericLikeC4ViewProps,
15
+ ReactLikeC4Props as GenericReactLikeC4Props
16
+ } from 'likec4/react'
17
+
18
+ import type { Aux, SpecAux } from '@likec4/core/types';
19
+
20
+ export type $Specs = SpecAux<
21
+ // Element kinds
22
+ | "actor"
23
+ | "component"
24
+ | "system",
25
+ // Deployment kinds
26
+ | "env"
27
+ | "vm",
28
+ // Relationship kinds
29
+ | "dislike"
30
+ | "like",
31
+ // Tags
32
+ | "tag1"
33
+ | "tag2",
34
+ // Metadata keys
35
+ | "key1"
36
+ | "key2"
37
+ >
38
+
39
+ export type $Aux = Aux<
40
+ "computed",
41
+ // Elements
42
+ | "alice"
43
+ | "bob"
44
+ | "cloud"
45
+ | "cloud.backend"
46
+ | "cloud.frontend"
47
+ | "cloud.backend.api"
48
+ | "cloud.backend.db",
49
+ // Deployments
50
+ | "dev"
51
+ | "prod"
52
+ | "dev.api"
53
+ | "dev.vm1"
54
+ | "prod.vm1"
55
+ | "prod.vm2",
56
+ // Views
57
+ | "view1"
58
+ | "view2",
59
+ // Project ID
60
+ "from-builder",
61
+ $Specs
62
+ >
63
+
64
+ export type $ElementId = $Aux['ElementId']
65
+ export type $DeploymentId = $Aux['DeploymentId']
66
+ export type $ViewId = $Aux['ViewId']
67
+
68
+ export type $ElementKind = $Aux['ElementKind']
69
+ export type $RelationKind = $Aux['RelationKind']
70
+ export type $DeploymentKind = $Aux['DeploymentKind']
71
+ export type $Tag = $Aux['Tag']
72
+ export type $Tags = readonly $Aux['Tag'][]
73
+ export type $MetadataKey = $Aux['MetadataKey']
74
+
75
+
76
+ declare function isLikeC4ViewId(value: unknown): value is $ViewId;
77
+
78
+ declare const likec4model: LikeC4Model<$Aux>;
79
+ declare function useLikeC4Model(): LikeC4Model<$Aux>;
80
+ declare function useLikeC4View(viewId: $ViewId): DiagramView<$Aux>;
81
+
82
+ declare function LikeC4ModelProvider(props: PropsWithChildren): JSX.Element;
83
+
84
+ type IconRendererProps = {
85
+ node: {
86
+ id: string
87
+ title: string
88
+ icon?: string | undefined
89
+ }
90
+ }
91
+ declare function RenderIcon(props: IconRendererProps): JSX.Element;
92
+
93
+ type LikeC4ViewProps = GenericLikeC4ViewProps<$Aux>;
94
+ declare function LikeC4View({viewId, ...props}: LikeC4ViewProps): JSX.Element;
95
+
96
+ type ReactLikeC4Props = GenericReactLikeC4Props<$Aux>
97
+ declare function ReactLikeC4({viewId, ...props}: ReactLikeC4Props): JSX.Element;
98
+
99
+ export {
100
+ type LikeC4ViewProps,
101
+ type ReactLikeC4Props,
102
+ isLikeC4ViewId,
103
+ useLikeC4Model,
104
+ useLikeC4View,
105
+ likec4model,
106
+ LikeC4ModelProvider,
107
+ LikeC4View,
108
+ RenderIcon,
109
+ ReactLikeC4
110
+ }
111
+ /* prettier-ignore-end */
@@ -0,0 +1,67 @@
1
+ import { Builder } from '@likec4/core/builder'
2
+ import { describe, it } from 'vitest'
3
+ import { generateReactTypes } from './generate-react-types'
4
+
5
+ describe('generateReactTypes', () => {
6
+ const m = Builder
7
+ .specification({
8
+ elements: {
9
+ actor: {},
10
+ system: {},
11
+ component: {},
12
+ },
13
+ deployments: {
14
+ env: {},
15
+ vm: {},
16
+ },
17
+ relationships: {
18
+ like: {},
19
+ dislike: {},
20
+ },
21
+ tags: {
22
+ tag1: {},
23
+ tag2: {},
24
+ },
25
+ metadataKeys: ['key1', 'key2'],
26
+ })
27
+ .model(({ actor, system, component, relTo }, _) =>
28
+ _(
29
+ actor('alice'),
30
+ actor('bob'),
31
+ system('cloud').with(
32
+ component('backend').with(
33
+ component('api'),
34
+ component('db', {
35
+ tags: ['tag2'],
36
+ }),
37
+ ),
38
+ component('frontend'),
39
+ ),
40
+ )
41
+ )
42
+ .deployment(({ env, vm, instanceOf }, _) =>
43
+ _(
44
+ env('prod').with(
45
+ vm('vm1'),
46
+ vm('vm2'),
47
+ ),
48
+ env('dev').with(
49
+ vm('vm1'),
50
+ instanceOf('cloud.backend.api'),
51
+ ),
52
+ )
53
+ )
54
+ // Test Element View
55
+ .views(({ view, $include }, _) =>
56
+ _(
57
+ // rules inside
58
+ view('view1', $include('cloud.backend')),
59
+ view('view2', $include('cloud.*')),
60
+ )
61
+ )
62
+
63
+ it('generate valid code', async ({ expect }) => {
64
+ const aux = generateReactTypes(m.toLikeC4Model())
65
+ await expect(aux).toMatchFileSnapshot('__snapshots__/valid-code.snap')
66
+ })
67
+ })
@@ -1,45 +1,10 @@
1
- import type { LikeC4Model } from '@likec4/core'
2
- import { compareNatural, sortNaturalByFqn } from '@likec4/core'
1
+ import { invariant } from '@likec4/core'
2
+ import type { AnyLikeC4Model } from '@likec4/core/model'
3
+ import { generateAux } from '../model/generate-aux'
3
4
 
4
- function toUnion(unionset: string[] | Set<string>) {
5
- if (unionset instanceof Set) {
6
- unionset = [...unionset].sort(compareNatural)
7
- }
8
- const union = unionset.map(v => ` | ${JSON.stringify(v)}`)
9
- if (union.length === 0) {
10
- union.push(' never')
11
- }
12
- return union.join('\n') + ';'
13
- }
14
-
15
- export function generateReactTypes(model: LikeC4Model.Layouted) {
16
- const {
17
- fqns,
18
- tags,
19
- kinds,
20
- } = sortNaturalByFqn([...model.elements()]).reduce((acc, d) => {
21
- acc.fqns.push(d.id)
22
- acc.kinds.add(d.kind)
23
- acc.tags.push(...d.tags)
24
- return acc
25
- }, {
26
- fqns: [] as string[],
27
- kinds: new Set<string>(),
28
- tags: [] as string[],
29
- })
30
- const deploymentFqns = sortNaturalByFqn([...model.deployment.elements()]).map((e) => e.id)
31
-
32
- const {
33
- viewIds,
34
- } = [...model.views()].reduce((acc, d) => {
35
- acc.viewIds.add(d.id)
36
- acc.tags.push(...d.tags)
37
- acc.tags.push(...d.includedTags)
38
- return acc
39
- }, {
40
- viewIds: new Set<string>(),
41
- tags,
42
- })
5
+ export function generateReactTypes(model: AnyLikeC4Model) {
6
+ invariant(!model.isParsed(), 'can not generate react types for parsed model')
7
+ const aux = generateAux(model)
43
8
 
44
9
  return `
45
10
  /* prettier-ignore-start */
@@ -52,39 +17,20 @@ export function generateReactTypes(model: LikeC4Model.Layouted) {
52
17
 
53
18
  import type { PropsWithChildren } from 'react'
54
19
  import type { JSX } from 'react/jsx-runtime'
55
- import type {
56
- DiagramView,
57
- LikeC4Model as GenericLikeC4Model,
58
- } from 'likec4/model'
20
+ import type { LikeC4Model } from '@likec4/core/model'
21
+ import type { DiagramView } from '@likec4/core/types'
59
22
  import type {
60
23
  LikeC4ViewProps as GenericLikeC4ViewProps,
61
24
  ReactLikeC4Props as GenericReactLikeC4Props
62
25
  } from 'likec4/react'
63
26
 
64
- type LikeC4ElementId =
65
- ${toUnion(fqns)}
66
-
67
- type LikeC4DeploymentId =
68
- ${toUnion(deploymentFqns)}
69
-
70
- type LikeC4ViewId =
71
- ${toUnion(viewIds)}
72
-
73
- type LikeC4ElementKind =
74
- ${toUnion(kinds)}
75
-
76
- type LikeC4Tag =
77
- ${toUnion(new Set(tags))}
78
-
79
- declare function isLikeC4ViewId(value: unknown): value is LikeC4ViewId;
27
+ ${aux}
80
28
 
81
- type Aux = GenericLikeC4Model.Typed<LikeC4ElementId, LikeC4DeploymentId, LikeC4ViewId, DiagramView<LikeC4ViewId>>;
82
- type LikeC4Model = GenericLikeC4Model<Aux>;
83
- type LikeC4ViewModel = GenericLikeC4Model.View<Aux>;
29
+ declare function isLikeC4ViewId(value: unknown): value is $ViewId;
84
30
 
85
- declare const likeC4Model: LikeC4Model;
86
- declare function useLikeC4Model(): LikeC4Model;
87
- declare function useLikeC4View(viewId: LikeC4ViewId): DiagramView<LikeC4ViewId>;
31
+ declare const likec4model: LikeC4Model<$Aux>;
32
+ declare function useLikeC4Model(): LikeC4Model<$Aux>;
33
+ declare function useLikeC4View(viewId: $ViewId): DiagramView<$Aux>;
88
34
 
89
35
  declare function LikeC4ModelProvider(props: PropsWithChildren): JSX.Element;
90
36
 
@@ -97,26 +43,19 @@ type IconRendererProps = {
97
43
  }
98
44
  declare function RenderIcon(props: IconRendererProps): JSX.Element;
99
45
 
100
- type LikeC4ViewProps = GenericLikeC4ViewProps<LikeC4ViewId, LikeC4Tag, LikeC4ElementKind>;
46
+ type LikeC4ViewProps = GenericLikeC4ViewProps<$Aux>;
101
47
  declare function LikeC4View({viewId, ...props}: LikeC4ViewProps): JSX.Element;
102
48
 
103
- type ReactLikeC4Props = GenericReactLikeC4Props<LikeC4ViewId, LikeC4Tag, LikeC4ElementKind>
49
+ type ReactLikeC4Props = GenericReactLikeC4Props<$Aux>
104
50
  declare function ReactLikeC4({viewId, ...props}: ReactLikeC4Props): JSX.Element;
105
51
 
106
52
  export {
107
- type LikeC4ElementId,
108
- type LikeC4DeploymentId,
109
- type LikeC4ViewId,
110
- type LikeC4Tag,
111
- type LikeC4ElementKind,
112
53
  type LikeC4ViewProps,
113
54
  type ReactLikeC4Props,
114
- type LikeC4Model,
115
55
  isLikeC4ViewId,
116
56
  useLikeC4Model,
117
57
  useLikeC4View,
118
- useLikeC4ViewModel,
119
- likeC4Model,
58
+ likec4model,
120
59
  LikeC4ModelProvider,
121
60
  LikeC4View,
122
61
  RenderIcon,
@@ -3,6 +3,9 @@ import JSON5 from 'json5'
3
3
  import { CompositeGeneratorNode, expandToNode, joinToNode, NL, toString } from 'langium/generate'
4
4
  import { generateViewId } from './generateViewId'
5
5
 
6
+ /**
7
+ * Generate *.js file with views data
8
+ */
6
9
  export function generateViewsDataJs(diagrams: Iterable<DiagramView>) {
7
10
  const views = Array.from(diagrams)
8
11
  const out = new CompositeGeneratorNode()
@@ -33,11 +36,11 @@ export function generateViewsDataJs(diagrams: Iterable<DiagramView>) {
33
36
  view => expandToNode`${JSON5.stringify(view.id)}: ${JSON5.stringify(view)}`,
34
37
  {
35
38
  separator: ',',
36
- appendNewLineIfNotEmpty: true
37
- }
38
- )
39
+ appendNewLineIfNotEmpty: true,
40
+ },
41
+ ),
39
42
  )
40
- }
43
+ },
41
44
  })
42
45
  .append('}', NL, NL).appendTemplate`
43
46
 
@@ -54,6 +57,9 @@ export function generateViewsDataJs(diagrams: Iterable<DiagramView>) {
54
57
  return toString(out)
55
58
  }
56
59
 
60
+ /**
61
+ * Generate *.ts file with views data
62
+ */
57
63
  export function generateViewsDataTs(diagrams: Iterable<DiagramView>) {
58
64
  const views = Array.from(diagrams)
59
65
  const out = new CompositeGeneratorNode()
@@ -88,16 +94,16 @@ export function generateViewsDataTs(diagrams: Iterable<DiagramView>) {
88
94
  view =>
89
95
  expandToNode`${JSON5.stringify(view.id)}: (${
90
96
  JSON5.stringify(
91
- view
97
+ view,
92
98
  )
93
99
  } as unknown) as DiagramView`,
94
100
  {
95
101
  separator: ',',
96
- appendNewLineIfNotEmpty: true
97
- }
98
- )
102
+ appendNewLineIfNotEmpty: true,
103
+ },
104
+ ),
99
105
  )
100
- }
106
+ },
101
107
  })
102
108
  .append('} as const satisfies Record<LikeC4ViewId, DiagramView>', NL, NL).appendTemplate`
103
109
  export type LikeC4Views = typeof LikeC4Views
@@ -115,6 +121,9 @@ export function generateViewsDataTs(diagrams: Iterable<DiagramView>) {
115
121
  return toString(out)
116
122
  }
117
123
 
124
+ /**
125
+ * Generate *.d.ts
126
+ */
118
127
  export function generateViewsDataDTs(diagrams: Iterable<DiagramView>) {
119
128
  const views = Array.from(diagrams)
120
129
  const out = new CompositeGeneratorNode()