@l4yercak3/cli 1.2.21 → 1.3.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.
@@ -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
+ };