@codeyam/codeyam-cli 0.1.0-staging.ad31e3e → 0.1.0-staging.bbe4da9

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 (161) hide show
  1. package/analyzer-template/.build-info.json +7 -7
  2. package/analyzer-template/log.txt +3 -3
  3. package/analyzer-template/packages/ai/index.ts +4 -1
  4. package/analyzer-template/packages/ai/src/lib/astScopes/processExpression.ts +18 -0
  5. package/analyzer-template/packages/ai/src/lib/dataStructure/ScopeDataStructure.ts +56 -0
  6. package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/cleanNonObjectFunctions.ts +64 -1
  7. package/analyzer-template/packages/ai/src/lib/worker/SerializableDataStructure.ts +2 -0
  8. package/analyzer-template/packages/analyze/src/lib/FileAnalyzer.ts +29 -2
  9. package/analyzer-template/packages/analyze/src/lib/asts/nodes/getNodeType.ts +1 -1
  10. package/analyzer-template/packages/analyze/src/lib/asts/sourceFiles/getAllExportedNodes.ts +4 -2
  11. package/analyzer-template/packages/analyze/src/lib/asts/sourceFiles/getAllExports.ts +5 -3
  12. package/analyzer-template/packages/analyze/src/lib/files/analyzeRemixRoute.ts +21 -33
  13. package/analyzer-template/packages/analyze/src/lib/files/getImportedExports.ts +75 -10
  14. package/analyzer-template/packages/analyze/src/lib/files/scenarios/gatherDataForMocks.ts +26 -0
  15. package/analyzer-template/packages/analyze/src/lib/files/scenarios/mergeValidatedDataStructures.ts +12 -2
  16. package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.d.ts.map +1 -1
  17. package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.js +2 -2
  18. package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.js.map +1 -1
  19. package/analyzer-template/packages/aws/src/lib/s3/getPresignedUrl.ts +2 -2
  20. package/analyzer-template/packages/github/dist/supabase/src/lib/scenarioToDb.d.ts.map +1 -1
  21. package/analyzer-template/packages/github/dist/supabase/src/lib/scenarioToDb.js +1 -1
  22. package/analyzer-template/packages/github/dist/supabase/src/lib/scenarioToDb.js.map +1 -1
  23. package/analyzer-template/packages/github/dist/utils/src/lib/applyUniversalMocks.d.ts.map +1 -1
  24. package/analyzer-template/packages/github/dist/utils/src/lib/applyUniversalMocks.js +39 -5
  25. package/analyzer-template/packages/github/dist/utils/src/lib/applyUniversalMocks.js.map +1 -1
  26. package/analyzer-template/packages/github/dist/utils/src/lib/lightweightEntityExtractor.d.ts.map +1 -1
  27. package/analyzer-template/packages/github/dist/utils/src/lib/lightweightEntityExtractor.js +17 -0
  28. package/analyzer-template/packages/github/dist/utils/src/lib/lightweightEntityExtractor.js.map +1 -1
  29. package/analyzer-template/packages/supabase/src/lib/scenarioToDb.ts +1 -0
  30. package/analyzer-template/packages/utils/dist/utils/src/lib/applyUniversalMocks.d.ts.map +1 -1
  31. package/analyzer-template/packages/utils/dist/utils/src/lib/applyUniversalMocks.js +39 -5
  32. package/analyzer-template/packages/utils/dist/utils/src/lib/applyUniversalMocks.js.map +1 -1
  33. package/analyzer-template/packages/utils/dist/utils/src/lib/lightweightEntityExtractor.d.ts.map +1 -1
  34. package/analyzer-template/packages/utils/dist/utils/src/lib/lightweightEntityExtractor.js +17 -0
  35. package/analyzer-template/packages/utils/dist/utils/src/lib/lightweightEntityExtractor.js.map +1 -1
  36. package/analyzer-template/packages/utils/src/lib/applyUniversalMocks.ts +46 -7
  37. package/analyzer-template/packages/utils/src/lib/lightweightEntityExtractor.ts +16 -0
  38. package/analyzer-template/project/constructMockCode.ts +29 -3
  39. package/analyzer-template/project/runMultiScenarioServer.ts +0 -4
  40. package/analyzer-template/project/runScenarioServer.ts +0 -4
  41. package/analyzer-template/project/start.ts +1 -11
  42. package/analyzer-template/project/startServer.ts +50 -70
  43. package/analyzer-template/project/writeMockDataTsx.ts +66 -3
  44. package/analyzer-template/project/writeScenarioComponents.ts +451 -25
  45. package/analyzer-template/scripts/postbuild.cjs +12 -1
  46. package/background/src/lib/virtualized/project/constructMockCode.js +25 -4
  47. package/background/src/lib/virtualized/project/constructMockCode.js.map +1 -1
  48. package/background/src/lib/virtualized/project/runMultiScenarioServer.js +0 -3
  49. package/background/src/lib/virtualized/project/runMultiScenarioServer.js.map +1 -1
  50. package/background/src/lib/virtualized/project/start.js +1 -8
  51. package/background/src/lib/virtualized/project/start.js.map +1 -1
  52. package/background/src/lib/virtualized/project/startServer.js +40 -68
  53. package/background/src/lib/virtualized/project/startServer.js.map +1 -1
  54. package/background/src/lib/virtualized/project/writeMockDataTsx.js +61 -3
  55. package/background/src/lib/virtualized/project/writeMockDataTsx.js.map +1 -1
  56. package/background/src/lib/virtualized/project/writeScenarioComponents.js +296 -20
  57. package/background/src/lib/virtualized/project/writeScenarioComponents.js.map +1 -1
  58. package/codeyam-cli/src/commands/debug.js +3 -2
  59. package/codeyam-cli/src/commands/debug.js.map +1 -1
  60. package/codeyam-cli/src/commands/setup-sandbox.js +2 -1
  61. package/codeyam-cli/src/commands/setup-sandbox.js.map +1 -1
  62. package/codeyam-cli/src/commands/test-startup.js +14 -5
  63. package/codeyam-cli/src/commands/test-startup.js.map +1 -1
  64. package/codeyam-cli/src/utils/analysisRunner.js +2 -1
  65. package/codeyam-cli/src/utils/analysisRunner.js.map +1 -1
  66. package/codeyam-cli/src/utils/analyzer.js +8 -16
  67. package/codeyam-cli/src/utils/analyzer.js.map +1 -1
  68. package/codeyam-cli/src/utils/generateReport.js +12 -6
  69. package/codeyam-cli/src/utils/generateReport.js.map +1 -1
  70. package/codeyam-cli/src/utils/queue/job.js +5 -4
  71. package/codeyam-cli/src/utils/queue/job.js.map +1 -1
  72. package/codeyam-cli/src/utils/sandbox.js +190 -0
  73. package/codeyam-cli/src/utils/sandbox.js.map +1 -0
  74. package/codeyam-cli/src/webserver/build/client/assets/{EntityTypeIcon-D5ZHFomX.js → EntityTypeIcon-Dp_FTAs1.js} +1 -1
  75. package/codeyam-cli/src/webserver/build/client/assets/InteractivePreview-TlHocYno.js +26 -0
  76. package/codeyam-cli/src/webserver/build/client/assets/{LibraryFunctionPreview-BYVx9KFp.js → LibraryFunctionPreview-CVMmGuIc.js} +1 -1
  77. package/codeyam-cli/src/webserver/build/client/assets/{LogViewer-CRcT5fOZ.js → LogViewer-JkfQ-VaI.js} +1 -1
  78. package/codeyam-cli/src/webserver/build/client/assets/ReportIssueModal-Cqce0_KG.js +1 -0
  79. package/codeyam-cli/src/webserver/build/client/assets/{SafeScreenshot-Bual6h18.js → SafeScreenshot-BrMAP1nP.js} +1 -1
  80. package/codeyam-cli/src/webserver/build/client/assets/ScenarioPreview-Bi-__7HT.js +6 -0
  81. package/codeyam-cli/src/webserver/build/client/assets/{ScenarioViewer-4D2vLLJz.js → ScenarioViewer-XmIpHcLJ.js} +1 -1
  82. package/codeyam-cli/src/webserver/build/client/assets/_index-BmfhU6CA.js +1 -0
  83. package/codeyam-cli/src/webserver/build/client/assets/activity.(_tab)-Dm8lM73z.js +10 -0
  84. package/codeyam-cli/src/webserver/build/client/assets/{chart-column-B8fb6wnw.js → chart-column-kA4jn9if.js} +1 -1
  85. package/codeyam-cli/src/webserver/build/client/assets/{chunk-WWGJGFF6-De6i8FUT.js → chunk-WWGJGFF6-CgXbbZRx.js} +1 -1
  86. package/codeyam-cli/src/webserver/build/client/assets/{circle-check-BACUUf75.js → circle-check-B2oHQ-zo.js} +1 -1
  87. package/codeyam-cli/src/webserver/build/client/assets/{clock-vWeoCemX.js → clock-BAfbP_iK.js} +1 -1
  88. package/codeyam-cli/src/webserver/build/client/assets/codeyam-name-logo-CvKwUgHo.svg +9 -0
  89. package/codeyam-cli/src/webserver/build/client/assets/{createLucideIcon-CS7XDrKv.js → createLucideIcon-BBYuR56H.js} +1 -1
  90. package/codeyam-cli/src/webserver/build/client/assets/{dev.empty-DIOEw_3i.js → dev.empty-BgPXZbm0.js} +1 -1
  91. package/codeyam-cli/src/webserver/build/client/assets/{entity._sha._-C6fctQ5v.js → entity._sha._-BkoAXaOa.js} +10 -10
  92. package/codeyam-cli/src/webserver/build/client/assets/{entity._sha_.create-scenario-C3FZJx1w.js → entity._sha_.create-scenario-Bj5GHkhb.js} +1 -1
  93. package/codeyam-cli/src/webserver/build/client/assets/{entity._sha_.edit._scenarioId-YJz_igar.js → entity._sha_.edit._scenarioId-eW5z9AyZ.js} +1 -1
  94. package/codeyam-cli/src/webserver/build/client/assets/{entityStatus-BEqj2qBy.js → entityStatus-C5Okl18j.js} +1 -1
  95. package/codeyam-cli/src/webserver/build/client/assets/{entityVersioning-Bk_YB1jM.js → entityVersioning-CU_Lchhc.js} +1 -1
  96. package/codeyam-cli/src/webserver/build/client/assets/{entry.client-DiP0q291.js → entry.client-B9tSboXM.js} +2 -2
  97. package/codeyam-cli/src/webserver/build/client/assets/{file-text-LM0mgxXE.js → file-text-18aYHZGd.js} +1 -1
  98. package/codeyam-cli/src/webserver/build/client/assets/files-Df79EyEb.js +1 -0
  99. package/codeyam-cli/src/webserver/build/client/assets/git-CDEwTVH_.js +12 -0
  100. package/codeyam-cli/src/webserver/build/client/assets/globals-DXRB6jBc.css +1 -0
  101. package/codeyam-cli/src/webserver/build/client/assets/{index-D-zYbzFZ.js → index-_LjBsTxX.js} +1 -1
  102. package/codeyam-cli/src/webserver/build/client/assets/{loader-circle-BXPKbHEb.js → loader-circle-D_EGChhq.js} +1 -1
  103. package/codeyam-cli/src/webserver/build/client/assets/manifest-3e0ffbcc.js +1 -0
  104. package/codeyam-cli/src/webserver/build/client/assets/{root-D0s7DnXb.js → root-CGyT4J4b.js} +3 -3
  105. package/codeyam-cli/src/webserver/build/client/assets/{settings-5zF_GOcS.js → settings-CEPbAsom.js} +1 -1
  106. package/codeyam-cli/src/webserver/build/client/assets/settings-R8QF_mHX.js +1 -0
  107. package/codeyam-cli/src/webserver/build/client/assets/simulations-B_PXvFom.js +1 -0
  108. package/codeyam-cli/src/webserver/build/client/assets/{triangle-alert-D7k-ArFa.js → triangle-alert-BthANBVv.js} +1 -1
  109. package/codeyam-cli/src/webserver/build/client/assets/{useLastLogLine-AlhS7g5F.js → useLastLogLine-Blr5oZDE.js} +1 -1
  110. package/codeyam-cli/src/webserver/build/client/assets/useReportContext-CANr3QJ5.js +1 -0
  111. package/codeyam-cli/src/webserver/build/client/assets/{useToast-Ddo4UQv7.js → useToast-Bbf4Hokd.js} +1 -1
  112. package/codeyam-cli/src/webserver/build/server/assets/{index-iaMjuNME.js → index-vf1FETCO.js} +1 -1
  113. package/codeyam-cli/src/webserver/build/server/assets/server-build-B5s58TvB.js +169 -0
  114. package/codeyam-cli/src/webserver/build/server/index.js +1 -1
  115. package/codeyam-cli/src/webserver/build-info.json +5 -5
  116. package/package.json +1 -1
  117. package/packages/ai/index.js +1 -1
  118. package/packages/ai/index.js.map +1 -1
  119. package/packages/ai/src/lib/astScopes/processExpression.js +12 -0
  120. package/packages/ai/src/lib/astScopes/processExpression.js.map +1 -1
  121. package/packages/ai/src/lib/dataStructure/ScopeDataStructure.js +38 -0
  122. package/packages/ai/src/lib/dataStructure/ScopeDataStructure.js.map +1 -1
  123. package/packages/ai/src/lib/dataStructure/helpers/cleanNonObjectFunctions.js +55 -1
  124. package/packages/ai/src/lib/dataStructure/helpers/cleanNonObjectFunctions.js.map +1 -1
  125. package/packages/ai/src/lib/worker/SerializableDataStructure.js.map +1 -1
  126. package/packages/analyze/src/lib/FileAnalyzer.js +22 -2
  127. package/packages/analyze/src/lib/FileAnalyzer.js.map +1 -1
  128. package/packages/analyze/src/lib/asts/nodes/getNodeType.js +1 -1
  129. package/packages/analyze/src/lib/asts/nodes/getNodeType.js.map +1 -1
  130. package/packages/analyze/src/lib/asts/sourceFiles/getAllExportedNodes.js +3 -2
  131. package/packages/analyze/src/lib/asts/sourceFiles/getAllExportedNodes.js.map +1 -1
  132. package/packages/analyze/src/lib/asts/sourceFiles/getAllExports.js +4 -3
  133. package/packages/analyze/src/lib/asts/sourceFiles/getAllExports.js.map +1 -1
  134. package/packages/analyze/src/lib/files/analyzeRemixRoute.js +18 -23
  135. package/packages/analyze/src/lib/files/analyzeRemixRoute.js.map +1 -1
  136. package/packages/analyze/src/lib/files/getImportedExports.js +56 -4
  137. package/packages/analyze/src/lib/files/getImportedExports.js.map +1 -1
  138. package/packages/analyze/src/lib/files/scenarios/gatherDataForMocks.js +24 -0
  139. package/packages/analyze/src/lib/files/scenarios/gatherDataForMocks.js.map +1 -1
  140. package/packages/analyze/src/lib/files/scenarios/mergeValidatedDataStructures.js +8 -2
  141. package/packages/analyze/src/lib/files/scenarios/mergeValidatedDataStructures.js.map +1 -1
  142. package/packages/supabase/src/lib/scenarioToDb.js +1 -1
  143. package/packages/supabase/src/lib/scenarioToDb.js.map +1 -1
  144. package/packages/utils/src/lib/applyUniversalMocks.js +39 -5
  145. package/packages/utils/src/lib/applyUniversalMocks.js.map +1 -1
  146. package/packages/utils/src/lib/lightweightEntityExtractor.js +17 -0
  147. package/packages/utils/src/lib/lightweightEntityExtractor.js.map +1 -1
  148. package/codeyam-cli/src/webserver/build/client/assets/InteractivePreview-XDSzQLOY.js +0 -26
  149. package/codeyam-cli/src/webserver/build/client/assets/ReportIssueModal-BORLgi0X.js +0 -1
  150. package/codeyam-cli/src/webserver/build/client/assets/ScenarioPreview-Bi-YUMa-.js +0 -6
  151. package/codeyam-cli/src/webserver/build/client/assets/_index-BC200mfN.js +0 -1
  152. package/codeyam-cli/src/webserver/build/client/assets/activity.(_tab)-CxvZPkCv.js +0 -10
  153. package/codeyam-cli/src/webserver/build/client/assets/circle-alert-IdsgAK39.js +0 -1
  154. package/codeyam-cli/src/webserver/build/client/assets/files-Dxh9CcaV.js +0 -1
  155. package/codeyam-cli/src/webserver/build/client/assets/git-BXmqrWCH.js +0 -12
  156. package/codeyam-cli/src/webserver/build/client/assets/globals-BGS74ED-.css +0 -1
  157. package/codeyam-cli/src/webserver/build/client/assets/manifest-e039ab42.js +0 -1
  158. package/codeyam-cli/src/webserver/build/client/assets/settings-Dc4MlMpK.js +0 -1
  159. package/codeyam-cli/src/webserver/build/client/assets/simulations-BQ-02-jB.js +0 -1
  160. package/codeyam-cli/src/webserver/build/client/assets/zap-_jw-9DCp.js +0 -1
  161. package/codeyam-cli/src/webserver/build/server/assets/server-build-CpyX1FZX.js +0 -169
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  Analysis,
3
3
  Entity,
