@promptui-lib/codegen 0.1.10 → 0.1.11

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.
@@ -1 +1 @@
1
- {"version":3,"file":"antd.template.d.ts","sourceRoot":"","sources":["../../src/frameworks/antd.template.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAmD,MAAM,oBAAoB,CAAC;AA4LzG;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAiChE;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAoB9E;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAwC7D"}
1
+ {"version":3,"file":"antd.template.d.ts","sourceRoot":"","sources":["../../src/frameworks/antd.template.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAkE,MAAM,oBAAoB,CAAC;AAuMxH;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAiChE;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAoB9E;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAwC7D"}
@@ -84,6 +84,12 @@ function stylesToInline(styles) {
84
84
  function isTextNode(child) {
85
85
  return 'type' in child && child.type === 'text';
86
86
  }
87
+ /**
88
+ * Check if a child is a component node
89
+ */
90
+ function isComponentNode(child) {
91
+ return 'type' in child && child.type === 'component';
92
+ }
87
93
  /**
88
94
  * Check if a child is a JSX node
89
95
  */
@@ -152,6 +158,10 @@ function generateJSX(node, indent = 2) {
152
158
  if (isTextNode(child)) {
153
159
  return `${spaces} ${child.value}`;
154
160
  }
161
+ if (isComponentNode(child)) {
162
+ // Render child component as <ComponentName />
163
+ return `${spaces} <${child.componentName} />`;
164
+ }
155
165
  if (isJSXNode(child)) {
156
166
  return generateJSX(child, indent + 2);
157
167
  }
@@ -1 +1 @@
1
- {"version":3,"file":"tsx-generator.d.ts","sourceRoot":"","sources":["../../src/generators/tsx-generator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,aAAa,EAMd,MAAM,oBAAoB,CAAC;AAkI5B;;GAEG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CA4BtD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAUxD"}
1
+ {"version":3,"file":"tsx-generator.d.ts","sourceRoot":"","sources":["../../src/generators/tsx-generator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,aAAa,EAQd,MAAM,oBAAoB,CAAC;AA0O5B;;GAEG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAmCtD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAUxD"}
@@ -2,6 +2,64 @@
2
2
  * TSX Generator
3
3
  * Gera código TypeScript/JSX a partir do AST do componente
4
4
  */
5
+ /**
6
+ * Coleta todos os componentes filhos de uma árvore JSX recursivamente
7
+ */
8
+ function collectChildComponents(node) {
9
+ const components = [];
10
+ for (const child of node.children) {
11
+ if ('type' in child && child.type === 'component') {
12
+ components.push(child);
13
+ }
14
+ else if ('tag' in child) {
15
+ // Recursivamente busca em nós JSX
16
+ components.push(...collectChildComponents(child));
17
+ }
18
+ }
19
+ return components;
20
+ }
21
+ /**
22
+ * Calcula o caminho relativo de import entre duas camadas do Atomic Design
23
+ */
24
+ function getRelativeImportPath(fromLayer, toLayer, toFileName) {
25
+ // Mapeamento de camadas para profundidade relativa
26
+ const layerDepth = {
27
+ atoms: 0,
28
+ molecules: 1,
29
+ organisms: 2,
30
+ };
31
+ const fromDepth = layerDepth[fromLayer];
32
+ const toDepth = layerDepth[toLayer];
33
+ // Se estão na mesma camada
34
+ if (fromLayer === toLayer) {
35
+ return `../${toFileName}`;
36
+ }
37
+ // Calcula o caminho relativo
38
+ // De organisms para atoms: ../../atoms/button
39
+ // De molecules para atoms: ../../atoms/button
40
+ // De organisms para molecules: ../../molecules/header
41
+ return `../../${toLayer}/${toFileName}`;
42
+ }
43
+ /**
44
+ * Gera imports de componentes filhos
45
+ */
46
+ function generateComponentImports(components, parentLayer) {
47
+ const imports = [];
48
+ const seen = new Set();
49
+ for (const comp of components) {
50
+ // Evita duplicatas
51
+ if (seen.has(comp.componentName)) {
52
+ continue;
53
+ }
54
+ seen.add(comp.componentName);
55
+ const importPath = getRelativeImportPath(parentLayer, comp.layer, comp.fileName);
56
+ imports.push({
57
+ from: importPath,
58
+ named: [comp.componentName],
59
+ });
60
+ }
61
+ return imports;
62
+ }
5
63
  /**
6
64
  * Gera string de imports
7
65
  */
@@ -55,6 +113,29 @@ function generatePropsDestructuring(props) {
55
113
  });
56
114
  return `{\n ${parts.join(',\n ')},\n}`;
57
115
  }
116
+ /**
117
+ * Gera JSX de um componente filho (self-closing)
118
+ */
119
+ function generateComponentJSX(component, indent) {
120
+ const spaces = ' '.repeat(indent);
121
+ let jsx = `${spaces}<${component.componentName}`;
122
+ // Adiciona props se existirem
123
+ if (component.props) {
124
+ for (const [key, value] of Object.entries(component.props)) {
125
+ if (typeof value === 'boolean') {
126
+ jsx += value ? ` ${key}` : '';
127
+ }
128
+ else if (typeof value === 'object' && value.type === 'expression') {
129
+ jsx += ` ${key}={${value.value}}`;
130
+ }
131
+ else {
132
+ jsx += ` ${key}="${value}"`;
133
+ }
134
+ }
135
+ }
136
+ jsx += ' />';
137
+ return jsx;
138
+ }
58
139
  /**
59
140
  * Gera JSX de um node
60
141
  */
@@ -103,6 +184,10 @@ function generateJSX(node, indent = 2) {
103
184
  else if ('type' in child && child.type === 'expression') {
104
185
  childrenJSX.push(`${childSpaces}{${child.value}}`);
105
186
  }
187
+ else if ('type' in child && child.type === 'component') {
188
+ // Renderiza componente filho como <ComponentName />
189
+ childrenJSX.push(generateComponentJSX(child, indent + 2));
190
+ }
106
191
  else {
107
192
  childrenJSX.push(generateJSX(child, indent + 2));
108
193
  }
@@ -117,8 +202,13 @@ function generateJSX(node, indent = 2) {
117
202
  */
118
203
  export function generateTSX(ast) {
119
204
  const lines = [];
205
+ // Coleta componentes filhos para gerar imports
206
+ const childComponents = collectChildComponents(ast.jsx);
207
+ const componentImports = generateComponentImports(childComponents, ast.layer);
208
+ // Combina imports existentes com imports de componentes
209
+ const allImports = [...(ast.imports ?? []), ...componentImports];
120
210
  // Imports
121
- const importsCode = generateImports(ast.imports ?? []);
211
+ const importsCode = generateImports(allImports);
122
212
  if (importsCode) {
123
213
  lines.push(importsCode);
124
214
  lines.push('');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptui-lib/codegen",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "private": false,
5
5
  "description": "Code generator for PromptUI - generates React TSX and SCSS",
6
6
  "license": "UNLICENSED",
@@ -30,7 +30,7 @@
30
30
  "dist"
31
31
  ],
32
32
  "dependencies": {
33
- "@promptui-lib/core": "0.1.10"
33
+ "@promptui-lib/core": "0.1.11"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/node": "^20.0.0",