@keak/sdk 1.0.9 → 2.0.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.
- package/README.md +102 -7
- package/dist/Conversion.d.ts.map +1 -1
- package/dist/index.cjs.js +199 -187
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +55 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +201 -187
- package/dist/index.js.map +1 -1
- package/dist/toolbar/KeakToolbar.d.ts.map +1 -1
- package/dist/toolbar.js +41 -0
- package/dist/toolbar.js.map +1 -1
- package/package.json +2 -1
- package/src/cli/ai-helper.js +117 -39
- package/src/cli/install.js +251 -152
- package/src/plugins/babel-source-inject.cjs +55 -131
- package/src/plugins/next.cjs +48 -221
- package/src/plugins/webpack-loader-babel/index.js +43 -117
package/src/cli/ai-helper.js
CHANGED
|
@@ -12,7 +12,7 @@ class AIHelper {
|
|
|
12
12
|
content: fileContent,
|
|
13
13
|
fileName: filePath.split('/').pop(),
|
|
14
14
|
framework: framework,
|
|
15
|
-
task: '
|
|
15
|
+
task: 'add_keak_hook'
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
const options = {
|
|
@@ -111,12 +111,10 @@ class AIHelper {
|
|
|
111
111
|
|
|
112
112
|
// Fallback AI logic when API is not available
|
|
113
113
|
class LocalAIHelper {
|
|
114
|
-
|
|
114
|
+
addKeakHook(content) {
|
|
115
115
|
const lines = content.split('\n');
|
|
116
116
|
const result = [];
|
|
117
117
|
let importAdded = false;
|
|
118
|
-
let inJSXReturn = false;
|
|
119
|
-
let jsxDepth = 0;
|
|
120
118
|
let lastImportLine = -1;
|
|
121
119
|
|
|
122
120
|
// First pass: find last import
|
|
@@ -129,76 +127,156 @@ class LocalAIHelper {
|
|
|
129
127
|
// Add import after last import
|
|
130
128
|
for (let i = 0; i < lines.length; i++) {
|
|
131
129
|
result.push(lines[i]);
|
|
132
|
-
|
|
130
|
+
|
|
133
131
|
if (i === lastImportLine && !importAdded) {
|
|
134
|
-
result.push("import {
|
|
132
|
+
result.push("import { useKeak, KeakToolbar } from '@keak/sdk';");
|
|
135
133
|
importAdded = true;
|
|
136
134
|
}
|
|
137
135
|
}
|
|
138
136
|
|
|
139
137
|
// If no imports found, add at the beginning
|
|
140
138
|
if (!importAdded) {
|
|
141
|
-
result.unshift("import {
|
|
139
|
+
result.unshift("import { useKeak, KeakToolbar } from '@keak/sdk';");
|
|
142
140
|
}
|
|
143
141
|
|
|
144
|
-
// Now
|
|
142
|
+
// Now add hook to the appropriate location
|
|
145
143
|
const finalContent = result.join('\n');
|
|
146
|
-
|
|
147
|
-
// Try different
|
|
144
|
+
|
|
145
|
+
// Try different strategies based on file type
|
|
148
146
|
if (finalContent.includes('ReactDOM.render')) {
|
|
149
147
|
return this.wrapReactDOMRender(finalContent);
|
|
150
148
|
} else if (finalContent.includes('createRoot')) {
|
|
151
149
|
return this.wrapCreateRoot(finalContent);
|
|
152
150
|
} else if (finalContent.includes('export default function')) {
|
|
153
|
-
return this.
|
|
151
|
+
return this.addHookToDefaultExport(finalContent);
|
|
154
152
|
} else {
|
|
155
|
-
return this.
|
|
153
|
+
return this.addHookToGenericComponent(finalContent);
|
|
156
154
|
}
|
|
157
155
|
}
|
|
158
156
|
|
|
159
157
|
wrapReactDOMRender(content) {
|
|
160
|
-
|
|
161
|
-
|
|
158
|
+
// For ReactDOM.render, create a KeakWrapper component with the hook
|
|
159
|
+
const wrapperComponent = `
|
|
160
|
+
// Keak wrapper component with hook
|
|
161
|
+
function KeakWrapper({ children }) {
|
|
162
|
+
useKeak({ debug: true });
|
|
163
|
+
return (
|
|
164
|
+
<>
|
|
165
|
+
{children}
|
|
166
|
+
<KeakToolbar position="bottom-right" />
|
|
167
|
+
</>
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
`;
|
|
171
|
+
|
|
172
|
+
// Insert wrapper component before ReactDOM.render
|
|
173
|
+
const renderIndex = content.indexOf('ReactDOM.render');
|
|
174
|
+
if (renderIndex === -1) return content;
|
|
175
|
+
|
|
176
|
+
const beforeRender = content.substring(0, renderIndex);
|
|
177
|
+
const afterRender = content.substring(renderIndex);
|
|
178
|
+
|
|
179
|
+
// Wrap the rendered component
|
|
180
|
+
const wrappedRender = afterRender.replace(
|
|
181
|
+
/ReactDOM\.render\s*\(\s*(<[\s\S]*?>[\s\S]*?<\/[\s\S]*?>)\s*,/,
|
|
162
182
|
`ReactDOM.render(
|
|
163
|
-
<
|
|
164
|
-
|
|
183
|
+
<KeakWrapper>
|
|
184
|
+
$1
|
|
185
|
+
</KeakWrapper>,`
|
|
165
186
|
);
|
|
187
|
+
|
|
188
|
+
return beforeRender + wrapperComponent + wrappedRender;
|
|
166
189
|
}
|
|
167
190
|
|
|
168
191
|
wrapCreateRoot(content) {
|
|
169
|
-
|
|
170
|
-
|
|
192
|
+
// For createRoot, create a KeakWrapper component with the hook
|
|
193
|
+
const wrapperComponent = `
|
|
194
|
+
// Keak wrapper component with hook
|
|
195
|
+
function KeakWrapper({ children }) {
|
|
196
|
+
useKeak({ debug: true });
|
|
197
|
+
return (
|
|
198
|
+
<>
|
|
199
|
+
{children}
|
|
200
|
+
<KeakToolbar position="bottom-right" />
|
|
201
|
+
</>
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
`;
|
|
205
|
+
|
|
206
|
+
// Insert wrapper component before root.render
|
|
207
|
+
const renderIndex = content.indexOf('root.render');
|
|
208
|
+
if (renderIndex === -1) return content;
|
|
209
|
+
|
|
210
|
+
const beforeRender = content.substring(0, renderIndex);
|
|
211
|
+
const afterRender = content.substring(renderIndex);
|
|
212
|
+
|
|
213
|
+
// Wrap the rendered component
|
|
214
|
+
const wrappedRender = afterRender.replace(
|
|
215
|
+
/root\.render\s*\(\s*(<[\s\S]*?>[\s\S]*?<\/[\s\S]*?>)\s*\)/,
|
|
171
216
|
`root.render(
|
|
172
|
-
<
|
|
173
|
-
|
|
217
|
+
<KeakWrapper>
|
|
218
|
+
$1
|
|
219
|
+
</KeakWrapper>)`
|
|
174
220
|
);
|
|
221
|
+
|
|
222
|
+
return beforeRender + wrapperComponent + wrappedRender;
|
|
175
223
|
}
|
|
176
224
|
|
|
177
|
-
|
|
178
|
-
//
|
|
179
|
-
const functionMatch = content.match(/(export\s+default\s+function\s+\w+\s*\([^)]*\)\s
|
|
180
|
-
|
|
225
|
+
addHookToDefaultExport(content) {
|
|
226
|
+
// For default export components, add the hook directly to the component body
|
|
227
|
+
const functionMatch = content.match(/(export\s+default\s+function\s+\w+\s*\([^)]*\)\s*\{)\s*/);
|
|
228
|
+
|
|
181
229
|
if (functionMatch) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
230
|
+
// Add hook at the start of the component
|
|
231
|
+
const withHook = content.replace(
|
|
232
|
+
functionMatch[0],
|
|
233
|
+
`${functionMatch[1]}
|
|
234
|
+
useKeak({ debug: true });
|
|
235
|
+
`
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
// Add KeakToolbar before the closing JSX tag
|
|
239
|
+
const returnMatch = withHook.match(/(return\s+\([\s\S]*?)(\s*<\/\w+>\s*\)\s*;?\s*})/);
|
|
240
|
+
if (returnMatch) {
|
|
241
|
+
return withHook.replace(
|
|
242
|
+
returnMatch[0],
|
|
243
|
+
`${returnMatch[1]}
|
|
244
|
+
<KeakToolbar position="bottom-right" />${returnMatch[2]}`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return withHook;
|
|
186
249
|
}
|
|
187
|
-
|
|
250
|
+
|
|
188
251
|
return content;
|
|
189
252
|
}
|
|
190
253
|
|
|
191
|
-
|
|
192
|
-
//
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
254
|
+
addHookToGenericComponent(content) {
|
|
255
|
+
// For generic components, try to add the hook to the component body
|
|
256
|
+
const functionMatch = content.match(/(function\s+\w+\s*\([^)]*\)\s*\{)\s*/);
|
|
257
|
+
|
|
258
|
+
if (functionMatch) {
|
|
259
|
+
// Add hook at the start of the component
|
|
260
|
+
const withHook = content.replace(
|
|
261
|
+
functionMatch[0],
|
|
262
|
+
`${functionMatch[1]}
|
|
263
|
+
useKeak({ debug: true });
|
|
264
|
+
`
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
// Add KeakToolbar before the closing JSX tag
|
|
268
|
+
const returnMatch = withHook.match(/(return\s+\([\s\S]*?)(\s*<\/\w+>\s*\)\s*;?)/);
|
|
269
|
+
if (returnMatch) {
|
|
270
|
+
return withHook.replace(
|
|
271
|
+
returnMatch[0],
|
|
272
|
+
`${returnMatch[1]}
|
|
273
|
+
<KeakToolbar position="bottom-right" />${returnMatch[2]}`
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return withHook;
|
|
200
278
|
}
|
|
201
|
-
|
|
279
|
+
|
|
202
280
|
return content;
|
|
203
281
|
}
|
|
204
282
|
}
|