4
+ EntityType,
4
5
  File,
5
6
  Scenario,
6
7
  Project,
@@ -25,6 +26,14 @@ import * as fs from 'fs';
25
26
  import * as path from 'path';
26
27
  import { LazyFileStore } from './LazyFileStore';
27
28
 
29
+ /**
30
+ * Escape special regex characters in a string so it can be used as a literal pattern.
31
+ * This prevents characters like . * + ? ^ $ { } ( ) | [ ] \ from being interpreted as regex metacharacters.
32
+ */
33
+ function escapeRegExp(str: string): string {
34
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
35
+ }
36
+
28
37
  interface WriteScenarioComponentsArgs {
29
38
  project: Project;
30
39
  file: File;
@@ -53,10 +62,12 @@ interface WriteScenarioComponentsArgs {
53
62
 
54
63
  type EnhancedImportedExport = Omit<
55
64
  Entity['metadata']['importedExports'][0],
56
- 'entityType' | 'calls'
65
+ 'calls' | 'entityType'
57
66
  > & {
58
67
  analysisId: string;
59
68
  isNodeModule?: boolean;
69
+ entityType?: EntityType; // Optional because nodeModuleImports don't have it
70
+ calls?: readonly string[]; // Optional - used for Zod schema detection heuristic
60
71
  };
61
72
 
62
73
  function hasMockStructure(
@@ -79,6 +90,7 @@ function hasMockStructure(
79
90
  importedExport.filePath
80
91
  ]?.[importedExport.name],
81
92
  )?.metadata?.mergedDataStructure?.dependencySchemas,
93
+ importedExport.entityType,
82
94
  );
83
95
  }
