@pie-element/cli 0.1.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 (255) hide show
  1. package/.compatibility/report.json +2491 -0
  2. package/.turbo/turbo-build.log +1 -0
  3. package/CHANGELOG.md +8 -0
  4. package/README.md +240 -0
  5. package/REFACTORING.md +131 -0
  6. package/bin/dev.js +5 -0
  7. package/bin/run.js +5 -0
  8. package/dist/commands/dev/demo/index.d.ts +33 -0
  9. package/dist/commands/dev/demo/index.d.ts.map +1 -0
  10. package/dist/commands/dev/demo/index.js +274 -0
  11. package/dist/commands/dev/demo/index.js.map +1 -0
  12. package/dist/commands/dev/test-bundler.d.ts +18 -0
  13. package/dist/commands/dev/test-bundler.d.ts.map +1 -0
  14. package/dist/commands/dev/test-bundler.js +284 -0
  15. package/dist/commands/dev/test-bundler.js.map +1 -0
  16. package/dist/commands/mathquill/migrate.d.ts +20 -0
  17. package/dist/commands/mathquill/migrate.d.ts.map +1 -0
  18. package/dist/commands/mathquill/migrate.js +240 -0
  19. package/dist/commands/mathquill/migrate.js.map +1 -0
  20. package/dist/commands/mathquill/status.d.ts +20 -0
  21. package/dist/commands/mathquill/status.d.ts.map +1 -0
  22. package/dist/commands/mathquill/status.js +231 -0
  23. package/dist/commands/mathquill/status.js.map +1 -0
  24. package/dist/commands/packages/create-controllers.d.ts +13 -0
  25. package/dist/commands/packages/create-controllers.d.ts.map +1 -0
  26. package/dist/commands/packages/create-controllers.js +103 -0
  27. package/dist/commands/packages/create-controllers.js.map +1 -0
  28. package/dist/commands/packages/create-lib-react.d.ts +17 -0
  29. package/dist/commands/packages/create-lib-react.d.ts.map +1 -0
  30. package/dist/commands/packages/create-lib-react.js +228 -0
  31. package/dist/commands/packages/create-lib-react.js.map +1 -0
  32. package/dist/commands/packages/create-react-elements.d.ts +16 -0
  33. package/dist/commands/packages/create-react-elements.d.ts.map +1 -0
  34. package/dist/commands/packages/create-react-elements.js +259 -0
  35. package/dist/commands/packages/create-react-elements.js.map +1 -0
  36. package/dist/commands/packages/enable-publishing.d.ts +11 -0
  37. package/dist/commands/packages/enable-publishing.d.ts.map +1 -0
  38. package/dist/commands/packages/enable-publishing.js +117 -0
  39. package/dist/commands/packages/enable-publishing.js.map +1 -0
  40. package/dist/commands/packages/init-synced-elements.d.ts +16 -0
  41. package/dist/commands/packages/init-synced-elements.d.ts.map +1 -0
  42. package/dist/commands/packages/init-synced-elements.js +204 -0
  43. package/dist/commands/packages/init-synced-elements.js.map +1 -0
  44. package/dist/commands/upstream/analyze-esm.d.ts +59 -0
  45. package/dist/commands/upstream/analyze-esm.d.ts.map +1 -0
  46. package/dist/commands/upstream/analyze-esm.js +1481 -0
  47. package/dist/commands/upstream/analyze-esm.js.map +1 -0
  48. package/dist/commands/upstream/check.d.ts +15 -0
  49. package/dist/commands/upstream/check.d.ts.map +1 -0
  50. package/dist/commands/upstream/check.js +165 -0
  51. package/dist/commands/upstream/check.js.map +1 -0
  52. package/dist/commands/upstream/deps.d.ts +15 -0
  53. package/dist/commands/upstream/deps.d.ts.map +1 -0
  54. package/dist/commands/upstream/deps.js +174 -0
  55. package/dist/commands/upstream/deps.js.map +1 -0
  56. package/dist/commands/upstream/sync-mathquill.d.ts +12 -0
  57. package/dist/commands/upstream/sync-mathquill.d.ts.map +1 -0
  58. package/dist/commands/upstream/sync-mathquill.js +97 -0
  59. package/dist/commands/upstream/sync-mathquill.js.map +1 -0
  60. package/dist/commands/upstream/sync.d.ts +25 -0
  61. package/dist/commands/upstream/sync.d.ts.map +1 -0
  62. package/dist/commands/upstream/sync.js +552 -0
  63. package/dist/commands/upstream/sync.js.map +1 -0
  64. package/dist/commands/upstream/track.d.ts +15 -0
  65. package/dist/commands/upstream/track.d.ts.map +1 -0
  66. package/dist/commands/upstream/track.js +115 -0
  67. package/dist/commands/upstream/track.js.map +1 -0
  68. package/dist/commands/upstream/update.d.ts +15 -0
  69. package/dist/commands/upstream/update.d.ts.map +1 -0
  70. package/dist/commands/upstream/update.js +98 -0
  71. package/dist/commands/upstream/update.js.map +1 -0
  72. package/dist/commands/verify/controllers.d.ts +12 -0
  73. package/dist/commands/verify/controllers.d.ts.map +1 -0
  74. package/dist/commands/verify/controllers.js +138 -0
  75. package/dist/commands/verify/controllers.js.map +1 -0
  76. package/dist/commands/verify/react-build.d.ts +13 -0
  77. package/dist/commands/verify/react-build.d.ts.map +1 -0
  78. package/dist/commands/verify/react-build.js +135 -0
  79. package/dist/commands/verify/react-build.js.map +1 -0
  80. package/dist/index.d.ts +2 -0
  81. package/dist/index.d.ts.map +1 -0
  82. package/dist/index.js +2 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/lib/mathquill/extension-generator.d.ts +9 -0
  85. package/dist/lib/mathquill/extension-generator.d.ts.map +1 -0
  86. package/dist/lib/mathquill/extension-generator.js +107 -0
  87. package/dist/lib/mathquill/extension-generator.js.map +1 -0
  88. package/dist/lib/mathquill/khan-extractor.d.ts +16 -0
  89. package/dist/lib/mathquill/khan-extractor.d.ts.map +1 -0
  90. package/dist/lib/mathquill/khan-extractor.js +148 -0
  91. package/dist/lib/mathquill/khan-extractor.js.map +1 -0
  92. package/dist/lib/mathquill/learnosity-extractor.d.ts +15 -0
  93. package/dist/lib/mathquill/learnosity-extractor.d.ts.map +1 -0
  94. package/dist/lib/mathquill/learnosity-extractor.js +170 -0
  95. package/dist/lib/mathquill/learnosity-extractor.js.map +1 -0
  96. package/dist/lib/mathquill/migration-orchestrator.d.ts +77 -0
  97. package/dist/lib/mathquill/migration-orchestrator.d.ts.map +1 -0
  98. package/dist/lib/mathquill/migration-orchestrator.js +391 -0
  99. package/dist/lib/mathquill/migration-orchestrator.js.map +1 -0
  100. package/dist/lib/mathquill/package-updater.d.ts +9 -0
  101. package/dist/lib/mathquill/package-updater.d.ts.map +1 -0
  102. package/dist/lib/mathquill/package-updater.js +103 -0
  103. package/dist/lib/mathquill/package-updater.js.map +1 -0
  104. package/dist/lib/mathquill/pie-extractor.d.ts +14 -0
  105. package/dist/lib/mathquill/pie-extractor.d.ts.map +1 -0
  106. package/dist/lib/mathquill/pie-extractor.js +167 -0
  107. package/dist/lib/mathquill/pie-extractor.js.map +1 -0
  108. package/dist/lib/upstream/repo-utils.d.ts +9 -0
  109. package/dist/lib/upstream/repo-utils.d.ts.map +1 -0
  110. package/dist/lib/upstream/repo-utils.js +10 -0
  111. package/dist/lib/upstream/repo-utils.js.map +1 -0
  112. package/dist/lib/upstream/sync-compatibility.d.ts +29 -0
  113. package/dist/lib/upstream/sync-compatibility.d.ts.map +1 -0
  114. package/dist/lib/upstream/sync-compatibility.js +70 -0
  115. package/dist/lib/upstream/sync-compatibility.js.map +1 -0
  116. package/dist/lib/upstream/sync-constants.d.ts +50 -0
  117. package/dist/lib/upstream/sync-constants.d.ts.map +1 -0
  118. package/dist/lib/upstream/sync-constants.js +66 -0
  119. package/dist/lib/upstream/sync-constants.js.map +1 -0
  120. package/dist/lib/upstream/sync-controllers-strategy.d.ts +18 -0
  121. package/dist/lib/upstream/sync-controllers-strategy.d.ts.map +1 -0
  122. package/dist/lib/upstream/sync-controllers-strategy.js +243 -0
  123. package/dist/lib/upstream/sync-controllers-strategy.js.map +1 -0
  124. package/dist/lib/upstream/sync-demo-metadata.d.ts +15 -0
  125. package/dist/lib/upstream/sync-demo-metadata.d.ts.map +1 -0
  126. package/dist/lib/upstream/sync-demo-metadata.js +165 -0
  127. package/dist/lib/upstream/sync-demo-metadata.js.map +1 -0
  128. package/dist/lib/upstream/sync-dev-exports.d.ts +13 -0
  129. package/dist/lib/upstream/sync-dev-exports.d.ts.map +1 -0
  130. package/dist/lib/upstream/sync-dev-exports.js +115 -0
  131. package/dist/lib/upstream/sync-dev-exports.js.map +1 -0
  132. package/dist/lib/upstream/sync-externals.d.ts +29 -0
  133. package/dist/lib/upstream/sync-externals.d.ts.map +1 -0
  134. package/dist/lib/upstream/sync-externals.js +164 -0
  135. package/dist/lib/upstream/sync-externals.js.map +1 -0
  136. package/dist/lib/upstream/sync-filesystem.d.ts +21 -0
  137. package/dist/lib/upstream/sync-filesystem.d.ts.map +1 -0
  138. package/dist/lib/upstream/sync-filesystem.js +64 -0
  139. package/dist/lib/upstream/sync-filesystem.js.map +1 -0
  140. package/dist/lib/upstream/sync-imports.d.ts +284 -0
  141. package/dist/lib/upstream/sync-imports.d.ts.map +1 -0
  142. package/dist/lib/upstream/sync-imports.js +816 -0
  143. package/dist/lib/upstream/sync-imports.js.map +1 -0
  144. package/dist/lib/upstream/sync-mathquill.d.ts +19 -0
  145. package/dist/lib/upstream/sync-mathquill.d.ts.map +1 -0
  146. package/dist/lib/upstream/sync-mathquill.js +160 -0
  147. package/dist/lib/upstream/sync-mathquill.js.map +1 -0
  148. package/dist/lib/upstream/sync-package-json.d.ts +28 -0
  149. package/dist/lib/upstream/sync-package-json.d.ts.map +1 -0
  150. package/dist/lib/upstream/sync-package-json.js +66 -0
  151. package/dist/lib/upstream/sync-package-json.js.map +1 -0
  152. package/dist/lib/upstream/sync-package-manager.d.ts +50 -0
  153. package/dist/lib/upstream/sync-package-manager.d.ts.map +1 -0
  154. package/dist/lib/upstream/sync-package-manager.js +570 -0
  155. package/dist/lib/upstream/sync-package-manager.js.map +1 -0
  156. package/dist/lib/upstream/sync-pielib-strategy.d.ts +26 -0
  157. package/dist/lib/upstream/sync-pielib-strategy.d.ts.map +1 -0
  158. package/dist/lib/upstream/sync-pielib-strategy.js +345 -0
  159. package/dist/lib/upstream/sync-pielib-strategy.js.map +1 -0
  160. package/dist/lib/upstream/sync-react-strategy.d.ts +54 -0
  161. package/dist/lib/upstream/sync-react-strategy.d.ts.map +1 -0
  162. package/dist/lib/upstream/sync-react-strategy.js +733 -0
  163. package/dist/lib/upstream/sync-react-strategy.js.map +1 -0
  164. package/dist/lib/upstream/sync-strategy.d.ts +49 -0
  165. package/dist/lib/upstream/sync-strategy.d.ts.map +1 -0
  166. package/dist/lib/upstream/sync-strategy.js +2 -0
  167. package/dist/lib/upstream/sync-strategy.js.map +1 -0
  168. package/dist/lib/upstream/sync-summary.d.ts +20 -0
  169. package/dist/lib/upstream/sync-summary.d.ts.map +1 -0
  170. package/dist/lib/upstream/sync-summary.js +157 -0
  171. package/dist/lib/upstream/sync-summary.js.map +1 -0
  172. package/dist/lib/upstream/sync-transforms.d.ts +60 -0
  173. package/dist/lib/upstream/sync-transforms.d.ts.map +1 -0
  174. package/dist/lib/upstream/sync-transforms.js +97 -0
  175. package/dist/lib/upstream/sync-transforms.js.map +1 -0
  176. package/dist/lib/upstream/sync-vite-config.d.ts +13 -0
  177. package/dist/lib/upstream/sync-vite-config.d.ts.map +1 -0
  178. package/dist/lib/upstream/sync-vite-config.js +231 -0
  179. package/dist/lib/upstream/sync-vite-config.js.map +1 -0
  180. package/dist/utils/compatibility.d.ts +129 -0
  181. package/dist/utils/compatibility.d.ts.map +1 -0
  182. package/dist/utils/compatibility.js +44 -0
  183. package/dist/utils/compatibility.js.map +1 -0
  184. package/dist/utils/conversion.d.ts +25 -0
  185. package/dist/utils/conversion.d.ts.map +1 -0
  186. package/dist/utils/conversion.js +123 -0
  187. package/dist/utils/conversion.js.map +1 -0
  188. package/dist/utils/git.d.ts +7 -0
  189. package/dist/utils/git.d.ts.map +1 -0
  190. package/dist/utils/git.js +63 -0
  191. package/dist/utils/git.js.map +1 -0
  192. package/dist/utils/logger.d.ts +29 -0
  193. package/dist/utils/logger.d.ts.map +1 -0
  194. package/dist/utils/logger.js +63 -0
  195. package/dist/utils/logger.js.map +1 -0
  196. package/dist/utils/package-json.d.ts +12 -0
  197. package/dist/utils/package-json.d.ts.map +1 -0
  198. package/dist/utils/package-json.js +29 -0
  199. package/dist/utils/package-json.js.map +1 -0
  200. package/package.json +59 -0
  201. package/packages/cli/bin/dev.js +5 -0
  202. package/packages/cli/bin/run.js +5 -0
  203. package/src/commands/dev/demo/index.ts +313 -0
  204. package/src/commands/dev/test-bundler.ts +345 -0
  205. package/src/commands/mathquill/migrate.ts +269 -0
  206. package/src/commands/mathquill/status.ts +267 -0
  207. package/src/commands/packages/create-controllers.ts +127 -0
  208. package/src/commands/packages/create-lib-react.ts +279 -0
  209. package/src/commands/packages/create-react-elements.ts +306 -0
  210. package/src/commands/packages/enable-publishing.ts +161 -0
  211. package/src/commands/packages/init-synced-elements.ts +259 -0
  212. package/src/commands/upstream/README.md +444 -0
  213. package/src/commands/upstream/analyze-esm.ts +1973 -0
  214. package/src/commands/upstream/check.ts +228 -0
  215. package/src/commands/upstream/deps.ts +222 -0
  216. package/src/commands/upstream/sync-mathquill.ts +108 -0
  217. package/src/commands/upstream/sync.ts +687 -0
  218. package/src/commands/upstream/track.ts +145 -0
  219. package/src/commands/upstream/update.ts +110 -0
  220. package/src/commands/verify/controllers.ts +166 -0
  221. package/src/commands/verify/react-build.ts +171 -0
  222. package/src/index.ts +1 -0
  223. package/src/lib/mathquill/extension-generator.ts +111 -0
  224. package/src/lib/mathquill/khan-extractor.ts +175 -0
  225. package/src/lib/mathquill/learnosity-extractor.ts +198 -0
  226. package/src/lib/mathquill/migration-orchestrator.ts +513 -0
  227. package/src/lib/mathquill/package-updater.ts +120 -0
  228. package/src/lib/mathquill/pie-extractor.ts +194 -0
  229. package/src/lib/upstream/repo-utils.ts +18 -0
  230. package/src/lib/upstream/sync-compatibility.ts +89 -0
  231. package/src/lib/upstream/sync-constants.ts +77 -0
  232. package/src/lib/upstream/sync-controllers-strategy.ts +291 -0
  233. package/src/lib/upstream/sync-demo-metadata.ts +199 -0
  234. package/src/lib/upstream/sync-dev-exports.ts +151 -0
  235. package/src/lib/upstream/sync-externals.ts +159 -0
  236. package/src/lib/upstream/sync-filesystem.ts +87 -0
  237. package/src/lib/upstream/sync-imports.ts +992 -0
  238. package/src/lib/upstream/sync-mathquill.ts +194 -0
  239. package/src/lib/upstream/sync-package-json.ts +86 -0
  240. package/src/lib/upstream/sync-package-manager.ts +694 -0
  241. package/src/lib/upstream/sync-pielib-strategy.ts +441 -0
  242. package/src/lib/upstream/sync-react-strategy.ts +945 -0
  243. package/src/lib/upstream/sync-strategy.ts +61 -0
  244. package/src/lib/upstream/sync-summary.ts +194 -0
  245. package/src/lib/upstream/sync-transforms.ts +154 -0
  246. package/src/lib/upstream/sync-vite-config.ts +243 -0
  247. package/src/types/es-module-lexer.d.ts +4 -0
  248. package/src/utils/compatibility.ts +182 -0
  249. package/src/utils/conversion.ts +153 -0
  250. package/src/utils/git.ts +63 -0
  251. package/src/utils/logger.ts +69 -0
  252. package/src/utils/package-json.ts +43 -0
  253. package/tests/sync-package-manager.test.ts +112 -0
  254. package/tests/transform-assign-props.test.ts +101 -0
  255. package/tsconfig.json +15 -0
