Skip to content

Commit a0a579f

Browse files
committed
Add Vercel deployment command and update README with deployment instructions
1 parent 971759f commit a0a579f

File tree

4 files changed

+240
-3
lines changed

4 files changed

+240
-3
lines changed

README.md

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This tool eliminates the need for manual configurations, boilerplate code copyin
4242
- [6. devcli init-dockerfiles](#6-initialize-docker-files)
4343
- [7. devcli add-eslint](#7-add-eslint-and-prettierrc)
4444
- [8. devcli add-jwt-auth](#8-add-jwt-authetication-and-authorization)
45+
- [9. devcli deploy --vercel](#9-deploy-frontend-to-Vercel)
4546
- [Complete User Journey Example](#-Complete-User-Journey-Example)
4647
- [Future Enhancements](#-future-enhancements)
4748
- [Contribute](#-contribute-to-the-project)
@@ -569,8 +570,6 @@ This command generates a basic ESLint configuration file (`.eslintrc.json`) that
569570
### 8. Add JWT Authetication and Authorization
570571
Here is the content for the 8th command, "Add JWT Authentication":
571572

572-
### 8. Add JWT Authentication and Authorization
573-
574573
Add JWT authentication boilerplate to your backend project.
575574

576575
```bash
@@ -639,6 +638,133 @@ The generated files implement the following functionality:
639638

640639
After running this command, you can start using the authentication system in your backend application.
641640

641+
### 9. Deploy Frontend to Vercel
642+
Deploy your frontend application to Vercel with a single command. This feature supports production deployments, preview deployments, custom domains, and automatic environment variable handling.
643+
644+
# 🚀 Frontend Deployment to Vercel CLI Guide
645+
646+
## Overview
647+
Deploy your frontend application to Vercel with a single command using the `devcli` tool. This feature supports production deployments, preview deployments, custom domains, and automatic environment variable handling.
648+
649+
## Command Syntax
650+
```bash
651+
devcli deploy --vercel [options]
652+
```
653+
654+
## Options
655+
656+
| Option | Description |
657+
|--------|-------------|
658+
| `--vercel` | Deploy the frontend to Vercel |
659+
| `--preview` | Deploy a preview version (not production) |
660+
| `--domain <domain>` | Specify a custom domain for deployment (e.g., `myapp.com`) |
661+
662+
## Features
663+
664+
#### 1. Automatic Environment Variable Handling
665+
- Uploads environment variables from `.env` files automatically
666+
- Validates required environment variables before deployment
667+
668+
#### 2. Preview Deployments
669+
- Deploy a preview version of your app for testing and validation
670+
671+
#### 3. Custom Domain Support
672+
- Deploy your application to a custom domain
673+
674+
#### 4. Deployment Status and URL
675+
- Displays the deployment URL after successful deployment
676+
677+
#### 5. Vercel Login Check
678+
- Automatically checks if the user is logged in to Vercel
679+
- Prompts for login if not authenticated
680+
681+
#### Usage Examples
682+
683+
#### 1. Production Deployment
684+
```bash
685+
devcli deploy --vercel
686+
```
687+
688+
**Expected Output:**
689+
```
690+
🚀 Deploying frontend to Vercel...
691+
📦 Uploading environment variables...
692+
✅ Frontend deployed successfully!
693+
694+
🎉 Your frontend has been deployed to Vercel!
695+
👉 Open the deployed URL: https://myapp.vercel.app
696+
```
697+
698+
#### 2. Preview Deployment
699+
```bash
700+
devcli deploy --vercel --preview
701+
```
702+
703+
**Expected Output:**
704+
```
705+
🚀 Deploying frontend to Vercel (preview)...
706+
📦 Uploading environment variables...
707+
✅ Frontend deployed successfully!
708+
709+
🎉 Your frontend has been deployed to Vercel!
710+
👉 Open the preview URL: https://myapp-git-branch.vercel.app
711+
```
712+
713+
#### 3. Custom Domain Deployment
714+
```bash
715+
devcli deploy --vercel --domain myapp.com
716+
```
717+
718+
**Expected Output:**
719+
```
720+
🚀 Deploying frontend to Vercel with custom domain...
721+
📦 Uploading environment variables...
722+
✅ Frontend deployed successfully!
723+
724+
🎉 Your frontend has been deployed to Vercel!
725+
👉 Open the deployed URL: https://myapp.com
726+
```
727+
728+
### Prerequisites
729+
730+
### Vercel CLI Installation
731+
Install the Vercel CLI globally:
732+
```bash
733+
npm install -g vercel
734+
```
735+
736+
### Vercel Login
737+
Log in to Vercel:
738+
```bash
739+
vercel login
740+
```
741+
742+
### Error Handling
743+
744+
#### 1. Invalid Directory
745+
```
746+
❌ This does not seem to be a valid frontend app. Make sure you are in the root of your frontend project.
747+
```
748+
749+
#### 2. Vercel CLI Not Installed
750+
```
751+
❌ Vercel CLI is not installed. Please install it using `npm install -g vercel`.
752+
```
753+
754+
#### 3. Vercel Login Required
755+
```
756+
🔑 You are not logged in to Vercel. Please log in to continue.
757+
```
758+
759+
### Best Practices
760+
- Ensure your project has a valid `package.json`
761+
- Configure your Vercel project settings in the Vercel dashboard
762+
- Use environment variables for sensitive configuration
763+
- Regularly test preview deployments before production
764+
765+
### Support
766+
For additional help, consult the Vercel documentation or contact your development team's support channels.
767+
642768

643769
## 📖 Complete User Journey Example
644770

commands/deployCommand.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { execSync } from 'child_process';
2+
import fs from 'fs-extra';
3+
import path from 'path';
4+
import chalk from 'chalk';
5+
6+
function checkVercelLogin() {
7+
try {
8+
// Check if the user is logged in to Vercel
9+
execSync('vercel whoami', { stdio: 'ignore' });
10+
return true;
11+
} catch (error) {
12+
return false;
13+
}
14+
}
15+
16+
function deployFrontendToVercel(options) {
17+
const currentDir = process.cwd();
18+
19+
// Step 1: Check if the current directory is a valid frontend app
20+
if (!fs.existsSync(path.join(currentDir, 'package.json'))) {
21+
console.error(
22+
chalk.red(
23+
'❌ This does not seem to be a valid frontend app. Make sure you are in the root of your frontend project.'
24+
)
25+
);
26+
process.exit(1);
27+
}
28+
29+
// Step 2: Check if Vercel CLI is installed
30+
try {
31+
execSync('vercel --version', { stdio: 'ignore' });
32+
} catch (error) {
33+
console.error(
34+
chalk.red(
35+
'❌ Vercel CLI is not installed. Please install it using `npm install -g vercel`.'
36+
)
37+
);
38+
process.exit(1);
39+
}
40+
41+
// Step 3: Check if the user is logged in to Vercel
42+
if (!checkVercelLogin()) {
43+
console.log(
44+
chalk.yellow(
45+
'🔑 You are not logged in to Vercel. Please log in to continue.'
46+
)
47+
);
48+
try {
49+
execSync('vercel login', { stdio: 'inherit' });
50+
} catch (error) {
51+
console.error(chalk.red('❌ Failed to log in to Vercel.'));
52+
process.exit(1);
53+
}
54+
}
55+
56+
// Step 4: Upload environment variables (if .env file exists)
57+
if (fs.existsSync(path.join(currentDir, '.env'))) {
58+
console.log(chalk.blue('📦 Uploading environment variables...'));
59+
try {
60+
execSync('vercel env pull .env', { stdio: 'inherit' });
61+
} catch (error) {
62+
console.error(chalk.red('❌ Failed to upload environment variables.'));
63+
process.exit(1);
64+
}
65+
}
66+
67+
// Step 5: Deploy to Vercel
68+
console.log(chalk.blue('🚀 Deploying frontend to Vercel...'));
69+
try {
70+
const deployCommand = options.preview ? 'vercel' : 'vercel --prod';
71+
execSync(deployCommand, { stdio: 'inherit' });
72+
console.log(chalk.green('✅ Frontend deployed successfully!'));
73+
} catch (error) {
74+
console.error(chalk.red(`❌ Failed to deploy frontend: ${error.message}`));
75+
process.exit(1);
76+
}
77+
78+
// Step 6: Display deployment URL
79+
try {
80+
const deploymentUrl = execSync('vercel --prod --yes', {
81+
stdio: 'pipe',
82+
}).toString();
83+
console.log(
84+
chalk.green.bold('\n🎉 Your frontend has been deployed to Vercel!')
85+
);
86+
console.log(chalk.cyan(`👉 Open the deployed URL: ${deploymentUrl}`));
87+
} catch (error) {
88+
console.error(chalk.red('❌ Failed to retrieve deployment URL.'));
89+
}
90+
}
91+
92+
export default function addDeployCommand(program) {
93+
program
94+
.command('deploy')
95+
.description('Deploy your frontend to Vercel')
96+
.option('--vercel', 'Deploy to Vercel')
97+
.option('--preview', 'Deploy a preview version (not production)')
98+
.option('--domain <domain>', 'Specify a custom domain for deployment')
99+
.action(async (options) => {
100+
if (!options.vercel) {
101+
console.log(
102+
chalk.red('Please specify a deployment target (e.g., --vercel)')
103+
);
104+
process.exit(1);
105+
}
106+
107+
deployFrontendToVercel(options);
108+
});
109+
}

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import createFrontend from './commands/createFrontend.js';
1414
import initializeDockerCommand from './commands/initializeDocker.js';
1515
import addESLintCommand from './commands/addESLint.js';
1616
import addJWTAuthCommand from './commands/addJWTAuth.js';
17+
import addDeployCommand from './commands/deployCommand.js';
1718

1819
// Step 1: Get the directory of the current file
1920
const __filename = fileURLToPath(import.meta.url);
@@ -43,6 +44,7 @@ createFrontend(program);
4344
initializeDockerCommand(program);
4445
addESLintCommand(program);
4546
addJWTAuthCommand(program);
47+
addDeployCommand(program);
4648

4749
// Step 5: Parse the provided arguments and start the CLI
4850
program.parse(process.argv);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mern-project-cli",
3-
"version": "2.1.4",
3+
"version": "2.1.5",
44
"description": "A developer-friendly CLI tool that streamlines MERN stack development by automating project setup, database configuration, and boilerplate generation by implementing MVC Architecture. Create production-ready MongoDB, Express, React, and Node.js applications with best practices built-in",
55
"main": "index.js",
66
"type": "module",

0 commit comments

Comments
 (0)