@l4yercak3/cli 1.2.21 → 1.3.0
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/bin/cli.js +25 -0
- package/docs/CLI_PAGE_DETECTION_REQUIREMENTS.md +519 -0
- package/package.json +1 -1
- package/src/api/backend-client.js +149 -0
- package/src/commands/connect.js +243 -0
- package/src/commands/pages.js +317 -0
- package/src/commands/scaffold.js +409 -0
- package/src/commands/spread.js +59 -181
- package/src/commands/sync.js +169 -0
- package/src/detectors/index.js +13 -0
- package/src/detectors/mapping-suggestor.js +119 -0
- package/src/detectors/model-detector.js +318 -0
- package/src/detectors/page-detector.js +480 -0
- package/src/generators/manifest-generator.js +154 -0
- package/src/utils/init-helpers.js +243 -0
- package/tests/page-detector.test.js +371 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scaffold Command
|
|
3
|
+
* Generates integration files based on the project manifest (.l4yercak3.json)
|
|
4
|
+
* Diff-aware: only creates files that don't already exist
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const path = require('path');
|
|
9
|
+
const configManager = require('../config/config-manager');
|
|
10
|
+
const manifestGenerator = require('../generators/manifest-generator');
|
|
11
|
+
const { ensureDir, checkFileOverwrite, GENERATED_HEADER } = require('../utils/file-utils');
|
|
12
|
+
const { requireAuth } = require('../utils/init-helpers');
|
|
13
|
+
const chalk = require('chalk');
|
|
14
|
+
|
|
15
|
+
async function handleScaffold() {
|
|
16
|
+
requireAuth(configManager);
|
|
17
|
+
|
|
18
|
+
console.log(chalk.cyan(' 📦 Scaffolding integration files...\n'));
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const projectPath = process.cwd();
|
|
22
|
+
|
|
23
|
+
// Step 1: Load manifest
|
|
24
|
+
const manifest = manifestGenerator.loadManifest(projectPath);
|
|
25
|
+
if (!manifest) {
|
|
26
|
+
console.log(chalk.yellow(' ⚠️ No .l4yercak3.json manifest found.'));
|
|
27
|
+
console.log(chalk.gray(' Run "l4yercak3 init" first to scan your project.\n'));
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const framework = manifest.framework || 'unknown';
|
|
32
|
+
const routerType = manifest.routerType || 'pages';
|
|
33
|
+
const isTypeScript = manifest.typescript || false;
|
|
34
|
+
const ext = isTypeScript ? 'ts' : 'js';
|
|
35
|
+
|
|
36
|
+
console.log(chalk.green(` ✅ Loaded manifest: ${framework} (${routerType} router, ${isTypeScript ? 'TypeScript' : 'JavaScript'})`));
|
|
37
|
+
console.log(chalk.gray(` ${manifest.detectedModels?.length || 0} models, ${manifest.detectedRoutes?.length || 0} routes\n`));
|
|
38
|
+
|
|
39
|
+
// Step 2: Determine which files to generate
|
|
40
|
+
const filesToGenerate = [];
|
|
41
|
+
|
|
42
|
+
// a) lib/l4yercak3 client wrapper
|
|
43
|
+
const clientDir = path.join(projectPath, 'lib');
|
|
44
|
+
const clientFile = path.join(clientDir, `l4yercak3.${ext}`);
|
|
45
|
+
filesToGenerate.push({
|
|
46
|
+
path: clientFile,
|
|
47
|
+
name: `lib/l4yercak3.${ext}`,
|
|
48
|
+
content: generateClientWrapper(manifest, isTypeScript),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// b) Webhook handler (framework-specific path)
|
|
52
|
+
let webhookFile;
|
|
53
|
+
if (framework === 'nextjs' && routerType === 'app') {
|
|
54
|
+
webhookFile = path.join(projectPath, 'app', 'api', 'webhooks', 'l4yercak3', `route.${ext}`);
|
|
55
|
+
} else if (framework === 'nextjs') {
|
|
56
|
+
webhookFile = path.join(projectPath, 'pages', 'api', 'webhooks', `l4yercak3.${ext}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (webhookFile) {
|
|
60
|
+
const relPath = path.relative(projectPath, webhookFile);
|
|
61
|
+
filesToGenerate.push({
|
|
62
|
+
path: webhookFile,
|
|
63
|
+
name: relPath,
|
|
64
|
+
content: generateWebhookHandler(manifest, isTypeScript, routerType),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// c) Types file (if TypeScript and has models/mappings)
|
|
69
|
+
if (isTypeScript && manifest.detectedModels?.length > 0) {
|
|
70
|
+
const typesDir = path.join(projectPath, 'types');
|
|
71
|
+
const typesFile = path.join(typesDir, 'l4yercak3.ts');
|
|
72
|
+
filesToGenerate.push({
|
|
73
|
+
path: typesFile,
|
|
74
|
+
name: 'types/l4yercak3.ts',
|
|
75
|
+
content: generateTypes(manifest),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Step 3: Diff-aware generation
|
|
80
|
+
console.log(chalk.cyan(' 🔍 Checking existing files...\n'));
|
|
81
|
+
const created = [];
|
|
82
|
+
const skipped = [];
|
|
83
|
+
|
|
84
|
+
for (const file of filesToGenerate) {
|
|
85
|
+
const action = await checkFileOverwrite(file.path, { defaultAction: 'skip' });
|
|
86
|
+
|
|
87
|
+
if (action === 'skip') {
|
|
88
|
+
skipped.push(file.name);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Ensure directory exists
|
|
93
|
+
ensureDir(path.dirname(file.path));
|
|
94
|
+
|
|
95
|
+
// Write file
|
|
96
|
+
fs.writeFileSync(file.path, file.content, 'utf8');
|
|
97
|
+
created.push(file.name);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Step 4: Display results
|
|
101
|
+
if (created.length > 0) {
|
|
102
|
+
console.log(chalk.green(' ✅ Files created:\n'));
|
|
103
|
+
for (const name of created) {
|
|
104
|
+
console.log(chalk.gray(` • ${name}`));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (skipped.length > 0) {
|
|
109
|
+
console.log(chalk.gray(`\n ⏭️ Skipped (already exist):\n`));
|
|
110
|
+
for (const name of skipped) {
|
|
111
|
+
console.log(chalk.gray(` • ${name}`));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (created.length === 0 && skipped.length > 0) {
|
|
116
|
+
console.log(chalk.green('\n ✅ All integration files already exist. Nothing to generate.\n'));
|
|
117
|
+
} else {
|
|
118
|
+
console.log(chalk.cyan('\n 🎉 Scaffold complete!\n'));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
console.log(chalk.gray(' Next steps:'));
|
|
122
|
+
console.log(chalk.gray(' • Import the L4YERCAK3 client: import { l4yercak3 } from "@/lib/l4yercak3"'));
|
|
123
|
+
console.log(chalk.gray(' • Run "l4yercak3 sync" after changing your project structure\n'));
|
|
124
|
+
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error(chalk.red(`\n ❌ Error: ${error.message}\n`));
|
|
127
|
+
if (process.env.L4YERCAK3_DEBUG && error.stack) {
|
|
128
|
+
console.error(chalk.gray(error.stack));
|
|
129
|
+
}
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Generate the L4YERCAK3 client wrapper
|
|
136
|
+
*/
|
|
137
|
+
function generateClientWrapper(manifest, isTypeScript) {
|
|
138
|
+
const mappings = manifest.suggestedMappings || [];
|
|
139
|
+
const hasContact = mappings.some(m => m.platformType === 'contact');
|
|
140
|
+
const hasBooking = mappings.some(m => m.platformType === 'booking');
|
|
141
|
+
const hasEvent = mappings.some(m => m.platformType === 'event');
|
|
142
|
+
const hasProduct = mappings.some(m => m.platformType === 'product');
|
|
143
|
+
|
|
144
|
+
const typeAnnotation = isTypeScript ? ': string' : '';
|
|
145
|
+
const typeImport = isTypeScript
|
|
146
|
+
? `import type { L4yercak3Config } from '@/types/l4yercak3';\n\n`
|
|
147
|
+
: '';
|
|
148
|
+
|
|
149
|
+
return `/**
|
|
150
|
+
* L4YERCAK3 API Client
|
|
151
|
+
* ${GENERATED_HEADER}
|
|
152
|
+
*
|
|
153
|
+
* Usage: import { l4yercak3 } from '@/lib/l4yercak3';
|
|
154
|
+
*/
|
|
155
|
+
|
|
156
|
+
${typeImport}const API_KEY${typeAnnotation} = process.env.L4YERCAK3_API_KEY || '';
|
|
157
|
+
const BASE_URL${typeAnnotation} = process.env.L4YERCAK3_BACKEND_URL || 'https://agreeable-lion-828.convex.site';
|
|
158
|
+
const ORG_ID${typeAnnotation} = process.env.L4YERCAK3_ORG_ID || '';
|
|
159
|
+
const APP_ID${typeAnnotation} = process.env.L4YERCAK3_APP_ID || '';
|
|
160
|
+
|
|
161
|
+
class L4yercak3Client {
|
|
162
|
+
constructor() {
|
|
163
|
+
this.apiKey = API_KEY;
|
|
164
|
+
this.baseUrl = BASE_URL;
|
|
165
|
+
this.orgId = ORG_ID;
|
|
166
|
+
this.appId = APP_ID;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async request(method${typeAnnotation ? ': string' : ''}, endpoint${typeAnnotation ? ': string' : ''}, data${isTypeScript ? '?: Record<string, unknown>' : ''}) {
|
|
170
|
+
const url = \`\${this.baseUrl}\${endpoint}\`;
|
|
171
|
+
const headers${isTypeScript ? ': Record<string, string>' : ''} = {
|
|
172
|
+
'Content-Type': 'application/json',
|
|
173
|
+
'Authorization': \`Bearer \${this.apiKey}\`,
|
|
174
|
+
'X-Organization-Id': this.orgId,
|
|
175
|
+
'X-Application-Id': this.appId,
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const options${isTypeScript ? ': RequestInit' : ''} = { method, headers };
|
|
179
|
+
if (data && (method === 'POST' || method === 'PUT' || method === 'PATCH')) {
|
|
180
|
+
options.body = JSON.stringify(data);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const response = await fetch(url, options);
|
|
184
|
+
if (!response.ok) {
|
|
185
|
+
const error = await response.json().catch(() => ({}));
|
|
186
|
+
throw new Error(error.message || \`API error: \${response.status}\`);
|
|
187
|
+
}
|
|
188
|
+
return response.json();
|
|
189
|
+
}
|
|
190
|
+
${hasContact ? `
|
|
191
|
+
// Contact methods
|
|
192
|
+
async getContacts() {
|
|
193
|
+
return this.request('GET', '/api/v1/contacts');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async createContact(data${isTypeScript ? ': Record<string, unknown>' : ''}) {
|
|
197
|
+
return this.request('POST', '/api/v1/contacts', data);
|
|
198
|
+
}
|
|
199
|
+
` : ''}${hasBooking ? `
|
|
200
|
+
// Booking methods
|
|
201
|
+
async getBookings() {
|
|
202
|
+
return this.request('GET', '/api/v1/bookings');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async createBooking(data${isTypeScript ? ': Record<string, unknown>' : ''}) {
|
|
206
|
+
return this.request('POST', '/api/v1/bookings', data);
|
|
207
|
+
}
|
|
208
|
+
` : ''}${hasEvent ? `
|
|
209
|
+
// Event methods
|
|
210
|
+
async getEvents() {
|
|
211
|
+
return this.request('GET', '/api/v1/events');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async createEvent(data${isTypeScript ? ': Record<string, unknown>' : ''}) {
|
|
215
|
+
return this.request('POST', '/api/v1/events', data);
|
|
216
|
+
}
|
|
217
|
+
` : ''}${hasProduct ? `
|
|
218
|
+
// Product methods
|
|
219
|
+
async getProducts() {
|
|
220
|
+
return this.request('GET', '/api/v1/products');
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async createProduct(data${isTypeScript ? ': Record<string, unknown>' : ''}) {
|
|
224
|
+
return this.request('POST', '/api/v1/products', data);
|
|
225
|
+
}
|
|
226
|
+
` : ''}
|
|
227
|
+
// Generic resource methods
|
|
228
|
+
async get(resource${typeAnnotation ? ': string' : ''}) {
|
|
229
|
+
return this.request('GET', \`/api/v1/\${resource}\`);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async create(resource${typeAnnotation ? ': string' : ''}, data${isTypeScript ? ': Record<string, unknown>' : ''}) {
|
|
233
|
+
return this.request('POST', \`/api/v1/\${resource}\`, data);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async update(resource${typeAnnotation ? ': string' : ''}, id${typeAnnotation ? ': string' : ''}, data${isTypeScript ? ': Record<string, unknown>' : ''}) {
|
|
237
|
+
return this.request('PATCH', \`/api/v1/\${resource}/\${id}\`, data);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async remove(resource${typeAnnotation ? ': string' : ''}, id${typeAnnotation ? ': string' : ''}) {
|
|
241
|
+
return this.request('DELETE', \`/api/v1/\${resource}/\${id}\`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export const l4yercak3 = new L4yercak3Client();
|
|
246
|
+
export default l4yercak3;
|
|
247
|
+
`;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Generate webhook handler
|
|
252
|
+
*/
|
|
253
|
+
function generateWebhookHandler(manifest, isTypeScript, routerType) {
|
|
254
|
+
if (routerType === 'app') {
|
|
255
|
+
return `/**
|
|
256
|
+
* L4YERCAK3 Webhook Handler (App Router)
|
|
257
|
+
* ${GENERATED_HEADER}
|
|
258
|
+
*
|
|
259
|
+
* Receives webhook events from the L4YERCAK3 platform.
|
|
260
|
+
* Configure your webhook URL in the L4YERCAK3 dashboard.
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
import { NextResponse } from 'next/server';
|
|
264
|
+
|
|
265
|
+
export async function POST(request${isTypeScript ? ': Request' : ''}) {
|
|
266
|
+
try {
|
|
267
|
+
const body = await request.json();
|
|
268
|
+
const eventType = body.type;
|
|
269
|
+
|
|
270
|
+
// Verify webhook signature (recommended for production)
|
|
271
|
+
// const signature = request.headers.get('x-l4yercak3-signature');
|
|
272
|
+
|
|
273
|
+
switch (eventType) {
|
|
274
|
+
case 'contact.created':
|
|
275
|
+
case 'contact.updated':
|
|
276
|
+
// Handle contact events
|
|
277
|
+
console.log(\`[L4YERCAK3] \${eventType}:\`, body.data);
|
|
278
|
+
break;
|
|
279
|
+
|
|
280
|
+
case 'booking.created':
|
|
281
|
+
case 'booking.updated':
|
|
282
|
+
// Handle booking events
|
|
283
|
+
console.log(\`[L4YERCAK3] \${eventType}:\`, body.data);
|
|
284
|
+
break;
|
|
285
|
+
|
|
286
|
+
default:
|
|
287
|
+
console.log(\`[L4YERCAK3] Unhandled event: \${eventType}\`);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return NextResponse.json({ received: true });
|
|
291
|
+
} catch (error) {
|
|
292
|
+
console.error('[L4YERCAK3] Webhook error:', error);
|
|
293
|
+
return NextResponse.json({ error: 'Webhook processing failed' }, { status: 500 });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
`;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Pages Router
|
|
300
|
+
return `/**
|
|
301
|
+
* L4YERCAK3 Webhook Handler (Pages Router)
|
|
302
|
+
* ${GENERATED_HEADER}
|
|
303
|
+
*
|
|
304
|
+
* Receives webhook events from the L4YERCAK3 platform.
|
|
305
|
+
* Configure your webhook URL in the L4YERCAK3 dashboard.
|
|
306
|
+
*/
|
|
307
|
+
|
|
308
|
+
export default async function handler(req, res) {
|
|
309
|
+
if (req.method !== 'POST') {
|
|
310
|
+
return res.status(405).json({ error: 'Method not allowed' });
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
try {
|
|
314
|
+
const body = req.body;
|
|
315
|
+
const eventType = body.type;
|
|
316
|
+
|
|
317
|
+
// Verify webhook signature (recommended for production)
|
|
318
|
+
// const signature = req.headers['x-l4yercak3-signature'];
|
|
319
|
+
|
|
320
|
+
switch (eventType) {
|
|
321
|
+
case 'contact.created':
|
|
322
|
+
case 'contact.updated':
|
|
323
|
+
// Handle contact events
|
|
324
|
+
console.log(\`[L4YERCAK3] \${eventType}:\`, body.data);
|
|
325
|
+
break;
|
|
326
|
+
|
|
327
|
+
case 'booking.created':
|
|
328
|
+
case 'booking.updated':
|
|
329
|
+
// Handle booking events
|
|
330
|
+
console.log(\`[L4YERCAK3] \${eventType}:\`, body.data);
|
|
331
|
+
break;
|
|
332
|
+
|
|
333
|
+
default:
|
|
334
|
+
console.log(\`[L4YERCAK3] Unhandled event: \${eventType}\`);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return res.status(200).json({ received: true });
|
|
338
|
+
} catch (error) {
|
|
339
|
+
console.error('[L4YERCAK3] Webhook error:', error);
|
|
340
|
+
return res.status(500).json({ error: 'Webhook processing failed' });
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
`;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Generate TypeScript types based on manifest models and mappings
|
|
348
|
+
*/
|
|
349
|
+
function generateTypes(manifest) {
|
|
350
|
+
const models = manifest.detectedModels || [];
|
|
351
|
+
const mappings = manifest.suggestedMappings || [];
|
|
352
|
+
|
|
353
|
+
let content = `/**
|
|
354
|
+
* L4YERCAK3 Types
|
|
355
|
+
* ${GENERATED_HEADER}
|
|
356
|
+
*
|
|
357
|
+
* Types generated from your project's detected models and platform mappings.
|
|
358
|
+
*/
|
|
359
|
+
|
|
360
|
+
export interface L4yercak3Config {
|
|
361
|
+
apiKey: string;
|
|
362
|
+
baseUrl: string;
|
|
363
|
+
orgId: string;
|
|
364
|
+
appId: string;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export interface WebhookEvent {
|
|
368
|
+
type: string;
|
|
369
|
+
data: Record<string, unknown>;
|
|
370
|
+
timestamp: number;
|
|
371
|
+
applicationId: string;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
`;
|
|
375
|
+
|
|
376
|
+
// Generate interfaces for each mapping
|
|
377
|
+
for (const mapping of mappings) {
|
|
378
|
+
const model = models.find(m => m.name === mapping.localModel);
|
|
379
|
+
if (model) {
|
|
380
|
+
content += `/**
|
|
381
|
+
* Platform mapping: ${model.name} → ${mapping.platformType} (${mapping.confidence}% confidence)
|
|
382
|
+
* Source: ${model.source}
|
|
383
|
+
*/
|
|
384
|
+
export interface ${model.name}Mapping {
|
|
385
|
+
`;
|
|
386
|
+
for (const field of model.fields) {
|
|
387
|
+
content += ` ${field}: unknown;\n`;
|
|
388
|
+
}
|
|
389
|
+
content += ` // Platform fields
|
|
390
|
+
_platformType: '${mapping.platformType}';
|
|
391
|
+
_platformId?: string;
|
|
392
|
+
}\n\n`;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Generate union type of all platform types used
|
|
397
|
+
const platformTypes = [...new Set(mappings.map(m => m.platformType))];
|
|
398
|
+
if (platformTypes.length > 0) {
|
|
399
|
+
content += `export type PlatformType = ${platformTypes.map(t => `'${t}'`).join(' | ')};\n`;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return content;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
module.exports = {
|
|
406
|
+
command: 'scaffold',
|
|
407
|
+
description: 'Generate integration files based on your project manifest',
|
|
408
|
+
handler: handleScaffold,
|
|
409
|
+
};
|
package/src/commands/spread.js
CHANGED
|
@@ -5,198 +5,27 @@
|
|
|
5
5
|
|
|
6
6
|
const fs = require('fs');
|
|
7
7
|
const path = require('path');
|
|
8
|
-
const { execSync } = require('child_process');
|
|
9
8
|
const configManager = require('../config/config-manager');
|
|
10
9
|
const backendClient = require('../api/backend-client');
|
|
11
10
|
const projectDetector = require('../detectors');
|
|
12
11
|
const fileGenerator = require('../generators');
|
|
12
|
+
const manifestGenerator = require('../generators/manifest-generator');
|
|
13
|
+
const pageDetector = require('../detectors/page-detector');
|
|
14
|
+
const { suggestMappings } = require('../detectors/mapping-suggestor');
|
|
13
15
|
const { generateProjectPathHash } = require('../utils/file-utils');
|
|
16
|
+
const {
|
|
17
|
+
createOrganization,
|
|
18
|
+
generateNewApiKey,
|
|
19
|
+
checkGitStatusBeforeGeneration,
|
|
20
|
+
requireAuth,
|
|
21
|
+
} = require('../utils/init-helpers');
|
|
14
22
|
const inquirer = require('inquirer');
|
|
15
23
|
const chalk = require('chalk');
|
|
16
24
|
const pkg = require('../../package.json');
|
|
17
25
|
const { showMainMenu, executeMenuAction } = require('../utils/prompt-utils');
|
|
18
26
|
|
|
19
|
-
/**
|
|
20
|
-
* Helper function to create an organization
|
|
21
|
-
*/
|
|
22
|
-
async function createOrganization(orgName) {
|
|
23
|
-
console.log(chalk.gray(` Creating organization "${orgName}"...`));
|
|
24
|
-
const newOrg = await backendClient.createOrganization(orgName);
|
|
25
|
-
// Handle different response formats
|
|
26
|
-
const organizationId = newOrg.organizationId || newOrg.id || newOrg.data?.organizationId || newOrg.data?.id;
|
|
27
|
-
const organizationName = newOrg.name || orgName;
|
|
28
|
-
|
|
29
|
-
if (!organizationId) {
|
|
30
|
-
throw new Error('Organization ID not found in response. Please check backend API endpoint.');
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
console.log(chalk.green(` ✅ Organization created: ${organizationName}\n`));
|
|
34
|
-
return { organizationId, organizationName };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Helper function to generate a new API key
|
|
39
|
-
*/
|
|
40
|
-
async function generateNewApiKey(organizationId) {
|
|
41
|
-
console.log(chalk.gray(' Generating API key...'));
|
|
42
|
-
const apiKeyResponse = await backendClient.generateApiKey(
|
|
43
|
-
organizationId,
|
|
44
|
-
'CLI Generated Key',
|
|
45
|
-
['*']
|
|
46
|
-
);
|
|
47
|
-
// Handle different response formats
|
|
48
|
-
const apiKey = apiKeyResponse.key || apiKeyResponse.apiKey || apiKeyResponse.data?.key || apiKeyResponse.data?.apiKey;
|
|
49
|
-
|
|
50
|
-
if (!apiKey) {
|
|
51
|
-
throw new Error('API key not found in response. Please check backend API endpoint.');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
console.log(chalk.green(` ✅ API key generated\n`));
|
|
55
|
-
return apiKey;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Check if the project is a git repository
|
|
60
|
-
*/
|
|
61
|
-
function isGitRepo(projectPath) {
|
|
62
|
-
try {
|
|
63
|
-
execSync('git rev-parse --is-inside-work-tree', {
|
|
64
|
-
cwd: projectPath,
|
|
65
|
-
stdio: 'pipe',
|
|
66
|
-
});
|
|
67
|
-
return true;
|
|
68
|
-
} catch {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Get git status (uncommitted changes)
|
|
75
|
-
*/
|
|
76
|
-
function getGitStatus(projectPath) {
|
|
77
|
-
try {
|
|
78
|
-
const status = execSync('git status --porcelain', {
|
|
79
|
-
cwd: projectPath,
|
|
80
|
-
encoding: 'utf8',
|
|
81
|
-
});
|
|
82
|
-
return status.trim();
|
|
83
|
-
} catch {
|
|
84
|
-
return '';
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Check for uncommitted changes and prompt user to commit first
|
|
90
|
-
* Returns true if we should proceed, false if user wants to abort
|
|
91
|
-
*/
|
|
92
|
-
async function checkGitStatusBeforeGeneration(projectPath) {
|
|
93
|
-
const debug = process.env.L4YERCAK3_DEBUG;
|
|
94
|
-
|
|
95
|
-
if (debug) {
|
|
96
|
-
console.log('\n[DEBUG] Git status check:');
|
|
97
|
-
console.log(` projectPath: "${projectPath}"`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Skip if not a git repo
|
|
101
|
-
if (!isGitRepo(projectPath)) {
|
|
102
|
-
if (debug) {
|
|
103
|
-
console.log(' → Not a git repo, skipping check');
|
|
104
|
-
}
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const status = getGitStatus(projectPath);
|
|
109
|
-
|
|
110
|
-
if (debug) {
|
|
111
|
-
console.log(` → Git status: "${status.substring(0, 100)}${status.length > 100 ? '...' : ''}"`);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// No uncommitted changes - proceed
|
|
115
|
-
if (!status) {
|
|
116
|
-
if (debug) {
|
|
117
|
-
console.log(' → No uncommitted changes, proceeding');
|
|
118
|
-
}
|
|
119
|
-
return true;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Count changes
|
|
123
|
-
const changes = status.split('\n').filter(line => line.trim());
|
|
124
|
-
const modifiedCount = changes.filter(line => line.startsWith(' M') || line.startsWith('M ')).length;
|
|
125
|
-
const untrackedCount = changes.filter(line => line.startsWith('??')).length;
|
|
126
|
-
const stagedCount = changes.filter(line => /^[MADRC]/.test(line)).length;
|
|
127
|
-
|
|
128
|
-
console.log(chalk.yellow(' ⚠️ Uncommitted changes detected\n'));
|
|
129
|
-
|
|
130
|
-
if (modifiedCount > 0) {
|
|
131
|
-
console.log(chalk.gray(` ${modifiedCount} modified file(s)`));
|
|
132
|
-
}
|
|
133
|
-
if (untrackedCount > 0) {
|
|
134
|
-
console.log(chalk.gray(` ${untrackedCount} untracked file(s)`));
|
|
135
|
-
}
|
|
136
|
-
if (stagedCount > 0) {
|
|
137
|
-
console.log(chalk.gray(` ${stagedCount} staged file(s)`));
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
console.log('');
|
|
141
|
-
console.log(chalk.gray(' We recommend committing your changes before generating'));
|
|
142
|
-
console.log(chalk.gray(' new files, so you can easily revert if needed.\n'));
|
|
143
|
-
|
|
144
|
-
const { action } = await inquirer.prompt([
|
|
145
|
-
{
|
|
146
|
-
type: 'list',
|
|
147
|
-
name: 'action',
|
|
148
|
-
message: 'How would you like to proceed?',
|
|
149
|
-
choices: [
|
|
150
|
-
{
|
|
151
|
-
name: 'Continue anyway - I\'ll handle it later',
|
|
152
|
-
value: 'continue',
|
|
153
|
-
},
|
|
154
|
-
{
|
|
155
|
-
name: 'Commit changes now - Create a checkpoint commit',
|
|
156
|
-
value: 'commit',
|
|
157
|
-
},
|
|
158
|
-
{
|
|
159
|
-
name: 'Abort - I\'ll commit manually first',
|
|
160
|
-
value: 'abort',
|
|
161
|
-
},
|
|
162
|
-
],
|
|
163
|
-
},
|
|
164
|
-
]);
|
|
165
|
-
|
|
166
|
-
if (action === 'abort') {
|
|
167
|
-
console.log(chalk.gray('\n No worries! Run "l4yercak3 spread" again after committing.\n'));
|
|
168
|
-
return false;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (action === 'commit') {
|
|
172
|
-
try {
|
|
173
|
-
// Stage all changes
|
|
174
|
-
execSync('git add -A', { cwd: projectPath, stdio: 'pipe' });
|
|
175
|
-
|
|
176
|
-
// Create commit
|
|
177
|
-
const commitMessage = 'chore: checkpoint before L4YERCAK3 integration';
|
|
178
|
-
execSync(`git commit -m "${commitMessage}"`, { cwd: projectPath, stdio: 'pipe' });
|
|
179
|
-
|
|
180
|
-
console.log(chalk.green('\n ✅ Changes committed successfully'));
|
|
181
|
-
console.log(chalk.gray(` Message: "${commitMessage}"`));
|
|
182
|
-
console.log(chalk.gray(' You can revert with: git reset --soft HEAD~1\n'));
|
|
183
|
-
} catch (error) {
|
|
184
|
-
console.log(chalk.yellow('\n ⚠️ Could not create commit automatically'));
|
|
185
|
-
console.log(chalk.gray(` ${error.message}`));
|
|
186
|
-
console.log(chalk.gray(' Proceeding with file generation anyway...\n'));
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return true;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
27
|
async function handleSpread() {
|
|
194
|
-
|
|
195
|
-
if (!configManager.isLoggedIn()) {
|
|
196
|
-
console.log(chalk.yellow(' ⚠️ You must be logged in first'));
|
|
197
|
-
console.log(chalk.gray('\n Run "l4yercak3 login" to authenticate\n'));
|
|
198
|
-
process.exit(1);
|
|
199
|
-
}
|
|
28
|
+
requireAuth(configManager);
|
|
200
29
|
|
|
201
30
|
console.log(chalk.cyan(' 🍰 Setting up your Layer Cake integration...\n'));
|
|
202
31
|
|
|
@@ -293,6 +122,45 @@ async function handleSpread() {
|
|
|
293
122
|
}
|
|
294
123
|
}
|
|
295
124
|
|
|
125
|
+
// Display model detection
|
|
126
|
+
if (detection.models && detection.models.hasModels) {
|
|
127
|
+
console.log(chalk.green(` ✅ Detected ${detection.models.models.length} model(s)`));
|
|
128
|
+
for (const model of detection.models.models.slice(0, 5)) {
|
|
129
|
+
console.log(chalk.gray(` • ${model.name} (${model.source}) [${model.fields.length} fields]`));
|
|
130
|
+
}
|
|
131
|
+
if (detection.models.models.length > 5) {
|
|
132
|
+
console.log(chalk.gray(` ... and ${detection.models.models.length - 5} more`));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Scan routes with HTTP methods
|
|
137
|
+
const detectedRoutes = pageDetector.detect(
|
|
138
|
+
detection.projectPath,
|
|
139
|
+
detection.framework.type,
|
|
140
|
+
detection.framework.metadata || {}
|
|
141
|
+
);
|
|
142
|
+
const apiRoutes = detectedRoutes.filter(r => r.pageType === 'api_route');
|
|
143
|
+
if (apiRoutes.length > 0) {
|
|
144
|
+
console.log(chalk.green(` ✅ Detected ${apiRoutes.length} API route(s)`));
|
|
145
|
+
for (const route of apiRoutes.slice(0, 5)) {
|
|
146
|
+
const methods = route.methods ? route.methods.join(', ') : 'GET, POST';
|
|
147
|
+
console.log(chalk.gray(` • ${route.path} [${methods}]`));
|
|
148
|
+
}
|
|
149
|
+
if (apiRoutes.length > 5) {
|
|
150
|
+
console.log(chalk.gray(` ... and ${apiRoutes.length - 5} more`));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Compute suggested mappings
|
|
155
|
+
const models = detection.models ? detection.models.models : [];
|
|
156
|
+
const mappings = suggestMappings(models);
|
|
157
|
+
if (mappings.length > 0) {
|
|
158
|
+
console.log(chalk.green(` ✅ ${mappings.length} suggested mapping(s)`));
|
|
159
|
+
for (const mapping of mappings) {
|
|
160
|
+
console.log(chalk.gray(` • ${mapping.localModel} → ${mapping.platformType} (${mapping.confidence}% confidence)`));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
296
164
|
console.log('');
|
|
297
165
|
|
|
298
166
|
// Step 1.5: Project name
|
|
@@ -772,6 +640,16 @@ async function handleSpread() {
|
|
|
772
640
|
console.log(chalk.gray(` • ${path.relative(process.cwd(), generatedFiles.gitignore)} (updated)`));
|
|
773
641
|
}
|
|
774
642
|
|
|
643
|
+
// Generate .l4yercak3.json manifest
|
|
644
|
+
const manifestPath = manifestGenerator.generate({
|
|
645
|
+
projectPath: detection.projectPath,
|
|
646
|
+
detection,
|
|
647
|
+
models,
|
|
648
|
+
routes: detectedRoutes,
|
|
649
|
+
mappings,
|
|
650
|
+
});
|
|
651
|
+
console.log(chalk.gray(` • ${path.relative(process.cwd(), manifestPath)}`));
|
|
652
|
+
|
|
775
653
|
// Step 9: Register application with backend
|
|
776
654
|
console.log(chalk.cyan('\n 🔗 Registering with L4YERCAK3...\n'));
|
|
777
655
|
|