vigthoria-cli 1.0.2 → 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.
@@ -0,0 +1,609 @@
1
+ /**
2
+ * Vigthoria CLI - Deploy Commands
3
+ *
4
+ * Deploy and host projects on Vigthoria infrastructure
5
+ *
6
+ * Usage:
7
+ * vig deploy - Interactive deploy wizard
8
+ * vig deploy --subdomain <name> - Deploy to name.vigthoria.io
9
+ * vig deploy --domain <domain> - Deploy to custom domain
10
+ * vig deploy status - Show deployment status
11
+ * vig deploy list - List all deployments
12
+ * vig deploy remove <domain> - Remove a deployment
13
+ */
14
+
15
+ import chalk from 'chalk';
16
+ import * as fs from 'fs';
17
+ import * as path from 'path';
18
+ import { Config } from '../utils/config.js';
19
+ import { Logger } from '../utils/logger.js';
20
+ import ora from 'ora';
21
+ import inquirer from 'inquirer';
22
+
23
+ interface HostingPlan {
24
+ id: string;
25
+ name: string;
26
+ display_name: string;
27
+ price_monthly: number;
28
+ price_yearly: number;
29
+ max_projects: number;
30
+ storage_mb: number;
31
+ custom_domain_allowed: boolean;
32
+ subdomain_allowed: boolean;
33
+ }
34
+
35
+ interface DeployedDomain {
36
+ id: number;
37
+ domain_type: 'preview' | 'subdomain' | 'custom';
38
+ subdomain: string | null;
39
+ custom_domain: string | null;
40
+ project_name: string;
41
+ hosting_tier: string;
42
+ ssl_status: string;
43
+ is_active: boolean;
44
+ url: string;
45
+ created_at: string;
46
+ }
47
+
48
+ interface DeployOptions {
49
+ subdomain?: string;
50
+ domain?: string;
51
+ project?: string;
52
+ force?: boolean;
53
+ }
54
+
55
+ export class DeployCommand {
56
+ private config: Config;
57
+ private logger: Logger;
58
+ private apiBase: string;
59
+
60
+ constructor(config: Config, logger: Logger) {
61
+ this.config = config;
62
+ this.logger = logger;
63
+ this.apiBase = this.config.get('apiUrl') || 'https://coder.vigthoria.io';
64
+ }
65
+
66
+ private getAuthHeaders(): Record<string, string> {
67
+ const token = this.config.get('authToken');
68
+ return {
69
+ 'Authorization': `Bearer ${token}`,
70
+ 'Content-Type': 'application/json'
71
+ };
72
+ }
73
+
74
+ private isAuthenticated(): boolean {
75
+ return !!this.config.get('authToken');
76
+ }
77
+
78
+ private requireAuth(): void {
79
+ if (!this.isAuthenticated()) {
80
+ console.log(chalk.red('\nāŒ Authentication required'));
81
+ console.log(chalk.gray(' Run `vig login` to authenticate first.\n'));
82
+ process.exit(1);
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Interactive deploy wizard
88
+ */
89
+ async deploy(options: DeployOptions = {}): Promise<void> {
90
+ this.requireAuth();
91
+
92
+ console.log(chalk.cyan('\nšŸš€ Vigthoria Deploy - Host Your Project\n'));
93
+
94
+ // If subdomain or domain specified, deploy directly
95
+ if (options.subdomain) {
96
+ await this.deployToSubdomain(options.subdomain, options.project);
97
+ return;
98
+ }
99
+
100
+ if (options.domain) {
101
+ await this.deployToCustomDomain(options.domain, options.project);
102
+ return;
103
+ }
104
+
105
+ // Interactive wizard
106
+ const { deployType } = await inquirer.prompt([{
107
+ type: 'list',
108
+ name: 'deployType',
109
+ message: 'How would you like to deploy?',
110
+ choices: [
111
+ { name: 'šŸ†“ Preview URL (Free) - coder.vigthoria.io/preview/...', value: 'preview' },
112
+ { name: '🌐 Vigthoria Subdomain (€4.99/mo) - yourapp.vigthoria.io', value: 'subdomain' },
113
+ { name: 'šŸ”— Custom Domain (€9.99/mo) - yourdomain.com', value: 'custom' },
114
+ { name: 'šŸ“Š View Hosting Plans', value: 'plans' }
115
+ ]
116
+ }]);
117
+
118
+ switch (deployType) {
119
+ case 'preview':
120
+ await this.deployToPreview(options.project);
121
+ break;
122
+ case 'subdomain':
123
+ await this.promptSubdomainDeploy(options.project);
124
+ break;
125
+ case 'custom':
126
+ await this.promptCustomDomainDeploy(options.project);
127
+ break;
128
+ case 'plans':
129
+ await this.showPlans();
130
+ break;
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Deploy to preview URL (free)
136
+ */
137
+ async deployToPreview(projectPath?: string): Promise<void> {
138
+ const spinner = ora('Deploying to preview...').start();
139
+
140
+ try {
141
+ const projectDir = projectPath || process.cwd();
142
+ const projectInfo = this.detectProjectInfo(projectDir);
143
+
144
+ const response = await fetch(`${this.apiBase}/api/hosting/deploy/preview`, {
145
+ method: 'POST',
146
+ headers: this.getAuthHeaders(),
147
+ body: JSON.stringify({
148
+ projectName: projectInfo.name,
149
+ projectPath: projectDir
150
+ })
151
+ });
152
+
153
+ if (!response.ok) {
154
+ const error = await response.json() as { error?: string };
155
+ throw new Error(error.error || 'Failed to deploy');
156
+ }
157
+
158
+ const data = await response.json() as { success: boolean; url: string; message: string };
159
+
160
+ spinner.succeed(chalk.green('Deployed to preview!'));
161
+
162
+ console.log(chalk.cyan('\nšŸ”— Preview URL:'));
163
+ console.log(chalk.white(` ${data.url}`));
164
+ console.log(chalk.gray('\n Note: Preview URLs may expire after 7 days of inactivity.'));
165
+ console.log(chalk.gray(' Upgrade to a subdomain for permanent hosting.\n'));
166
+
167
+ } catch (error) {
168
+ spinner.fail('Deploy failed');
169
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
170
+ console.log(chalk.red(`\nāŒ Error: ${errMsg}\n`));
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Deploy to Vigthoria subdomain
176
+ */
177
+ async deployToSubdomain(subdomain: string, projectPath?: string): Promise<void> {
178
+ const spinner = ora(`Deploying to ${subdomain}.vigthoria.io...`).start();
179
+
180
+ try {
181
+ // Validate subdomain format
182
+ if (!/^[a-z0-9][a-z0-9-]*[a-z0-9]$/.test(subdomain) || subdomain.length < 3) {
183
+ throw new Error('Subdomain must be 3+ chars, lowercase alphanumeric with hyphens');
184
+ }
185
+
186
+ const projectDir = projectPath || process.cwd();
187
+ const projectInfo = this.detectProjectInfo(projectDir);
188
+
189
+ const response = await fetch(`${this.apiBase}/api/hosting/deploy/subdomain`, {
190
+ method: 'POST',
191
+ headers: this.getAuthHeaders(),
192
+ body: JSON.stringify({
193
+ subdomain,
194
+ projectName: projectInfo.name,
195
+ projectPath: projectDir
196
+ })
197
+ });
198
+
199
+ const data = await response.json() as {
200
+ success: boolean;
201
+ url?: string;
202
+ error?: string;
203
+ requiresSubscription?: boolean;
204
+ checkoutUrl?: string;
205
+ };
206
+
207
+ if (!response.ok || !data.success) {
208
+ if (data.requiresSubscription) {
209
+ spinner.stop();
210
+ console.log(chalk.yellow('\nāš ļø Subdomain hosting requires a subscription (€4.99/mo)'));
211
+
212
+ const { proceed } = await inquirer.prompt([{
213
+ type: 'confirm',
214
+ name: 'proceed',
215
+ message: 'Would you like to subscribe now?',
216
+ default: true
217
+ }]);
218
+
219
+ if (proceed && data.checkoutUrl) {
220
+ console.log(chalk.cyan(`\nšŸ”— Opening checkout: ${data.checkoutUrl}`));
221
+ console.log(chalk.gray('Please open this URL in your browser to subscribe.\n'));
222
+ }
223
+ return;
224
+ }
225
+ throw new Error(data.error || 'Failed to deploy');
226
+ }
227
+
228
+ spinner.succeed(chalk.green(`Deployed to ${subdomain}.vigthoria.io!`));
229
+
230
+ console.log(chalk.cyan('\n🌐 Your Site is Live:'));
231
+ console.log(chalk.bold.white(` https://${subdomain}.vigthoria.io`));
232
+ console.log(chalk.gray('\n āœ“ SSL certificate auto-configured'));
233
+ console.log(chalk.gray(' āœ“ Global CDN enabled'));
234
+ console.log(chalk.gray(' āœ“ Unlimited traffic included\n'));
235
+
236
+ } catch (error) {
237
+ spinner.fail('Deploy failed');
238
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
239
+ console.log(chalk.red(`\nāŒ Error: ${errMsg}\n`));
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Deploy to custom domain
245
+ */
246
+ async deployToCustomDomain(domain: string, projectPath?: string): Promise<void> {
247
+ const spinner = ora(`Setting up ${domain}...`).start();
248
+
249
+ try {
250
+ const projectDir = projectPath || process.cwd();
251
+ const projectInfo = this.detectProjectInfo(projectDir);
252
+
253
+ const response = await fetch(`${this.apiBase}/api/hosting/deploy/custom`, {
254
+ method: 'POST',
255
+ headers: this.getAuthHeaders(),
256
+ body: JSON.stringify({
257
+ domain,
258
+ projectName: projectInfo.name,
259
+ projectPath: projectDir
260
+ })
261
+ });
262
+
263
+ const data = await response.json() as {
264
+ success: boolean;
265
+ url?: string;
266
+ error?: string;
267
+ requiresSubscription?: boolean;
268
+ checkoutUrl?: string;
269
+ dnsRecords?: { type: string; name: string; value: string }[];
270
+ verificationCode?: string;
271
+ };
272
+
273
+ if (!response.ok || !data.success) {
274
+ if (data.requiresSubscription) {
275
+ spinner.stop();
276
+ console.log(chalk.yellow('\nāš ļø Custom domain hosting requires a subscription (€9.99/mo)'));
277
+
278
+ const { proceed } = await inquirer.prompt([{
279
+ type: 'confirm',
280
+ name: 'proceed',
281
+ message: 'Would you like to subscribe now?',
282
+ default: true
283
+ }]);
284
+
285
+ if (proceed && data.checkoutUrl) {
286
+ console.log(chalk.cyan(`\nšŸ”— Checkout URL: ${data.checkoutUrl}`));
287
+ console.log(chalk.gray('Please open this URL in your browser to subscribe.\n'));
288
+ }
289
+ return;
290
+ }
291
+ throw new Error(data.error || 'Failed to deploy');
292
+ }
293
+
294
+ spinner.succeed(chalk.green('Domain registered!'));
295
+
296
+ if (data.dnsRecords) {
297
+ console.log(chalk.cyan('\nšŸ“ Configure your DNS records:'));
298
+ console.log(chalk.gray('─'.repeat(60)));
299
+
300
+ for (const record of data.dnsRecords) {
301
+ console.log(chalk.white(` Type: ${record.type.padEnd(6)} Name: ${record.name.padEnd(20)} Value: ${record.value}`));
302
+ }
303
+
304
+ console.log(chalk.gray('─'.repeat(60)));
305
+ console.log(chalk.yellow('\nā³ After adding DNS records, run:'));
306
+ console.log(chalk.white(` vig deploy verify ${domain}\n`));
307
+ }
308
+
309
+ } catch (error) {
310
+ spinner.fail('Deploy failed');
311
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
312
+ console.log(chalk.red(`\nāŒ Error: ${errMsg}\n`));
313
+ }
314
+ }
315
+
316
+ /**
317
+ * Interactive subdomain prompt
318
+ */
319
+ async promptSubdomainDeploy(projectPath?: string): Promise<void> {
320
+ const { subdomain } = await inquirer.prompt([{
321
+ type: 'input',
322
+ name: 'subdomain',
323
+ message: 'Enter your desired subdomain:',
324
+ suffix: chalk.gray('.vigthoria.io'),
325
+ validate: (input: string) => {
326
+ if (!/^[a-z0-9][a-z0-9-]*[a-z0-9]$/.test(input) || input.length < 3) {
327
+ return 'Subdomain must be 3+ chars, lowercase alphanumeric with hyphens';
328
+ }
329
+ return true;
330
+ }
331
+ }]);
332
+
333
+ await this.deployToSubdomain(subdomain, projectPath);
334
+ }
335
+
336
+ /**
337
+ * Interactive custom domain prompt
338
+ */
339
+ async promptCustomDomainDeploy(projectPath?: string): Promise<void> {
340
+ const { domain } = await inquirer.prompt([{
341
+ type: 'input',
342
+ name: 'domain',
343
+ message: 'Enter your domain:',
344
+ suffix: chalk.gray(' (e.g., myapp.com)'),
345
+ validate: (input: string) => {
346
+ if (!/^[a-z0-9][a-z0-9.-]+\.[a-z]{2,}$/i.test(input)) {
347
+ return 'Please enter a valid domain name';
348
+ }
349
+ return true;
350
+ }
351
+ }]);
352
+
353
+ await this.deployToCustomDomain(domain, projectPath);
354
+ }
355
+
356
+ /**
357
+ * Show hosting plans
358
+ */
359
+ async showPlans(): Promise<void> {
360
+ const spinner = ora('Fetching hosting plans...').start();
361
+
362
+ try {
363
+ const response = await fetch(`${this.apiBase}/api/hosting/plans`, {
364
+ headers: this.getAuthHeaders()
365
+ });
366
+
367
+ if (!response.ok) {
368
+ throw new Error('Failed to fetch plans');
369
+ }
370
+
371
+ const data = await response.json() as { success: boolean; plans: HostingPlan[] };
372
+
373
+ spinner.stop();
374
+
375
+ console.log(chalk.cyan('\nšŸ“Š Vigthoria Hosting Plans\n'));
376
+ console.log(chalk.gray('═'.repeat(70)));
377
+
378
+ for (const plan of data.plans) {
379
+ const price = plan.price_monthly === 0
380
+ ? chalk.green('FREE')
381
+ : chalk.yellow(`€${plan.price_monthly.toFixed(2)}/mo`);
382
+
383
+ console.log(chalk.bold.white(`\n ${plan.display_name} - ${price}`));
384
+ console.log(chalk.gray(' ' + '─'.repeat(50)));
385
+
386
+ const features = [];
387
+ if (plan.subdomain_allowed) features.push('āœ“ Vigthoria subdomain');
388
+ if (plan.custom_domain_allowed) features.push('āœ“ Custom domain');
389
+ features.push(`āœ“ ${plan.max_projects === -1 ? 'Unlimited' : plan.max_projects} project(s)`);
390
+ features.push(`āœ“ ${plan.storage_mb >= 1024 ? (plan.storage_mb / 1024) + 'GB' : plan.storage_mb + 'MB'} storage`);
391
+
392
+ features.forEach(f => console.log(chalk.gray(` ${f}`)));
393
+ }
394
+
395
+ console.log(chalk.gray('\n' + '═'.repeat(70)));
396
+ console.log(chalk.cyan('\n Subscribe: vig deploy --subdomain myapp\n'));
397
+
398
+ } catch (error) {
399
+ spinner.fail('Failed to fetch plans');
400
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
401
+ console.log(chalk.red(`\nāŒ Error: ${errMsg}\n`));
402
+ }
403
+ }
404
+
405
+ /**
406
+ * List all deployments
407
+ */
408
+ async list(): Promise<void> {
409
+ this.requireAuth();
410
+
411
+ const spinner = ora('Fetching deployments...').start();
412
+
413
+ try {
414
+ const response = await fetch(`${this.apiBase}/api/hosting/domains`, {
415
+ headers: this.getAuthHeaders()
416
+ });
417
+
418
+ if (!response.ok) {
419
+ throw new Error('Failed to fetch deployments');
420
+ }
421
+
422
+ const data = await response.json() as { success: boolean; domains: DeployedDomain[] };
423
+
424
+ spinner.stop();
425
+
426
+ if (data.domains.length === 0) {
427
+ console.log(chalk.yellow('\nšŸ“¦ No deployments yet.\n'));
428
+ console.log(chalk.gray(' Run `vig deploy` to deploy your first project.\n'));
429
+ return;
430
+ }
431
+
432
+ console.log(chalk.cyan(`\n🌐 Your Deployments (${data.domains.length})\n`));
433
+
434
+ for (const domain of data.domains) {
435
+ const statusIcon = domain.is_active ? '🟢' : 'šŸ”“';
436
+ const sslIcon = domain.ssl_status === 'active' ? 'šŸ”’' : 'āš ļø';
437
+
438
+ const url = domain.domain_type === 'subdomain'
439
+ ? `${domain.subdomain}.vigthoria.io`
440
+ : domain.domain_type === 'custom'
441
+ ? domain.custom_domain
442
+ : domain.url;
443
+
444
+ console.log(chalk.white(` ${statusIcon} ${url}`));
445
+ console.log(chalk.gray(` Project: ${domain.project_name} | SSL: ${sslIcon} ${domain.ssl_status} | Tier: ${domain.hosting_tier}`));
446
+ console.log();
447
+ }
448
+
449
+ } catch (error) {
450
+ spinner.fail('List failed');
451
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
452
+ console.log(chalk.red(`\nāŒ Error: ${errMsg}\n`));
453
+ }
454
+ }
455
+
456
+ /**
457
+ * Check deployment status
458
+ */
459
+ async status(domain?: string): Promise<void> {
460
+ this.requireAuth();
461
+
462
+ const spinner = ora('Checking status...').start();
463
+
464
+ try {
465
+ const endpoint = domain
466
+ ? `${this.apiBase}/api/hosting/domain/${encodeURIComponent(domain)}/status`
467
+ : `${this.apiBase}/api/hosting/status`;
468
+
469
+ const response = await fetch(endpoint, {
470
+ headers: this.getAuthHeaders()
471
+ });
472
+
473
+ if (!response.ok) {
474
+ throw new Error('Failed to fetch status');
475
+ }
476
+
477
+ const data = await response.json();
478
+
479
+ spinner.stop();
480
+
481
+ console.log(chalk.cyan('\nšŸ“Š Deployment Status\n'));
482
+ console.log(JSON.stringify(data, null, 2));
483
+
484
+ } catch (error) {
485
+ spinner.fail('Status check failed');
486
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
487
+ console.log(chalk.red(`\nāŒ Error: ${errMsg}\n`));
488
+ }
489
+ }
490
+
491
+ /**
492
+ * Verify DNS for custom domain
493
+ */
494
+ async verify(domain: string): Promise<void> {
495
+ this.requireAuth();
496
+
497
+ const spinner = ora(`Verifying DNS for ${domain}...`).start();
498
+
499
+ try {
500
+ const response = await fetch(`${this.apiBase}/api/hosting/domain/verify`, {
501
+ method: 'POST',
502
+ headers: this.getAuthHeaders(),
503
+ body: JSON.stringify({ domain })
504
+ });
505
+
506
+ const data = await response.json() as {
507
+ success: boolean;
508
+ verified: boolean;
509
+ error?: string;
510
+ sslStatus?: string;
511
+ };
512
+
513
+ if (!response.ok || !data.success) {
514
+ throw new Error(data.error || 'Verification failed');
515
+ }
516
+
517
+ if (data.verified) {
518
+ spinner.succeed(chalk.green('Domain verified!'));
519
+ console.log(chalk.cyan(`\nšŸŽ‰ Your site is now live at: https://${domain}`));
520
+ console.log(chalk.gray(` SSL Status: ${data.sslStatus || 'Provisioning...'}\n`));
521
+ } else {
522
+ spinner.warn(chalk.yellow('DNS not propagated yet'));
523
+ console.log(chalk.gray('\n DNS changes can take up to 48 hours to propagate.'));
524
+ console.log(chalk.gray(' Try again later with: vig deploy verify ' + domain + '\n'));
525
+ }
526
+
527
+ } catch (error) {
528
+ spinner.fail('Verification failed');
529
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
530
+ console.log(chalk.red(`\nāŒ Error: ${errMsg}\n`));
531
+ }
532
+ }
533
+
534
+ /**
535
+ * Remove a deployment
536
+ */
537
+ async remove(domain: string): Promise<void> {
538
+ this.requireAuth();
539
+
540
+ const { confirm } = await inquirer.prompt([{
541
+ type: 'confirm',
542
+ name: 'confirm',
543
+ message: chalk.red(`Are you sure you want to remove ${domain}?`),
544
+ default: false
545
+ }]);
546
+
547
+ if (!confirm) {
548
+ console.log(chalk.yellow('\nāš ļø Removal cancelled.\n'));
549
+ return;
550
+ }
551
+
552
+ const spinner = ora(`Removing ${domain}...`).start();
553
+
554
+ try {
555
+ const response = await fetch(`${this.apiBase}/api/hosting/domain/${encodeURIComponent(domain)}`, {
556
+ method: 'DELETE',
557
+ headers: this.getAuthHeaders()
558
+ });
559
+
560
+ if (!response.ok) {
561
+ const error = await response.json() as { error?: string };
562
+ throw new Error(error.error || 'Failed to remove');
563
+ }
564
+
565
+ spinner.succeed(chalk.green('Domain removed'));
566
+ console.log(chalk.gray('\n Your project files are still in your repository.\n'));
567
+
568
+ } catch (error) {
569
+ spinner.fail('Remove failed');
570
+ const errMsg = error instanceof Error ? error.message : 'Unknown error';
571
+ console.log(chalk.red(`\nāŒ Error: ${errMsg}\n`));
572
+ }
573
+ }
574
+
575
+ /**
576
+ * Detect project info from directory
577
+ */
578
+ private detectProjectInfo(projectPath: string): { name: string; techStack: string[] } {
579
+ const packageJsonPath = path.join(projectPath, 'package.json');
580
+ let name = path.basename(projectPath);
581
+ const techStack: string[] = [];
582
+
583
+ if (fs.existsSync(packageJsonPath)) {
584
+ try {
585
+ const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
586
+ name = pkg.name || name;
587
+
588
+ const deps = { ...pkg.dependencies, ...pkg.devDependencies };
589
+ if (deps.react) techStack.push('React');
590
+ if (deps.vue) techStack.push('Vue');
591
+ if (deps.next) techStack.push('Next.js');
592
+ if (deps.express) techStack.push('Express');
593
+ if (deps.typescript) techStack.push('TypeScript');
594
+ } catch (e) {
595
+ // Ignore parse errors
596
+ }
597
+ }
598
+
599
+ // Check for other frameworks
600
+ if (fs.existsSync(path.join(projectPath, 'requirements.txt'))) {
601
+ techStack.push('Python');
602
+ }
603
+ if (fs.existsSync(path.join(projectPath, 'index.html'))) {
604
+ techStack.push('Static HTML');
605
+ }
606
+
607
+ return { name, techStack };
608
+ }
609
+ }