@@ -0,0 +1,816 @@
1
+ /**
2
+ * Import rewriting utilities for sync operations
3
+ *
4
+ * This module contains transforms that rewrite imports during the sync process
5
+ * to handle differences between the upstream CommonJS/Webpack code and the
6
+ * ESM-only target environment.
7
+ *
8
+ * ## Why These Rewrites Are Needed:
9
+ *
10
+ * 1. **Lodash → Lodash-ES**:
11
+ * - Upstream uses CommonJS `lodash` package
12
+ * - ESM requires `lodash-es` for proper tree-shaking
13
+ *
14
+ * 2. **Recharts 2.x → 3.x**:
15
+ * - Upstream uses recharts 2.x which has lodash dependency
16
+ * - Recharts 3.x is fully ESM-compatible with no lodash dependency
17
+ *
18
+ * 3. **@pie-framework Events → Internal Packages**:
19
+ * - Upstream references external @pie-framework packages
20
+ * - Monorepo uses internal @pie-element/shared-* packages
21
+ *
22
+ * 4. **Editable-HTML Constants Inlining**:
23
+ * - `editable-html` package is not ESM-compatible (Slate v0.x dependencies)
24
+ * - We only need constants, so inline them to avoid the dependency
25
+ *
26
+ * 5. **TokenTypes Re-export**:
27
+ * - Upstream code works in CommonJS/Webpack (looser module resolution)
28
+ * - ESM requires explicit re-exports for proper module graph
29
+ *
30
+ * 6. **Configure Defaults Inlining**:
31
+ * - Configure package is not ESM-compatible (Slate v0.x dependencies)
32
+ * - Student-facing UI only needs minimal fallback configuration
33
+ * - Inline empty defaults object to avoid the dependency
34
+ */
35
+ import { readFile, writeFile } from 'node:fs/promises';
36
+ /**
37
+ * Fix import statements in a file to handle default export conversions
38
+ *
39
+ * When converting JS files with default exports to TS, we need to update
40
+ * imports in consuming files to use named imports instead.
41
+ */
42
+ export async function fixImportsInFile(filePath, defaultExportFiles) {
43
+ let content = await readFile(filePath, 'utf-8');
44
+ let modified = false;
45
+ // Match import statements: import Something from './file'
46
+ const importRegex = /import\s+(\w+)\s+from\s+['"](\.[^'"]+)['"]/g;
47
+ const matches = Array.from(content.matchAll(importRegex));
48
+ for (const match of matches) {
49
+ const [fullMatch, importName, importPath] = match;
50
+ // Normalize the import path (remove extensions, resolve relative paths)
51
+ const normalizedPath = importPath.replace(/\.(tsx?|jsx?)$/, '');
52
+ // Check if this file had a default export
53
+ if (defaultExportFiles.has(normalizedPath)) {
54
+ // Replace default import with named import
55
+ const replacement = `import { ${importName} } from '${importPath}'`;
56
+ content = content.replace(fullMatch, replacement);
57
+ modified = true;
58
+ }
59
+ }
60
+ if (modified) {
61
+ await writeFile(filePath, content, 'utf-8');
62
+ }
63
+ return modified;
64
+ }
65
+ /**
66
+ * Check if code contains JSX syntax
67
+ */
68
+ export function containsJsx(code) {
69
+ return /<[A-Za-z][\w-]*[\s/>]/.test(code);
70
+ }
71
+ /**
72
+ * Replace editable-html imports with inlined constants
73
+ *
74
+ * editable-html is not ESM-compatible (depends on Slate v0.x), but some packages
75
+ * only import constants from it. We inline these constants to avoid the dependency.
76
+ */
77
+ export function inlineEditableHtmlConstants(code) {
78
+ // Check if file imports from editable-html
79
+ const editableHtmlImportRegex = /import\s+(\w+)\s+from\s+['"]\.\.\/\.\.\/\.\.\/editable-html\/src\/constants['"]/;
80
+ const match = code.match(editableHtmlImportRegex);
81
+ if (!match) {
82
+ return code; // No editable-html import, return unchanged
83
+ }
84
+ const importName = match[1];
85
+ // Replace the import with inlined constants
86
+ const inlinedConstants = `// Inlined from editable-html/src/constants (not ESM-compatible)
87
+ const ${importName} = {
88
+ MAIN_CONTAINER_CLASS: 'main-container',
89
+ PIE_TOOLBAR__CLASS: 'pie-toolbar',
90
+ };`;
91
+ return code.replace(editableHtmlImportRegex, inlinedConstants);
92
+ }
93
+ /**
94
+ * Fix missing re-export of TokenTypes in text-select token-select/index.jsx
95
+ * The upstream imports TokenTypes from ./token but doesn't re-export it,
96
+ * causing build failures when parent index.js tries to import it.
97
+ */
98
+ export function reexportTokenTypes(code, filePath) {
99
+ // Only apply to text-select token-select/index file
100
+ if (!filePath.includes('text-select') || !filePath.includes('token-select/index')) {
101
+ return code;
102
+ }
103
+ // Check if file imports TokenTypes from ./token
104
+ if (!code.includes("import Token, { TokenTypes } from './token'")) {
105
+ return code;
106
+ }
107
+ // Check if already exports TokenTypes
108
+ if (code.includes('export { TokenTypes }')) {
109
+ return code;
110
+ }
111
+ // Add export after the default export
112
+ return code.replace(/^export default TokenSelect;$/m, 'export default TokenSelect;\nexport { TokenTypes };');
113
+ }
114
+ /**
115
+ * Replace configure/lib/defaults import with an empty defaults object
116
+ * when configure directory is not synced (ESM-incompatible due to Slate v0.x)
117
+ */
118
+ export function inlineConfigureDefaults(code) {
119
+ // Only apply to files that import from ../configure/lib/defaults
120
+ const configureImportRegex = /import\s+(\w+)\s+from\s+['"](\.\.\/configure\/lib\/defaults|\.\.\/\.\.\/configure\/lib\/defaults)['"]/;
121
+ const match = code.match(configureImportRegex);
122
+ if (!match)
123
+ return code;
124
+ const importName = match[1];
125
+ // Replace with minimal defaults object
126
+ // The configuration property is what's typically used in student-facing code
127
+ const inlinedDefaults = `// Inlined from configure/lib/defaults (configure/ not synced - ESM-incompatible)
128
+ const ${importName} = {
129
+ configuration: {
130
+ // Minimal configuration for student-facing UI
131
+ // Full authoring configuration is only needed in the configure package
132
+ } as any
133
+ };`;
134
+ return code.replace(configureImportRegex, inlinedDefaults);
135
+ }
136
+ /**
137
+ * Determine if a file should have .tsx extension based on content
138
+ */
139
+ export function shouldUseTsxExtension(content, originalPath) {
140
+ // If original file was .jsx, use .tsx
141
+ if (originalPath.endsWith('.jsx')) {
142
+ return true;
143
+ }
144
+ // If content contains JSX, use .tsx
145
+ if (containsJsx(content)) {
146
+ return true;
147
+ }
148
+ // Otherwise use .ts
149
+ return false;
150
+ }
151
+ /**
152
+ * Convert CommonJS module.exports to ES module exports
153
+ *
154
+ * Handles patterns like:
155
+ * module.exports = { foo: ..., bar: ... }
156
+ *
157
+ * Converts to:
158
+ * export const foo = ...;
159
+ * export const bar = ...;
160
+ */
161
+ export function convertModuleExportsToEsm(content) {
162
+ // Match: module.exports = { key: value, key2: value2, ... };
163
+ const moduleExportsPattern = /module\.exports\s*=\s*\{([\s\S]*?)\};?/;
164
+ const match = content.match(moduleExportsPattern);
165
+ if (!match) {
166
+ return content;
167
+ }
168
+ const [fullMatch, objectContent] = match;
169
+ // Parse the object content into key-value pairs
170
+ // Match patterns like: key: value, (with value possibly spanning multiple lines)
171
+ const exports = [];
172
+ const lines = objectContent.split('\n');
173
+ let currentKey = null;
174
+ let currentValue = [];
175
+ for (const line of lines) {
176
+ const trimmed = line.trim();
177
+ if (!trimmed || trimmed === '{' || trimmed === '}')
178
+ continue;
179
+ // Check if this line starts a new key
180
+ const keyMatch = trimmed.match(/^(\w+):\s*(.*)/);
181
+ if (keyMatch) {
182
+ // Save previous export if any
183
+ if (currentKey) {
184
+ const value = currentValue.join('\n').replace(/,\s*$/, ''); // Remove trailing comma
185
+ exports.push(`export const ${currentKey} =\n${value};`);
186
+ }
187
+ // Start new export
188
+ currentKey = keyMatch[1];
189
+ currentValue = [keyMatch[2]];
190
+ }
191
+ else {
192
+ // Continuation of current value
193
+ currentValue.push(line);
194
+ }
195
+ }
196
+ // Save last export
197
+ if (currentKey) {
198
+ const value = currentValue.join('\n').replace(/,\s*$/, '');
199
+ exports.push(`export const ${currentKey} =\n${value};`);
200
+ }
201
+ // Replace the module.exports block with ES module exports
202
+ const esmExports = exports.join('\n\n');
203
+ return content.replace(fullMatch, esmExports);
204
+ }
205
+ /**
206
+ * Transform lodash imports to lodash-es for ESM compatibility
207
+ *
208
+ * Handles all lodash import patterns:
209
+ * - Individual module imports: import isEmpty from 'lodash/isEmpty'
210
+ * - Named imports: import { isEmpty, isEqual } from 'lodash'
211
+ * - Default import: import _ from 'lodash'
212
+ * - Namespace import: import * as _ from 'lodash'
213
+ *
214
+ * Converts them to use lodash-es which is a pure ESM package.
215
+ */
216
+ export function transformLodashToLodashEs(content) {
217
+ let transformed = content;
218
+ // Pattern 1: Individual module imports
219
+ // import isEmpty from 'lodash/isEmpty' → import { isEmpty } from 'lodash-es'
220
+ // import cloneDeep from 'lodash/cloneDeep' → import { cloneDeep } from 'lodash-es'
221
+ transformed = transformed.replace(/import\s+(\w+)\s+from\s+['"]lodash\/(\w+)['"]/g, "import { $1 } from 'lodash-es'");
222
+ // Pattern 2: Named imports, default imports, and namespace imports from main module
223
+ // import { isEmpty, isEqual } from 'lodash' → import { isEmpty, isEqual } from 'lodash-es'
224
+ // import _ from 'lodash' → import _ from 'lodash-es'
225
+ // import * as _ from 'lodash' → import * as _ from 'lodash-es'
226
+ transformed = transformed.replace(/from\s+['"]lodash['"]/g, "from 'lodash-es'");
227
+ return transformed;
228
+ }
229
+ /**
230
+ * Transform package.json dependencies from lodash to lodash-es
231
+ *
232
+ * Replaces lodash with lodash-es version 4.17.22 (latest ESM version)
233
+ * Also removes @types/lodash as lodash-es includes built-in TypeScript types
234
+ */
235
+ export function transformPackageJsonLodash(packageJson) {
236
+ const transformed = { ...packageJson };
237
+ // Replace lodash with lodash-es in dependencies
238
+ if (transformed.dependencies?.lodash) {
239
+ transformed.dependencies['lodash-es'] = '^4.17.22';
240
+ delete transformed.dependencies.lodash;
241
+ }
242
+ // Replace lodash with lodash-es in devDependencies
243
+ if (transformed.devDependencies?.lodash) {
244
+ transformed.devDependencies['lodash-es'] = '^4.17.22';
245
+ delete transformed.devDependencies.lodash;
246
+ }
247
+ // Remove @types/lodash if present (lodash-es has built-in types)
248
+ if (transformed.dependencies?.['@types/lodash']) {
249
+ delete transformed.dependencies['@types/lodash'];
250
+ }
251
+ if (transformed.devDependencies?.['@types/lodash']) {
252
+ delete transformed.devDependencies['@types/lodash'];
253
+ }
254
+ return transformed;
255
+ }
256
+ /**
257
+ * Transform recharts version in package.json
258
+ *
259
+ * Upgrades recharts from 2.x to 3.x for ESM compatibility.
260
+ * Recharts 3.x removes the lodash dependency and is fully ESM-compatible.
261
+ */
262
+ export function transformPackageJsonRecharts(packageJson) {
263
+ const transformed = { ...packageJson };
264
+ // Upgrade recharts to 3.x in dependencies
265
+ if (transformed.dependencies?.recharts) {
266
+ // Only upgrade if it's 2.x
267
+ const version = transformed.dependencies.recharts;
268
+ if (version.includes('2.')) {
269
+ transformed.dependencies.recharts = '^3.7.0';
270
+ }
271
+ }
272
+ return transformed;
273
+ }
274
+ /**
275
+ * Transform @pie-framework event package imports to internal packages
276
+ *
277
+ * Handles:
278
+ * - @pie-framework/pie-player-events → @pie-element/shared-player-events
279
+ * - @pie-framework/pie-configure-events → @pie-element/shared-configure-events
280
+ */
281
+ export function transformPieFrameworkEventImports(content) {
282
+ let transformed = content;
283
+ // Transform pie-player-events imports
284
+ transformed = transformed.replace(/from\s+['"]@pie-framework\/pie-player-events['"]/g, "from '@pie-element/shared-player-events'");
285
+ // Transform pie-configure-events imports
286
+ transformed = transformed.replace(/from\s+['"]@pie-framework\/pie-configure-events['"]/g, "from '@pie-element/shared-configure-events'");
287
+ return transformed;
288
+ }
289
+ /**
290
+ * Transform @pie-lib/controller-utils imports to internal @pie-element/shared-controller-utils
291
+ *
292
+ * Handles:
293
+ * - @pie-lib/controller-utils → @pie-element/shared-controller-utils
294
+ *
295
+ * Our internal controller-utils package has the same API but with modernized TypeScript
296
+ * and no lodash/debug dependencies.
297
+ */
298
+ export function transformControllerUtilsImports(content) {
299
+ return content.replace(/from\s+['"]@pie-lib\/controller-utils['"]/g, "from '@pie-element/shared-controller-utils'");
300
+ }
301
+ /**
302
+ * Transform @pie-lib shared package imports to internal @pie-element/shared-* packages
303
+ *
304
+ * Handles:
305
+ * - @pie-lib/math-rendering → @pie-element/shared-math-rendering-mathjax
306
+ *
307
+ * These packages have been moved to shared/ for better version control and consistency.
308
+ */
309
+ export function transformSharedPackageImports(content) {
310
+ let transformed = content;
311
+ // Transform math-rendering
312
+ transformed = transformed.replace(/from\s+['"]@pie-lib\/math-rendering['"]/g, "from '@pie-element/shared-math-rendering-mathjax'");
313
+ // Transform feedback
314
+ transformed = transformed.replace(/from\s+['"]@pie-lib\/feedback['"]/g, "from '@pie-element/shared-feedback'");
315
+ return transformed;
316
+ }
317
+ /**
318
+ * Transform @pie-framework/mathquill imports to internal @pie-element/shared-mathquill
319
+ *
320
+ * Handles:
321
+ * - @pie-framework/mathquill → @pie-element/shared-mathquill
322
+ *
323
+ * Our internal mathquill package is a fork from the PIE org with matrix support,
324
+ * accessibility features, and modernized ESM code.
325
+ */
326
+ export function transformMathquillImports(content) {
327
+ return content.replace(/from\s+['"]@pie-framework\/mathquill['"]/g, "from '@pie-element/shared-mathquill'");
328
+ }
329
+ /**
330
+ * Transform package.json dependencies for PIE Framework event packages
331
+ *
332
+ * Replaces external @pie-framework event packages with internal workspace packages
333
+ */
334
+ export function transformPackageJsonPieEvents(packageJson) {
335
+ const transformed = { ...packageJson };
336
+ // Replace @pie-framework/pie-player-events with internal package
337
+ if (transformed.dependencies?.['@pie-framework/pie-player-events']) {
338
+ transformed.dependencies['@pie-element/shared-player-events'] = 'workspace:*';
339
+ delete transformed.dependencies['@pie-framework/pie-player-events'];
340
+ }
341
+ if (transformed.devDependencies?.['@pie-framework/pie-player-events']) {
342
+ transformed.devDependencies['@pie-element/shared-player-events'] = 'workspace:*';
343
+ delete transformed.devDependencies['@pie-framework/pie-player-events'];
344
+ }
345
+ // Replace @pie-framework/pie-configure-events with internal package
346
+ if (transformed.dependencies?.['@pie-framework/pie-configure-events']) {
347
+ transformed.dependencies['@pie-element/shared-configure-events'] = 'workspace:*';
348
+ delete transformed.dependencies['@pie-framework/pie-configure-events'];
349
+ }
350
+ if (transformed.devDependencies?.['@pie-framework/pie-configure-events']) {
351
+ transformed.devDependencies['@pie-element/shared-configure-events'] = 'workspace:*';
352
+ delete transformed.devDependencies['@pie-framework/pie-configure-events'];
353
+ }
354
+ return transformed;
355
+ }
356
+ /**
357
+ * Transform package.json dependencies for @pie-lib/controller-utils
358
+ *
359
+ * Replaces @pie-lib/controller-utils with internal @pie-element/shared-controller-utils
360
+ */
361
+ export function transformPackageJsonControllerUtils(packageJson) {
362
+ const transformed = { ...packageJson };
363
+ // Replace @pie-lib/controller-utils with internal package
364
+ if (transformed.dependencies?.['@pie-lib/controller-utils']) {
365
+ transformed.dependencies['@pie-element/shared-controller-utils'] = 'workspace:*';
366
+ delete transformed.dependencies['@pie-lib/controller-utils'];
367
+ }
368
+ if (transformed.devDependencies?.['@pie-lib/controller-utils']) {
369
+ transformed.devDependencies['@pie-element/shared-controller-utils'] = 'workspace:*';
370
+ delete transformed.devDependencies['@pie-lib/controller-utils'];
371
+ }
372
+ return transformed;
373
+ }
374
+ /**
375
+ * Transform package.json dependencies for shared packages
376
+ *
377
+ * Replaces @pie-lib shared packages with internal @pie-element/shared-* packages
378
+ */
379
+ export function transformPackageJsonSharedPackages(packageJson) {
380
+ const transformed = { ...packageJson };
381
+ const sharedPackages = {
382
+ '@pie-lib/math-rendering': '@pie-element/shared-math-rendering-mathjax',
383
+ '@pie-lib/feedback': '@pie-element/shared-feedback',
384
+ };
385
+ for (const [oldPkg, newPkg] of Object.entries(sharedPackages)) {
386
+ // Replace in dependencies
387
+ if (transformed.dependencies?.[oldPkg]) {
388
+ transformed.dependencies[newPkg] = 'workspace:*';
389
+ delete transformed.dependencies[oldPkg];
390
+ }
391
+ // Replace in devDependencies
392
+ if (transformed.devDependencies?.[oldPkg]) {
393
+ transformed.devDependencies[newPkg] = 'workspace:*';
394
+ delete transformed.devDependencies[oldPkg];
395
+ }
396
+ }
397
+ return transformed;
398
+ }
399
+ /**
400
+ * Transform package.json dependencies for @pie-framework/mathquill
401
+ *
402
+ * Replaces @pie-framework/mathquill with internal @pie-element/shared-mathquill
403
+ */
404
+ export function transformPackageJsonMathquill(packageJson) {
405
+ const transformed = { ...packageJson };
406
+ // Replace @pie-framework/mathquill with internal package
407
+ if (transformed.dependencies?.['@pie-framework/mathquill']) {
408
+ transformed.dependencies['@pie-element/shared-mathquill'] = 'workspace:*';
409
+ delete transformed.dependencies['@pie-framework/mathquill'];
410
+ }
411
+ if (transformed.devDependencies?.['@pie-framework/mathquill']) {
412
+ transformed.devDependencies['@pie-element/shared-mathquill'] = 'workspace:*';
413
+ delete transformed.devDependencies['@pie-framework/mathquill'];
414
+ }
415
+ return transformed;
416
+ }
417
+ /**
418
+ * Transform direct property assignment patterns to use assignProps utility
419
+ *
420
+ * Replaces patterns like:
421
+ * Object.entries(props).forEach(([key, value]) => { element[key] = value; });
422
+ * With:
423
+ * assignProps(element, props);
424
+ *
425
+ * And adds the import if not present.
426
+ *
427
+ * NOTE: This transform is available but NOT enabled in the sync pipeline by default.
428
+ * Only enable it if this pattern appears frequently in upstream code and needs
429
+ * to be refactored automatically. For one-off cases, manual refactoring is preferred.
430
+ *
431
+ * To enable: Import and call this in sync-pielib-strategy.ts after other transforms.
432
+ */
433
+ export function transformToAssignProps(content) {
434
+ let transformed = content;
435
+ let needsImport = false;
436
+ // Pattern 1: Object.entries(props).forEach with element[key] = value
437
+ // Matches: Object.entries(props).forEach(([key, value]) => { element[key] = value; });
438
+ const pattern1 = /Object\.entries\((\w+)\)\.forEach\(\(\[(\w+),\s*(\w+)\]\)\s*=>\s*\{\s*(\w+)\[\2\]\s*=\s*\3;\s*\}\);?/g;
439
+ const matches1 = Array.from(transformed.matchAll(pattern1));
440
+ for (const match of matches1) {
441
+ const [fullMatch, propsVar, _keyVar, _valueVar, elementVar] = match;
442
+ const replacement = `assignProps(${elementVar}, ${propsVar});`;
443
+ transformed = transformed.replace(fullMatch, replacement);
444
+ needsImport = true;
445
+ }
446
+ // Pattern 2: for...of loop with Object.entries
447
+ // Matches: for (const [key, value] of Object.entries(props)) { element[key] = value; }
448
+ const pattern2 = /for\s*\(const\s*\[(\w+),\s*(\w+)\]\s*of\s*Object\.entries\((\w+)\)\)\s*\{\s*(\w+)\[\1\]\s*=\s*\2;\s*\}/g;
449
+ const matches2 = Array.from(transformed.matchAll(pattern2));
450
+ for (const match of matches2) {
451
+ const [fullMatch, _keyVar, _valueVar, propsVar, elementVar] = match;
452
+ const replacement = `assignProps(${elementVar}, ${propsVar});`;
453
+ transformed = transformed.replace(fullMatch, replacement);
454
+ needsImport = true;
455
+ }
456
+ // Add import if needed and not already present
457
+ if (needsImport && !transformed.includes("from '@pie-element/shared-utils'")) {
458
+ // Find where to insert the import (after other imports)
459
+ const importMatch = transformed.match(/(import\s+.*?from\s+['"].*?['"];?\s*\n)+/);
460
+ if (importMatch && importMatch.index !== undefined) {
461
+ const lastImportEnd = importMatch.index + importMatch[0].length;
462
+ transformed =
463
+ transformed.slice(0, lastImportEnd) +
464
+ "import { assignProps } from '@pie-element/shared-utils';\n" +
465
+ transformed.slice(lastImportEnd);
466
+ }
467
+ else {
468
+ // No imports found, add at the beginning (after any leading comments)
469
+ const firstNonCommentLine = transformed.search(/^(?!\/\/|\/\*|\*|$)/m);
470
+ if (firstNonCommentLine !== -1) {
471
+ transformed =
472
+ transformed.slice(0, firstNonCommentLine) +
473
+ "import { assignProps } from '@pie-element/shared-utils';\n\n" +
474
+ transformed.slice(firstNonCommentLine);
475
+ }
476
+ }
477
+ }
478
+ return transformed;
479
+ }
480
+ /**
481
+ * Transform configure utility imports for flattened directory structure
482
+ *
483
+ * In upstream pie-elements, configure utility files live at configure/utils.js
484
+ * and are imported from configure/src/ files using '../utils'.
485
+ *
486
+ * In the synced structure, both configure/src/ files and configure/utils.js
487
+ * are synced to src/configure/, so the import path needs to change from
488
+ * '../utils' to './utils'.
489
+ *
490
+ * This transform handles:
491
+ * - import { x } from '../utils' → import { x } from './utils'
492
+ * - Only applies to files directly in configure/src/, not subdirectories
493
+ * - Only when the import is '../utils' (not '../utils/something')
494
+ */
495
+ export function transformConfigureUtilsImports(content, relativePath) {
496
+ // Only apply to files directly in configure/src/ (not in subdirectories)
497
+ // Note: In the target, these files end up in author/ directory
498
+ // Match patterns like:
499
+ // - "configure/src/something.js" (yes)
500
+ // - "configure/src/design/something.js" (no - has subdirectory)
501
+ const configureRootFilePattern = /^configure\/src\/[^/]+\.(jsx?|tsx?)$/;
502
+ if (!configureRootFilePattern.test(relativePath)) {
503
+ return content;
504
+ }
505
+ // Transform '../utils' to './utils' (only exact match, not '../utils/something')
506
+ let transformed = content;
507
+ transformed = transformed.replace(/from\s+['"]\.\.\/utils['"]/g, "from './utils'");
508
+ return transformed;
509
+ }
510
+ /**
511
+ * Transform SSR-unsafe require() calls to standard ESM imports
512
+ *
513
+ * Upstream pie-lib code uses this pattern for SSR compatibility with MathQuill:
514
+ *
515
+ * ```js
516
+ * // - mathquill error window not defined
517
+ * let EditableHtml;
518
+ * let StyledEditableHTML;
519
+ * if (typeof window !== 'undefined') {
520
+ * EditableHtml = require('@pie-lib/editable-html-tip-tap')['default'];
521
+ * StyledEditableHTML = styled(EditableHtml)(({ theme }) => ({
522
+ * fontFamily: theme.typography.fontFamily,
523
+ * }));
524
+ * }
525
+ * ```
526
+ *
527
+ * This doesn't work in browser ESM (require is not defined). Transform to:
528
+ *
529
+ * ```tsx
530
+ * import EditableHtmlImport from '@pie-lib/editable-html-tip-tap';
531
+ *
532
+ * const EditableHtml = EditableHtmlImport;
533
+ * const StyledEditableHTML = styled(EditableHtml)(({ theme }) => ({
534
+ * fontFamily: theme.typography.fontFamily,
535
+ * }));
536
+ * ```
537
+ *
538
+ * We use direct imports instead of React.lazy because:
539
+ * - Simpler code (no Suspense wrapper needed)
540
+ * - No loading flicker
541
+ * - Modern bundlers handle ESM imports correctly for SSR
542
+ * - The component is always needed (no code-splitting benefit)
543
+ */
544
+ export function transformSsrRequireToReactLazy(content) {
545
+ let transformed = content;
546
+ // Pattern 1: SSR check with require() for editable-html-tip-tap with styled wrapper
547
+ // Matches:
548
+ // // - mathquill error window not defined
549
+ // let EditableHtml;
550
+ // let StyledEditableHTML;
551
+ // if (typeof window !== 'undefined') {
552
+ // EditableHtml = require('@pie-lib/editable-html-tip-tap')['default'];
553
+ // StyledEditableHTML = styled(EditableHtml)(...);
554
+ // }
555
+ //
556
+ // Note: styled() call may span multiple lines
557
+ const ssrRequirePattern = /(?:\/\/import\s+\w+\s+from\s+['"]@pie-lib\/[^'"]+['"]\s*;\s*\n)?let\s+(\w+);\s*\nlet\s+(\w+);\s*\n(?:\/\/\s*-\s*mathquill\s+error\s+window\s+not\s+defined\s*\n)?if\s*\(\s*typeof\s+window\s*!==\s*['"]undefined['"]\s*\)\s*\{\s*\n\s*\1\s*=\s*require\(['"](@pie-lib\/[^'"]+)['"]\)\[['"]default['"]\];\s*\n\s*\2\s*=\s*styled\(\1\)\(([\s\S]*?)\);\s*\n\s*\}/;
558
+ const match = transformed.match(ssrRequirePattern);
559
+ if (match) {
560
+ const [fullMatch, componentVar, styledVar, importPath, styleParams] = match;
561
+ // Generate direct import replacement (better than React.lazy - simpler, no Suspense needed)
562
+ const importVarName = `${componentVar}Import`;
563
+ const replacement = `import ${importVarName} from '${importPath}';
564
+
565
+ const ${componentVar} = ${importVarName};
566
+ const ${styledVar} = styled(${componentVar})(${styleParams});`;
567
+ transformed = transformed.replace(fullMatch, replacement);
568
+ }
569
+ // Pattern 2: Simpler pattern without styled wrapper
570
+ // Matches:
571
+ // //import EditableHTML from '@pie-lib/editable-html-tip-tap';
572
+ // let EditableHtml;
573
+ // if (typeof window !== 'undefined') {
574
+ // EditableHtml = require('@pie-lib/editable-html-tip-tap')['default'];
575
+ // }
576
+ const simpleRequirePattern = /(?:\/\/import\s+\w+\s+from\s+['"]@pie-lib\/[^'"]+['"]\s*;\s*\n)?let\s+(\w+);\s*\nif\s*\(\s*typeof\s+window\s*!==\s*['"]undefined['"]\s*\)\s*\{\s*\n\s*\1\s*=\s*require\(['"](@pie-lib\/[^'"]+)['"]\)\[['"]default['"]\];\s*\n\s*\}/;
577
+ const simpleMatch = transformed.match(simpleRequirePattern);
578
+ if (simpleMatch && !match) {
579
+ // Only apply if we didn't already match the styled pattern
580
+ const [fullMatch, componentVar, importPath] = simpleMatch;
581
+ // Generate direct import replacement (better than React.lazy - simpler, no Suspense needed)
582
+ const importVarName = `${componentVar}Import`;
583
+ const replacement = `import ${importVarName} from '${importPath}';
584
+
585
+ const ${componentVar} = ${importVarName};`;
586
+ transformed = transformed.replace(fullMatch, replacement);
587
+ }
588
+ return transformed;
589
+ }
590
+ /**
591
+ * Fix TypeScript type inference errors for styled components
592
+ *
593
+ * TypeScript can't infer types for some MUI styled components, causing errors like:
594
+ * "The inferred type of 'X' cannot be named without a reference to '@emotion/styled'"
595
+ *
596
+ * This transform adds explicit type annotations to fix these errors.
597
+ */
598
+ export function fixStyledComponentTypes(content) {
599
+ let transformed = content;
600
+ // Pattern 1: Export const styled components (e.g., export const StyledFormControlLabel = styled(...))
601
+ // Add `: any` to fix type inference issues
602
+ // Match any identifier, not just those starting with "Styled"
603
+ const exportConstStyledRegex = /export const (\w+) = styled\(/g;
604
+ transformed = transformed.replace(exportConstStyledRegex, 'export const $1: any = styled(');
605
+ // Pattern 2: Const styled components (e.g., const StyledToken = styled(...), const MiniField = styled(...))
606
+ // Add `: any` to fix type inference issues
607
+ // Match any identifier, not just those starting with "Styled"
608
+ const constStyledRegex = /const (\w+) = styled\(/g;
609
+ transformed = transformed.replace(constStyledRegex, 'const $1: any = styled(');
610
+ // Pattern 3: Class methods/arrow functions that might return styled components
611
+ // Add `: any` return type to methods that are missing type annotations
612
+ // Match: methodName = () => { or methodName = (params) => {
613
+ // But only at start of line (class methods), not property assignments like reader.onload =
614
+ // And only if they don't already have a type annotation
615
+ const methodRegex = /^(\s*)(\w+)\s*=\s*\([^)]*\)\s*=>\s*\{/gm;
616
+ transformed = transformed.replace(methodRegex, (match) => {
617
+ // Check if already has type annotation (: type = )
618
+ const hasTypeAnnotation = match.includes(':');
619
+ if (hasTypeAnnotation) {
620
+ return match;
621
+ }
622
+ // Add `: any` type annotation
623
+ return match.replace(/(\w+)(\s*=)/, '$1: any$2');
624
+ });
625
+ return transformed;
626
+ }
627
+ /**
628
+ * Transform self-referential package imports to relative imports
629
+ *
630
+ * In upstream pie-elements, configure files sometimes import from their own package
631
+ * using the package name (e.g., import { X } from '@pie-element/fraction-model').
632
+ * This creates circular dependency issues in the build.
633
+ *
634
+ * This transform converts these self-referential imports to relative paths based on
635
+ * the package structure:
636
+ * - @pie-element/{name} → relative path to delivery component
637
+ *
638
+ * Example:
639
+ * In configure/src/main.jsx:
640
+ * import { FractionModelChart } from '@pie-element/fraction-model'
641
+ * Becomes:
642
+ * import FractionModelChart from '../delivery/fraction-model-chart'
643
+ *
644
+ * @param content - The file content
645
+ * @param packageName - The current package name (e.g., '@pie-element/fraction-model')
646
+ * @param sourceFilePath - The source file path relative to package root
647
+ * @returns Transformed content with relative imports
648
+ */
649
+ export function transformSelfReferentialImports(content, packageName, sourceFilePath) {
650
+ let transformed = content;
651
+ // Extract element name from package name (@pie-element/fraction-model → fraction-model)
652
+ const elementName = packageName.replace('@pie-element/', '');
653
+ // Pattern: import { X } from '@pie-element/element-name' or import X from '@pie-element/element-name'
654
+ const selfImportPattern = new RegExp(`import\\s+(?:\\{\\s*([^}]+)\\s*\\}|([\\w]+))\\s+from\\s+['"]@pie-element/${elementName}['"]`, 'g');
655
+ const matches = Array.from(transformed.matchAll(selfImportPattern));
656
+ for (const match of matches) {
657
+ const [fullMatch, namedImports, defaultImport] = match;
658
+ // Determine source directory (configure/src → author, src → delivery)
659
+ let targetPath = '';
660
+ if (sourceFilePath.startsWith('configure/src/')) {
661
+ // Configure files reference delivery components
662
+ // configure/src/main.jsx → author/main.tsx
663
+ // Relative path from author/ to delivery/
664
+ targetPath = '../delivery';
665
+ }
666
+ else if (sourceFilePath.startsWith('src/')) {
667
+ // Main src files shouldn't self-reference, but handle it anyway
668
+ targetPath = '.';
669
+ }
670
+ else {
671
+ // Unknown structure, skip
672
+ continue;
673
+ }
674
+ // Strategy: Self-referential imports typically come from the package's main
675
+ // index file which re-exports from various locations. Rather than trying to
676
+ // guess individual file locations (which often fails for namespace exports
677
+ // and re-exports), we keep the import pointing to the index (delivery folder)
678
+ // which handles all re-exports correctly.
679
+ //
680
+ // Examples:
681
+ // - import { NumberLineComponent, dataConverter, tickUtils } from '@pie-element/number-line'
682
+ // → import { NumberLineComponent, dataConverter, tickUtils } from '../delivery'
683
+ // - import FractionModel from '@pie-element/fraction-model'
684
+ // → import FractionModel from '../delivery'
685
+ if (namedImports) {
686
+ // Named imports - keep them as a single import from the index
687
+ const replacement = `import { ${namedImports} } from '${targetPath}'`;
688
+ transformed = transformed.replace(fullMatch, replacement);
689
+ }
690
+ else if (defaultImport) {
691
+ // Default import - reference main index
692
+ const replacement = `import ${defaultImport} from '${targetPath}'`;
693
+ transformed = transformed.replace(fullMatch, replacement);
694
+ }
695
+ }
696
+ return transformed;
697
+ }
698
+ /**
699
+ * Fix type inference errors for exported functions with complex return types
700
+ *
701
+ * TypeScript can't infer types for some exported functions that have complex dependencies,
702
+ * causing errors like: "The inferred type of 'X' cannot be named without a reference to 'Y'"
703
+ *
704
+ * This transform adds explicit `: any` type annotations to fix these portability errors.
705
+ */
706
+ export function fixExportedFunctionTypes(content, sourcePath) {
707
+ let transformed = content;
708
+ // Only apply to specific files that are known to have issues
709
+ if (sourcePath?.includes('charting') && sourcePath.includes('utils')) {
710
+ // Fix: export const dataToXBand = (...) => { ... }
711
+ // Pattern: export const functionName = (params) => {
712
+ transformed = transformed.replace(/export const dataToXBand = \(/g, 'export const dataToXBand: any = (');
713
+ }
714
+ // Fix translator package default export type inference issue
715
+ // The spread operator `...i18next` causes TypeScript to require a reference to i18next types
716
+ if (sourcePath?.includes('translator') && sourcePath?.includes('/src/index')) {
717
+ // Add type imports
718
+ if (!transformed.includes('type i18n')) {
719
+ transformed = transformed.replace(/import i18next from 'i18next';/, "import i18next, { type i18n, type TOptions } from 'i18next';");
720
+ }
721
+ // Add type annotations before the export default
722
+ if (!transformed.includes('interface Translator')) {
723
+ const typeAnnotations = `
724
+ interface Translator extends i18n {
725
+ t: (key: string, options: TOptions) => string;
726
+ }
727
+
728
+ interface TranslatorModule {
729
+ translator: Translator;
730
+ languageOptions: Array<{ value: string; label: string }>;
731
+ }
732
+
733
+ const translatorModule: TranslatorModule = `;
734
+ transformed = transformed.replace(/export default \{/, typeAnnotations + '{');
735
+ // Replace the export statement
736
+ transformed = transformed.replace(/\n\};$/m, '\n};\n\nexport default translatorModule;');
737
+ }
738
+ }
739
+ return transformed;
740
+ }
741
+ /**
742
+ * Transform MUI Menu imports to use InlineMenu from @pie-lib/render-ui
743
+ *
744
+ * Replaces direct imports of Menu from '@mui/material/Menu' with InlineMenu
745
+ * from '@pie-lib/render-ui', which includes fixes for the backdrop overlay issue.
746
+ *
747
+ * This addresses a common issue where MUI's Menu component covers the entire screen
748
+ * with a white background when used in inline contexts (dropdowns within text).
749
+ *
750
+ * Transformations:
751
+ * - import Menu from '@mui/material/Menu' → import { InlineMenu as Menu } from '@pie-lib/render-ui'
752
+ * - import Menu, { MenuProps } from '@mui/material/Menu' → import { InlineMenu as Menu } from '@pie-lib/render-ui'; import type { MenuProps } from '@mui/material/Menu'
753
+ * - styled(Menu) continues to work as Menu is aliased to InlineMenu
754
+ *
755
+ * @param content - Source code content
756
+ * @returns Transformed content with Menu imports replaced
757
+ */
758
+ export function transformMenuToInlineMenu(content) {
759
+ let transformed = content;
760
+ // Pattern 1: import Menu from '@mui/material/Menu'
761
+ // → import { InlineMenu as Menu } from '@pie-lib/render-ui'
762
+ if (/import\s+Menu\s+from\s+['"]@mui\/material\/Menu['"]/.test(transformed)) {
763
+ transformed = transformed.replace(/import\s+Menu\s+from\s+['"]@mui\/material\/Menu['"]/g, "import { InlineMenu as Menu } from '@pie-lib/render-ui'");
764
+ }
765
+ // Pattern 2: import Menu, { MenuProps } from '@mui/material/Menu'
766
+ // → import { InlineMenu as Menu } from '@pie-lib/render-ui'
767
+ // import type { MenuProps } from '@mui/material/Menu'
768
+ const namedImportMatch = transformed.match(/import\s+Menu\s*,\s*\{([^}]+)\}\s+from\s+['"]@mui\/material\/Menu['"]/);
769
+ if (namedImportMatch) {
770
+ const namedImports = namedImportMatch[1];
771
+ transformed = transformed.replace(/import\s+Menu\s*,\s*\{([^}]+)\}\s+from\s+['"]@mui\/material\/Menu['"]/, `import { InlineMenu as Menu } from '@pie-lib/render-ui';\nimport type {${namedImports}} from '@mui/material/Menu'`);
772
+ }
773
+ return transformed;
774
+ }
775
+ /**
776
+ * Add InlineMenu export to render-ui index file
777
+ *
778
+ * The InlineMenu component is a pie-elements-ng specific component that wraps
779
+ * MUI's Menu to fix the backdrop overlay issue. Since it doesn't exist in the
780
+ * upstream pie-lib, we need to add its export after the sync completes.
781
+ *
782
+ * This transform adds the InlineMenu export to the end of the render-ui index
783
+ * file if it's not already present.
784
+ *
785
+ * @param content - Source code content
786
+ * @param sourcePath - Path to the source file (to identify render-ui index)
787
+ * @returns Transformed content with InlineMenu export added if needed
788
+ */
789
+ export function addInlineMenuExport(content, sourcePath) {
790
+ // Only apply to render-ui root index file (not subdirectories like collapsible/index.tsx)
791
+ // sourcePath format: pie-lib/packages/render-ui/src/index.js
792
+ if (!sourcePath?.includes('render-ui/src/index.js')) {
793
+ return content;
794
+ }
795
+ // Check if InlineMenu export already exists
796
+ if (content.includes('InlineMenu')) {
797
+ return content;
798
+ }
799
+ // Add InlineMenu export at the end of the file
800
+ const exportStatement = "\n// Non-synced pie-elements-ng exports\nexport { InlineMenu } from './inline-menu';\n";
801
+ return content.trimEnd() + exportStatement;
802
+ }
803
+ /**
804
+ * Transform MathQuill.getInterface(2) to getInterface(3)
805
+ *
806
+ * Interface version 2 requires jQuery, but pie-elements-ng uses interface version 3
807
+ * which has a jQuery-free API. This transform updates calls to use version 3.
808
+ *
809
+ * @param content - Source code content
810
+ * @returns Transformed content with getInterface(3) instead of getInterface(2)
811
+ */
812
+ export function transformMathQuillInterface(content) {
813
+ // Replace MathQuill.getInterface(2) with MathQuill.getInterface(3)
814
+ return content.replace(/MathQuill\.getInterface\(2\)/g, 'MathQuill.getInterface(3)');
815
+ }
816
+ //# sourceMappingURL=sync-imports.js.map