84
96
 
@@ -88,6 +100,113 @@ function isIndexPath(filePath: string) {
88
100
  return fileName.startsWith('index.');
89
101
  }
90
102
 
103
+ /**
104
+ * Convert .d.ts type declaration content to stub implementations.
105
+ * .d.ts files only have type declarations which don't provide runtime exports.
106
+ * This function converts them to actual stub implementations.
107
+ *
108
+ * @param content - The .d.ts file content
109
+ * @param entityName - The name of the entity we're generating for (used to ensure it's exported)
110
+ * @returns The converted content with stub implementations
111
+ */
112
+ function convertDtsToStubs(content: string, entityName: string): string {
113
+ let result = content;
114
+
115
+ // Handle object types FIRST (before simple types) since they span multiple lines
116
+ // and contain semicolons that would incorrectly terminate the simple type regex
117
+
118
+ // Convert "export declare const NAME: { ... };" (object type) to "export const NAME = {} as any;"
119
+ result = result.replace(
120
+ /export\s+declare\s+const\s+(\w+)\s*:\s*\{[^}]*\}\s*;/gs,
121
+ 'export const $1 = {} as any;',
122
+ );
123
+
124
+ // Convert "export declare let NAME: { ... };" (object type) to "export let NAME = {} as any;"
125
+ result = result.replace(
126
+ /export\s+declare\s+let\s+(\w+)\s*:\s*\{[^}]*\}\s*;/gs,
127
+ 'export let $1 = {} as any;',
128
+ );
129
+
130
+ // Convert "export declare var NAME: { ... };" (object type) to "export var NAME = {} as any;"
131
+ result = result.replace(
132
+ /export\s+declare\s+var\s+(\w+)\s*:\s*\{[^}]*\}\s*;/gs,
133
+ 'export var $1 = {} as any;',
134
+ );
135
+
136
+ // Now handle simple types (non-object types)
137
+
138
+ // Convert "export declare const NAME: TYPE;" to "export const NAME = {} as any;"
139
+ result = result.replace(
140
+ /export\s+declare\s+const\s+(\w+)\s*:\s*[^;]+;/g,
141
+ 'export const $1 = {} as any;',
142
+ );
143
+
144
+ // Convert "export declare let NAME: TYPE;" to "export let NAME = {} as any;"
145
+ result = result.replace(
146
+ /export\s+declare\s+let\s+(\w+)\s*:\s*[^;]+;/g,
147
+ 'export let $1 = {} as any;',
148
+ );
149
+
150
+ // Convert "export declare var NAME: TYPE;" to "export var NAME = {} as any;"
151
+ result = result.replace(
152
+ /export\s+declare\s+var\s+(\w+)\s*:\s*[^;]+;/g,
153
+ 'export var $1 = {} as any;',
154
+ );
155
+
156
+ // Convert "export declare function NAME(...): TYPE;" to "export function NAME() { return {} as any; }"
157
+ result = result.replace(
158
+ /export\s+declare\s+function\s+(\w+)\s*\([^)]*\)\s*:\s*[^;]+;/g,
159
+ 'export function $1(...args: any[]) { return {} as any; }',
160
+ );
161
+
162
+ // Convert "export declare class NAME { ... }" to "export class NAME {}"
163
+ // This is tricky because class bodies can span multiple lines
164
+ result = result.replace(
165
+ /export\s+declare\s+class\s+(\w+)\s*(?:extends\s+[^{]+)?\{[^}]*\}/gs,
166
+ 'export class $1 {}',
167
+ );
168
+
169
+ // Convert "declare const NAME: { ... }" (object type, non-exported) to "const NAME = {} as any;"
170
+ // Object types can span multiple lines and contain semicolons, so we handle them separately
171
+ // The 's' flag makes . match newlines, so this matches the entire object type block
172
+ result = result.replace(
173
+ /^declare\s+const\s+(\w+)\s*:\s*\{[^}]*\}\s*;/gms,
174
+ 'const $1 = {} as any;',
175
+ );
176
+
177
+ // Convert "declare const NAME: TYPE;" (simple type, non-exported) to "const NAME = {} as any;"
178
+ // These are often used with "export { NAME }" at the end
179
+ // This must come AFTER the object type handler above
180
+ result = result.replace(
181
+ /^declare\s+const\s+(\w+)\s*:\s*[^;]+;/gm,
182
+ 'const $1 = {} as any;',
183
+ );
184
+
185
+ // Convert "declare function NAME" (non-exported) to "function NAME() { return {} as any; }"
186
+ result = result.replace(
187
+ /^declare\s+function\s+(\w+)\s*\([^)]*\)\s*:\s*[^;]+;/gm,
188
+ 'function $1(...args: any[]) { return {} as any; }',
189
+ );
190
+
191
+ // Convert "declare class NAME" (non-exported) to "class NAME {}"
192
+ result = result.replace(
193
+ /^declare\s+class\s+(\w+)\s*(?:extends\s+[^{]+)?\{[^}]*\}/gm,
194
+ 'class $1 {}',
195
+ );
196
+
197
+ // Remove "export { }" at the end (empty export statement that marks module as having no exports)
198
+ result = result.replace(/export\s*\{\s*\}\s*;?\s*$/g, '');
199
+
200
+ // Keep export type and export interface statements as-is (they're valid in .ts)
201
+ // No transformation needed for these
202
+
203
+ console.log(
204
+ `CodeYam: Converted .d.ts content for entity "${entityName}". Result length: ${result.length}`,
205
+ );
206
+
207
+ return result;
208
+ }
209
+
91
210
  /**
92
211
  * Rewrite relative asset paths to correct locations when a file is moved.
93
212
  *
@@ -147,6 +266,177 @@ function rewriteAssetImports(
147
266
  );
148
267
  }
149
268
 
269
+ /**
270
+ * Rewrite relative TypeScript/JavaScript module imports when a file is moved.
271
+ *
272
+ * When a file is moved from one location to another (e.g., from [environmentId]/
273
+ * to _environmentId_/), relative imports like "./lib/organization" need to be
274
+ * rewritten to point to the correct location from the new path.
275
+ *
276
+ * This is similar to rewriteAssetImports but handles TypeScript/JavaScript modules
277
+ * instead of asset files (CSS, images, etc.).
278
+ */
279
+ function rewriteRelativeModuleImports(
280
+ fileContent: string,
281
+ originalFilePath: string,
282
+ newFilePath: string,
283
+ ): string {
284
+ // Module file extensions that should have their relative paths rewritten
285
+ const moduleExtensions = 'ts|tsx|js|jsx|mjs|cjs';
286
+
287
+ // Match import statements with relative paths (starting with ./ or ../)
288
+ // This matches:
289
+ // - import { foo } from "./path"
290
+ // - import foo from "../path"
291
+ // - import * as foo from "./path"
292
+ // - import "./path" (side-effect imports)
293
+ // The path may or may not have a file extension
294
+ const relativeImportRegex = new RegExp(
295
+ `(from\\s+)(['"])(\\.\\.?\\/[^'"]+?)(?:\\.(?:${moduleExtensions}))?\\2`,
296
+ 'g',
297
+ );
298
+
299
+ // Get the directory part of the original file path (relative to project root)
300
+ const originalDirParts = originalFilePath.split('/').slice(0, -1);
301
+
302
+ return fileContent.replace(
303
+ relativeImportRegex,
304
+ (match, fromKeyword, quote, importPath) => {
305
+ // Skip imports that already point to generated CodeYam files:
306
+ // 1. Scenario component files with SHA hashes (64 hex chars) and scenario name suffixes
307
+ // e.g., "abc123def_EnvironmentLayout_Empty_Survey_No_Responses"
308
+ // 2. Mock data files in __codeyamMocks__ directory
309
+ // e.g., "../__codeyamMocks__/MockData_Empty_Survey_No_Responses"
310
+ const scenarioFilePattern = /[a-f0-9]{64}_\w+_[A-Z][a-zA-Z_]+$/;
311
+ const mockDataPattern = /__codeyamMocks__\//;
312
+ if (
313
+ scenarioFilePattern.test(importPath) ||
314
+ mockDataPattern.test(importPath)
315
+ ) {
316
+ // This import already points to a generated file, don't rewrite it
317
+ return match;
318
+ }
319
+
320
+ // Resolve the import path relative to the original file's directory
321
+ const pathParts = importPath.split('/');
322
+ const resolvedParts = [...originalDirParts];
323
+
324
+ for (const part of pathParts) {
325
+ if (part === '..') {
326
+ resolvedParts.pop();
327
+ } else if (part !== '.') {
328
+ resolvedParts.push(part);
329
+ }
330
+ }
331
+
332
+ // Build the project-relative path to the module
333
+ const absoluteModulePath = resolvedParts.join('/');
334
+
335
+ // Calculate the new relative path from new file location
336
+ const newRelativePath = getRelativePath(newFilePath, absoluteModulePath);
337
+
338
+ // Replace the path in the original match, preserving the quote style
339
+ return `${fromKeyword}${quote}${newRelativePath}${quote}`;
340
+ },
341
+ );
342
+ }
343
+
344
+ /**
345
+ * Strip <html> and <body> tags from root layout files for Next.js App Router.
346
+ *
347
+ * When a root layout (app/layout.tsx) is generated as a scenario component and placed
348
+ * under the /static/ route, it becomes nested under the real app root layout. Having
349
+ * two layouts with <html> and <body> tags causes hydration errors:
350
+ *
351
+ * "In HTML, <html> cannot be a child of <body>"
352
+ * "You are mounting a new html component when a previous one has not first unmounted"
353
+ *
354
+ * This function removes the <html> and <body> wrapper tags while preserving the inner
355
+ * content, allowing the scenario layout to work correctly when nested.
356
+ *
357
+ * Before: <html lang="en"><body className={...}><main>{children}</main></body></html>
358
+ * After: <div className={...}><main>{children}</main></div>
359
+ */
360
+ function stripHtmlBodyTags(
361
+ fileContent: string,
362
+ filePath: string,
363
+ framework: ProjectFramework,
364
+ ): string {
365
+ // Only apply to Next.js App Router root layouts
366
+ if (framework !== ProjectFramework.Next) {
367
+ return fileContent;
368
+ }
369
+
370
+ // Check if this is a root layout file (typically app/layout.tsx or apps/*/app/layout.tsx)
371
+ // Root layouts are at the app directory level, not in subdirectories like (app)/ or routes/
372
+ const pathParts = filePath.split('/');
373
+ const fileName = pathParts[pathParts.length - 1];
374
+
375
+ // Must be a layout file
376
+ if (!fileName?.startsWith('layout.')) {
377
+ return fileContent;
378
+ }
379
+
380
+ // Check if it's at the app root level (parent directory is 'app')
381
+ const parentDir = pathParts[pathParts.length - 2];
382
+ if (parentDir !== 'app') {
383
+ return fileContent;
384
+ }
385
+
386
+ // Check if this file actually contains <html> and <body> tags
387
+ if (!/<html[^>]*>/.test(fileContent) || !/<body[^>]*>/.test(fileContent)) {
388
+ return fileContent;
389
+ }
390
+
391
+ console.log(
392
+ `CodeYam: Stripping <html> and <body> tags from root layout: ${filePath}`,
393
+ );
394
+
395
+ // Extract the body className/attributes if any, to preserve styling
396
+ const bodyMatch = fileContent.match(/<body([^>]*)>/);
397
+ const bodyAttributes = bodyMatch?.[1]?.trim() || '';
398
+
399
+ // Replace the JSX structure:
400
+ // <html ...><body ...>CONTENT</body></html>
401
+ // With: <div ...>CONTENT</div>
402
+ //
403
+ // This regex handles:
404
+ // 1. Opening <html> tag with any attributes
405
+ // 2. Opening <body> tag with any attributes (captured for the wrapper div)
406
+ // 3. Content between body tags (captured)
407
+ // 4. Closing </body> and </html> tags
408
+ const htmlBodyRegex =
409
+ /(<html[^>]*>\s*<body)([^>]*)(>)([\s\S]*?)(<\/body>\s*<\/html>)/g;
410
+
411
+ let result = fileContent.replace(
412
+ htmlBodyRegex,
413
+ (match, _htmlBody, bodyAttrs, closeBracket, content, _closing) => {
414
+ // Create a wrapper div with the body's attributes
415
+ const attrs = bodyAttrs?.trim() || '';
416
+ if (attrs) {
417
+ return `(<div${bodyAttrs}${closeBracket}${content}</div>)`;
418
+ } else {
419
+ return `(<>${content}</>)`;
420
+ }
421
+ },
422
+ );
423
+
424
+ // If the regex didn't match (perhaps due to different formatting),
425
+ // try a more lenient approach - just remove the tags
426
+ if (result === fileContent) {
427
+ // Remove <html ...> opening tag
428
+ result = result.replace(/<html[^>]*>\s*/g, '');
429
+ // Remove </html> closing tag
430
+ result = result.replace(/\s*<\/html>/g, '');
431
+ // Replace <body ...> with <div ...> to preserve attributes
432
+ result = result.replace(/<body([^>]*)>/g, '<div$1>');
433
+ // Replace </body> with </div>
434
+ result = result.replace(/<\/body>/g, '</div>');
435
+ }
436
+
437
+ return result;
438
+ }
439
+
150
440
  function addMockToContent(
151
441
  fileContent: string,
152
442
  importedExport: EnhancedImportedExport,
@@ -171,7 +461,11 @@ function addMockToContent(
171
461
  rootAnalysis.metadata?.mergedDataStructure?.dependencySchemas;
172
462
  }
173
463
 
174
- const mockCode = constructMockCode(importedExport.name, dependencySchemas);
464
+ const mockCode = constructMockCode(
465
+ importedExport.name,
466
+ dependencySchemas,
467
+ importedExport.entityType,
468
+ );
175
469
 
176
470
  if (!mockCode) {
177
471
  console.log(
@@ -218,8 +512,13 @@ function addMockToContent(
218
512
  );
219
513
  fileContent = fileContent.replace(entireImportRegExp, '');
220
514
  } else {
515
+ // Escape regex special characters in importPath (e.g., brackets in [environmentId])
516
+ const escapedImportPath = importPath.replace(
517
+ /[.*+?^${}()|[\]\\]/g,
518
+ '\\$&',
519
+ );
221
520
  const importRegExp = new RegExp(
222
- `(import(?:(?!${firstPart}|from|import)[\\s\\S])*?)${firstPart}(?:,\\s*)?((?:(?!import)[\\s\\S])*?from\\s+['"]${importPath}['"])`,
521
+ `(import(?:(?!${firstPart}|from|import)[\\s\\S])*?)${firstPart}(?:,\\s*)?((?:(?!import)[\\s\\S])*?from\\s+['"]${escapedImportPath}['"])`,
223
522
  'm',
224
523
  );
225
524
 
@@ -239,6 +538,38 @@ function addMockToContent(
239
538
  '',
240
539
  );
241
540
  }
541
+ } else {
542
+ // Fallback: When importPath is undefined (e.g., path alias not in mapping),
543
+ // we still need to remove the import to avoid "name defined multiple times" errors.
544
+ // Search for the entity name in any import statement and remove it.
545
+ const importedExportNameParts = splitOutsideParenthesesAndArrays(
546
+ importedExport.name,
547
+ );
548
+ const firstPart = importedExportNameParts[0];
549
+
550
+ // Match the entity name in any named import and remove just that name
551
+ // This handles imports like: import { useEnvironment, otherThing } from "any/path"
552
+ // Removes useEnvironment but keeps otherThing
553
+ const namedImportRegExp = new RegExp(
554
+ `(import\\s*\\{[^}]*?)\\b${escapeRegExp(firstPart)}\\b(?:,\\s*|\\s*,)?((?:[^}]*\\}\\s*from\\s*['"][^'"]*['"];?))`,
555
+ 'm',
556
+ );
557
+
558
+ if (importedExportNameParts.length > 1) {
559
+ // Rename the import instead of removing (for destructured access patterns)
560
+ fileContent = fileContent.replace(
561
+ namedImportRegExp,
562
+ `$1${firstPart}__cyOriginal$2`,
563
+ );
564
+ } else {
565
+ fileContent = fileContent.replace(namedImportRegExp, '$1$2');
566
+ }
567
+
568
+ // Clean up empty imports that might result from removing the only import
569
+ fileContent = fileContent.replace(
570
+ /import\s*\{\s*\}\s*from\s+['"][^'"]*['"];?\s*\n?/g,
571
+ '',
572
+ );
242
573
  }
243
574
 
244
575
  if (fileContent.indexOf('import { scenarios } from') === -1) {
@@ -544,6 +875,29 @@ export default async function writeScenarioComponents({
544
875
  );
545
876
 
546
877
  let fileContent = file.content;
878
+
879
+ // Handle .d.ts files: convert type declarations to stub implementations
880
+ // .d.ts files only have type declarations (e.g., "export declare const logger")
881
+ // which don't provide runtime exports. We need to generate actual stub implementations.
882
+ if (file.path.endsWith('.d.ts')) {
883
+ console.log(
884
+ `CodeYam: Converting .d.ts file to stub implementations: ${file.path}`,
885
+ );
886
+ fileContent = convertDtsToStubs(fileContent, entity.name);
887
+ }
888
+
889
+ // Extract "use client" or "use server" directive FIRST, before any modifications
890
+ // This ensures the directive isn't buried by prepended imports
891
+ const directiveMatch = fileContent.match(
892
+ /^(\s*["']use (client|server)["'];?\s*\n?)/,
893
+ );
894
+ let extractedDirective: string | null = null;
895
+ if (directiveMatch) {
896
+ extractedDirective = directiveMatch[1].trim();
897
+ // Remove the directive from fileContent - we'll add it back at the very end
898
+ fileContent = fileContent.slice(directiveMatch[0].length);
899
+ }
900
+
547
901
  const fileAnalyzer = projectAnalyzer.getFileAnalyzer(file);
548
902
  const importMapping = fileAnalyzer.getRelativeImportMappings();
549
903
 
@@ -749,21 +1103,67 @@ export default async function writeScenarioComponents({
749
1103
  importedExport.name !== entity.name
750
1104
  ) {
751
1105
  // For same-file dependencies:
752
- // - Strip if isMocked is true (e.g., Remix/Next loaders, server-side code)
753
- // - Strip if we have a mock structure we can generate
754
- // - Only add mock code if we have a mock structure
755
- const shouldStrip =
756
- importedExport.isMocked ||
757
- hasMockStructure(importedExport, fileAnalyses);
758
-
759
- if (shouldStrip) {
1106
+ // - Strip and replace if we have a mock structure we can generate
1107
+ // - Strip and stub if isMocked is true AND it's a callable entity (visual, library, functionCall)
1108
+ // - Preserve as-is for data entities (like Zod schemas) that need their methods
1109
+ const hasMock = hasMockStructure(importedExport, fileAnalyses);
1110
+ const entityType = importedExport.entityType;
1111
+ // Data and type entities should be preserved - they're not callable and may have methods
1112
+ // that stubbing would break (e.g., Zod schemas with .superRefine())
1113
+ const isDataEntity = entityType === 'data' || entityType === 'type';
1114
+
1115
+ // Heuristic: Zod schemas are often misclassified as 'library' but should be preserved
1116
+ // Detect by: name starts with Z + uppercase letter, AND has Zod method calls
1117
+ const looksLikeZodSchema =
1118
+ entityType === 'library' &&
1119
+ /^Z[A-Z]/.test(importedExport.name) &&
1120
+ importedExport.calls?.some((call: string) =>
1121
+ /\.(superRefine|refine|transform|default|optional|nullable|array|object|string|number|boolean|parse|safeParse)\s*\(/.test(
1122
+ call,
1123
+ ),
1124
+ );
1125
+
1126
+ if (looksLikeZodSchema) {
1127
+ console.log(
1128
+ `CodeYam: Detected Zod schema "${importedExport.name}" (misclassified as library) - will preserve`,
1129
+ );
1130
+ }
1131
+
1132
+ // Callable entities can be safely stubbed (but not Zod schemas)
1133
+ const isCallable =
1134
+ !isDataEntity && !looksLikeZodSchema && entityType !== undefined;
1135
+
1136
+ // Determine what action to take
1137
+ const shouldStripAndReplace = hasMock;
1138
+ const shouldStripAndStub =
1139
+ !hasMock && importedExport.isMocked && isCallable;
1140
+ const shouldPreserve = !hasMock && importedExport.isMocked && !isCallable;
1141
+
1142
+ // Log warning if entityType is undefined for a mocked same-file dependency
1143
+ // This could indicate an analysis issue where entityType wasn't properly set
1144
+ if (importedExport.isMocked && entityType === undefined) {
1145
+ console.warn(
1146
+ `CodeYam: WARNING - Same-file dependency "${importedExport.name}" is isMocked but has no entityType. ` +
1147
+ `Treating as non-callable (will preserve). File: ${file.path}`,
1148
+ );
1149
+ }
1150
+
1151
+ if (shouldPreserve) {
1152
+ // For data entities (like Zod schemas), don't strip or stub - preserve the original
1153
+ // This ensures schema methods like .superRefine() continue to work
1154
+ console.log(
1155
+ `CodeYam: Preserving ${importedExport.name} (entityType: ${entityType}) - not stripping data entities`,
1156
+ );
1157
+ // Don't modify fileContent - keep the original code
1158
+ } else if (shouldStripAndReplace || shouldStripAndStub) {
1159
+ // Strip the original code
760
1160
  const entityCode = fileAnalyzer.getEntityCode(importedExport.name);
761
1161
  if (entityCode) {
762
1162
  fileContent = fileContent.replace(entityCode, '');
763
1163
  }
764
1164
 
765
- // Only add mock if we have a mock structure to generate
766
- if (hasMockStructure(importedExport, fileAnalyses)) {
1165
+ if (shouldStripAndReplace) {
1166
+ // Add mock from mock structure
767
1167
  fileContent = addMockToContent(
768
1168
  fileContent,
769
1169
  importedExport,
@@ -772,12 +1172,14 @@ export default async function writeScenarioComponents({
772
1172
  relativeMocksDir,
773
1173
  scenario.name,
774
1174
  );
775
- } else if (importedExport.isMocked) {
776
- // No mock structure available, but isMocked is true.
1175
+ } else if (shouldStripAndStub) {
777
1176
  // Generate a simple stub mock that returns scenario data.
778
1177
  // This prevents ReferenceError at runtime when the stripped
779
1178
  // function is called (e.g., local helper functions like getInitialProps).
780
1179
  const functionName = importedExport.name;
1180
+ console.log(
1181
+ `CodeYam: Generating stub mock for ${functionName} (entityType: ${entityType}) in ${file.path}`,
1182
+ );
781
1183
 
782
1184
  // Add scenarios import if not present
783
1185
  if (fileContent.indexOf('import { scenarios } from') === -1) {
@@ -910,7 +1312,7 @@ export default async function writeScenarioComponents({
910
1312
  // Otherwise, skip rewriting (we can't find the import statement without knowing what to search for)
911
1313
  if (mockImportMapping) {
912
1314
  const importRegExp = new RegExp(
913
- `${mockImportMapping.replace(/([$()])/g, '\\$1')}(['"])`,
1315
+ `${escapeRegExp(mockImportMapping)}(['"])`,
914
1316
  'g',
915
1317
  );
916
1318
 
@@ -929,9 +1331,11 @@ export default async function writeScenarioComponents({
929
1331
  // First, try to remove this entity from the already-rewritten grouped import
930
1332
  // This prevents duplicate/conflicting imports
931
1333
  // Match patterns like "EntityName," or ", EntityName" or "EntityName" (if only one)
1334
+ // Note: entityImportName needs escaping since JS identifiers can contain $ (a regex metacharacter)
1335
+ const escapedEntityName = escapeRegExp(entityImportName);
932
1336
  const removeFromGroupedImportPatterns = [
933
- new RegExp(`\\b${entityImportName}\\s*,\\s*`, 'g'), // "EntityName, "
934
- new RegExp(`\\s*,\\s*${entityImportName}\\b`, 'g'), // ", EntityName"
1337
+ new RegExp(`\\b${escapedEntityName}\\s*,\\s*`, 'g'), // "EntityName, "
1338
+ new RegExp(`\\s*,\\s*${escapedEntityName}\\b`, 'g'), // ", EntityName"
935
1339
  ];
936
1340
  for (const pattern of removeFromGroupedImportPatterns) {
937
1341
  fileContent = fileContent.replace(pattern, '');
@@ -976,9 +1380,9 @@ export default async function writeScenarioComponents({
976
1380
 
977
1381
  for (const universalMock of nodeModuleUniversalMocks) {
978
1382
  const originalPath = universalMock.filePath;
979
- // Create the mock file name using the same pattern as applyUniversalMocks
980
- const safeFileName = originalPath.replace(/[^a-zA-Z0-9_\-./]/g, '_');
981
- const mockFileRelativePath = `${relativeMocksDir}/${safeFileName}`;
1383
+ // Create the mock file name using the same safeFileName function as writeUniversalMocks
1384
+ const safeMockFileName = safeFileName(originalPath);
1385
+ const mockFileRelativePath = `${relativeMocksDir}/${safeMockFileName}`;
982
1386
 
983
1387
  // Match both single and double quotes, and handle both named and default imports
984
1388
  // Pattern 1: import { x, y } from "module"
@@ -1036,6 +1440,10 @@ export default async function writeScenarioComponents({
1036
1440
  });
1037
1441
  }
1038
1442
 
1443
+ // Strip <html> and <body> tags from root layout files for Next.js
1444
+ // These tags cause hydration errors when the scenario layout is nested under the real root
1445
+ fileContent = stripHtmlBodyTags(fileContent, file.path, framework);
1446
+
1039
1447
  // Rewrite asset imports (CSS, images, fonts, etc.) to correct relative paths
1040
1448
  // The original file path is relative to PROJECT_RELATIVE_PATH, the new path is scenarioComponentPath
1041
1449
  fileContent = rewriteAssetImports(
@@ -1044,6 +1452,15 @@ export default async function writeScenarioComponents({
1044
1452
  scenarioComponentPath,
1045
1453
  );
1046
1454
 
1455
+ // Rewrite relative TypeScript/JavaScript module imports to correct relative paths
1456
+ // This handles cases where the file is moved (e.g., from [environmentId]/ to _environmentId_/)
1457
+ // and relative imports like "./lib/organization" need to be rewritten
1458
+ fileContent = rewriteRelativeModuleImports(
1459
+ fileContent,
1460
+ `${PROJECT_RELATIVE_PATH}/${file.path}`,
1461
+ scenarioComponentPath,
1462
+ );
1463
+
1047
1464
  console.log(
1048
1465
  'Writing scenario component',
1049
1466
  file.path,
@@ -1063,10 +1480,19 @@ export default async function writeScenarioComponents({
1063
1480
  // Scenario: ${scenario.id} - ${scenario.name}
1064
1481
  `;
1065
1482
 
1066
- await writeFile(
1067
- scenarioComponentPath,
1068
- `${scenarioComponentComment}\n\n${fileContent}`,
1069
- );
1483
+ // Use the directive that was extracted at the beginning of processing
1484
+ // This ensures it stays at the very top even after imports are prepended
1485
+ let finalContent: string;
1486
+ if (extractedDirective) {
1487
+ finalContent = `${extractedDirective}\n\n${scenarioComponentComment}\n\n${fileContent}`;
1488
+ console.log(
1489
+ `CodeYam: Placed "${extractedDirective}" directive at top of file: ${scenarioComponentPath}`,
1490
+ );
1491
+ } else {
1492
+ finalContent = `${scenarioComponentComment}\n\n${fileContent}`;
1493
+ }
1494
+
1495
+ await writeFile(scenarioComponentPath, finalContent);
1070
1496
  scenarioComponentPaths.push(scenarioComponentPath);
1071
1497
 
1072
1498
  console.log('CodeYam [writeScenarioComponents]: Generated scenario files', {
@@ -32,6 +32,17 @@ function updateImportStatements(directory, level = 0) {
32
32
  result = result.replace(
33
33
  /from\s+['"](\.+\/)(.*?)['"]/g,
34
34
  (match, relativePath, moduleName) => {
35
+ // Skip template literals with variables (e.g., '../${importPath}')
36
+ // These are dynamic imports that shouldn't be processed at build time
37
+ if (moduleName.includes('${')) {
38
+ return match;
39
+ }
40
+
41
+ // Skip if the module already has a file extension
42
+ if (/\.(js|ts|tsx|jsx|mjs|cjs|json)$/.test(moduleName)) {
43
+ return match;
44
+ }
45
+
35
46
  const fullPath = path.resolve(
36
47
  path.dirname(filePath),
37
48
  relativePath + moduleName,
@@ -78,7 +89,7 @@ function updateImportStatements(directory, level = 0) {
78
89
  return `'${relativeLevel}/packages/${importPath}/index.js'`;
79
90
  },
80
91
  );
81
- result = result.replace(/.js.js/g, '.js');
92
+ result = result.replace(/\.js\.js/g, '.js');
82
93
  result = result.replace(/index\/index\.js/g, 'index.js');
83
94
  result = result.replace(/(index\.js\/)+/g, '');
84
95