Skip to content

Commit acde7d6

Browse files
benjaminshafiiopencode
andcommitted
feat: Add local development support for CLI
- Added environment variable support via dotenv - Created auth config command to switch between local/production - Added helper scripts for easy environment switching - Updated auth command with better error messages for local dev - Added comprehensive local development guide - Added useful npm scripts for common operations Now you can easily switch between local and production: - npm run use:local - Configure for localhost - npm run use:production - Configure for production - npm run config - Check current configuration The CLI now properly supports local development with: - ZERO_API_URL and ZERO_WEB_URL environment variables - .env.local file support - Configuration commands - Clear status indicators 🤖 Generated with [opencode](https://opencode.ai) Co-Authored-By: opencode <[email protected]>
1 parent 8d8609f commit acde7d6

File tree

7 files changed

+360
-16
lines changed

7 files changed

+360
-16
lines changed

packages/cli/LOCAL_DEVELOPMENT.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# Local Development Guide for Zero Finance CLI
2+
3+
## Quick Start
4+
5+
### 1. Configure CLI for Local Development
6+
7+
```bash
8+
# Option 1: Use the script
9+
npm run use:local
10+
11+
# Option 2: Use environment variables
12+
export ZERO_API_URL=http://localhost:3000/api/trpc
13+
export ZERO_WEB_URL=http://localhost:3000
14+
npm start
15+
16+
# Option 3: Configure manually
17+
npm run config -- --api-url http://localhost:3000/api/trpc --web-url http://localhost:3000
18+
```
19+
20+
### 2. Start the Web App
21+
22+
In another terminal:
23+
```bash
24+
cd ../web
25+
npm run dev
26+
```
27+
28+
The web app should be running at http://localhost:3000
29+
30+
### 3. Authenticate
31+
32+
```bash
33+
npm run auth:login
34+
```
35+
36+
This will:
37+
1. Open your browser to http://localhost:3000/cli-auth
38+
2. Sign in if needed
39+
3. Generate a token
40+
4. Copy and paste the token into the CLI
41+
42+
### 4. Verify Authentication
43+
44+
```bash
45+
npm run auth:status
46+
```
47+
48+
You should see:
49+
- ✓ Valid authentication token
50+
- API: http://localhost:3000/api/trpc
51+
- Web: http://localhost:3000
52+
- Environment: Local Development
53+
54+
## Configuration Methods
55+
56+
### Method 1: Environment Variables (.env.local)
57+
58+
Create or edit `packages/cli/.env.local`:
59+
```env
60+
ZERO_API_URL=http://localhost:3000/api/trpc
61+
ZERO_WEB_URL=http://localhost:3000
62+
```
63+
64+
These are automatically loaded when the CLI starts.
65+
66+
### Method 2: CLI Configuration Command
67+
68+
```bash
69+
# Set local URLs
70+
npm run config -- --api-url http://localhost:3000/api/trpc --web-url http://localhost:3000
71+
72+
# Check current configuration
73+
npm run config
74+
75+
# Reset to production
76+
npm run config -- --reset
77+
```
78+
79+
### Method 3: Helper Scripts
80+
81+
```bash
82+
# Switch to local development
83+
npm run use:local
84+
85+
# Switch back to production
86+
npm run use:production
87+
```
88+
89+
## Available NPM Scripts
90+
91+
- `npm run use:local` - Configure for localhost
92+
- `npm run use:production` - Configure for production
93+
- `npm run auth:login` - Authenticate
94+
- `npm run auth:status` - Check auth status
95+
- `npm run auth:logout` - Log out
96+
- `npm run config` - Show/update configuration
97+
- `npm test` - Run automated tests
98+
- `npm run test:mock` - Run with mock auth
99+
100+
## Troubleshooting
101+
102+
### "Authentication failed" when using localhost
103+
104+
1. **Check the web app is running:**
105+
```bash
106+
cd ../web
107+
npm run dev
108+
```
109+
110+
2. **Verify you can access the web app:**
111+
Open http://localhost:3000 in your browser
112+
113+
3. **Check your configuration:**
114+
```bash
115+
npm run config
116+
```
117+
Should show:
118+
- API URL: http://localhost:3000/api/trpc
119+
- Web URL: http://localhost:3000
120+
121+
4. **Try logging out and back in:**
122+
```bash
123+
npm run auth:logout
124+
npm run auth:login
125+
```
126+
127+
### "Config file corrupted" message
128+
129+
This is normal when switching between versions. The CLI will create a new config automatically.
130+
131+
### Can't connect to API
132+
133+
1. Make sure the web app is running
134+
2. Check that you're using the correct port (default is 3000)
135+
3. If using a different port, update the configuration:
136+
```bash
137+
npm run config -- --api-url http://localhost:YOUR_PORT/api/trpc --web-url http://localhost:YOUR_PORT
138+
```
139+
140+
## Testing Without Real API
141+
142+
For testing CLI features without a running backend:
143+
```bash
144+
# Use the mock auth version
145+
npm run test:mock
146+
147+
# Run specific commands
148+
node test-cli-full.js auth login
149+
node test-cli-full.js company
150+
node test-cli-full.js balance
151+
```
152+
153+
## Switching Between Environments
154+
155+
### To Local Development:
156+
```bash
157+
npm run use:local
158+
npm run auth:login # Re-authenticate for local
159+
```
160+
161+
### To Production:
162+
```bash
163+
npm run use:production
164+
npm run auth:login # Re-authenticate for production
165+
```
166+
167+
### Check Current Environment:
168+
```bash
169+
npm run config
170+
# or
171+
npm run auth:status
172+
```
173+
174+
## Development Workflow
175+
176+
1. **Start both services:**
177+
```bash
178+
# Terminal 1: Web app
179+
cd packages/web
180+
npm run dev
181+
182+
# Terminal 2: CLI
183+
cd packages/cli
184+
npm run use:local
185+
npm run auth:login
186+
```
187+
188+
2. **Make changes to CLI code**
189+
190+
3. **Test your changes:**
191+
```bash
192+
npm start -- [your command]
193+
```
194+
195+
4. **Run automated tests:**
196+
```bash
197+
npm test
198+
```
199+
200+
5. **Switch back to production when done:**
201+
```bash
202+
npm run use:production
203+
```

packages/cli/package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
"start": "node src/index.js",
1212
"dev": "node --watch src/index.js",
1313
"build": "echo 'No build step required'",
14-
"test": "echo 'No tests yet'"
14+
"test": "node test-cli.js",
15+
"test:mock": "node test-cli-full.js",
16+
"use:local": "./scripts/use-local.sh",
17+
"use:production": "./scripts/use-production.sh",
18+
"auth:login": "node src/index.js auth login",
19+
"auth:status": "node src/index.js auth status",
20+
"auth:logout": "node src/index.js auth logout",
21+
"config": "node src/index.js auth config"
1522
},
1623
"keywords": [
1724
"zero-finance",
@@ -29,6 +36,7 @@
2936
"cli-table3": "^0.6.3",
3037
"commander": "^11.1.0",
3138
"conf": "^12.0.0",
39+
"dotenv": "^17.2.1",
3240
"figlet": "^1.7.0",
3341
"gradient-string": "^2.0.2",
3442
"inquirer": "^9.2.12",
@@ -41,4 +49,4 @@
4149
"devDependencies": {
4250
"@types/qrcode": "^1.5.5"
4351
}
44-
}
52+
}

packages/cli/scripts/use-local.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# Script to configure CLI for local development
4+
5+
echo "🔧 Configuring Zero Finance CLI for local development..."
6+
7+
# Set local URLs
8+
node src/index.js auth config --api-url http://localhost:3000/api/trpc --web-url http://localhost:3000
9+
10+
echo ""
11+
echo "✅ CLI configured for local development!"
12+
echo ""
13+
echo "Make sure the web app is running:"
14+
echo " cd ../web && npm run dev"
15+
echo ""
16+
echo "Then authenticate:"
17+
echo " npm start auth login"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# Script to configure CLI for production
4+
5+
echo "🔧 Configuring Zero Finance CLI for production..."
6+
7+
# Reset to production URLs
8+
node src/index.js auth config --reset
9+
10+
echo ""
11+
echo "✅ CLI configured for production!"
12+
echo ""
13+
echo "Authenticate with:"
14+
echo " npm start auth login"

packages/cli/src/commands/auth.js

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ authCommand
1717
.action(async () => {
1818
console.log(chalk.cyan('🔐 Zero Finance CLI Authentication\n'));
1919

20-
const authUrl = 'https://zerofinance.ai/cli-auth';
20+
const webUrl = config.get('api.webUrl');
21+
const authUrl = `${webUrl}/cli-auth`;
22+
2123
console.log(chalk.yellow('Opening browser for authentication...'));
2224
console.log(`If browser doesn't open, visit: ${chalk.bold(authUrl)}\n`);
2325

@@ -51,13 +53,25 @@ authCommand
5153

5254
spinner.succeed('Authentication successful!');
5355
console.log(chalk.green('\n✓ You are now authenticated with Zero Finance CLI'));
56+
57+
// Show current environment
58+
if (webUrl.includes('localhost')) {
59+
console.log(chalk.dim(` Connected to: ${webUrl} (local development)`));
60+
}
5461
} catch (error) {
5562
// Remove invalid token
5663
config.delete('auth.token');
5764
config.delete('auth.expiresAt');
5865

5966
spinner.fail('Authentication failed');
6067
console.error(chalk.red('\n✗ Invalid token. Please try again.'));
68+
69+
if (webUrl.includes('localhost')) {
70+
console.log(chalk.yellow('\nNote: You are connecting to localhost. Make sure:'));
71+
console.log(chalk.dim(' 1. The web app is running (npm run dev in packages/web)'));
72+
console.log(chalk.dim(' 2. You are signed in at http://localhost:3000'));
73+
}
74+
6175
process.exit(1);
6276
}
6377
});
@@ -77,6 +91,8 @@ authCommand
7791
.action(async () => {
7892
const token = config.get('auth.token');
7993
const expiresAt = config.get('auth.expiresAt');
94+
const apiUrl = config.get('api.url');
95+
const webUrl = config.get('api.webUrl');
8096

8197
if (!token) {
8298
console.log(chalk.yellow('✗ Not authenticated'));
@@ -106,11 +122,60 @@ authCommand
106122
const daysLeft = Math.ceil((expires - new Date()) / (1000 * 60 * 60 * 24));
107123
console.log(chalk.dim(` Token expires in ${daysLeft} days`));
108124
}
125+
126+
// Show environment info
127+
console.log(chalk.dim(` API: ${apiUrl}`));
128+
console.log(chalk.dim(` Web: ${webUrl}`));
129+
130+
if (apiUrl.includes('localhost')) {
131+
console.log(chalk.cyan(' Environment: Local Development'));
132+
} else {
133+
console.log(chalk.cyan(' Environment: Production'));
134+
}
109135
} catch (error) {
110136
spinner.fail('Authentication check failed');
111137
console.log(chalk.red('✗ Token is invalid or expired'));
112138
console.log(chalk.dim('Run "zero auth login" to re-authenticate'));
139+
140+
if (apiUrl.includes('localhost')) {
141+
console.log(chalk.yellow('\nNote: Connecting to localhost. Is the web app running?'));
142+
}
143+
}
144+
});
145+
146+
authCommand
147+
.command('config')
148+
.description('Show or update CLI configuration')
149+
.option('--api-url <url>', 'Set the API URL')
150+
.option('--web-url <url>', 'Set the Web URL')
151+
.option('--reset', 'Reset to default configuration')
152+
.action((options) => {
153+
if (options.reset) {
154+
config.set('api.url', 'https://zerofinance.ai/api/trpc');
155+
config.set('api.webUrl', 'https://zerofinance.ai');
156+
console.log(chalk.green('✓ Configuration reset to defaults'));
157+
return;
158+
}
159+
160+
if (options.apiUrl) {
161+
config.set('api.url', options.apiUrl);
162+
console.log(chalk.green(`✓ API URL set to: ${options.apiUrl}`));
163+
}
164+
165+
if (options.webUrl) {
166+
config.set('api.webUrl', options.webUrl);
167+
console.log(chalk.green(`✓ Web URL set to: ${options.webUrl}`));
168+
}
169+
170+
// Show current configuration
171+
console.log(chalk.cyan('\nCurrent Configuration:'));
172+
console.log(` API URL: ${config.get('api.url')}`);
173+
console.log(` Web URL: ${config.get('api.webUrl')}`);
174+
175+
const apiUrl = config.get('api.url');
176+
if (apiUrl.includes('localhost')) {
177+
console.log(chalk.yellow('\n⚠️ Using local development environment'));
113178
}
114179
});
115180

116-
export default authCommand;
181+
export default authCommand;

0 commit comments

Comments
 (0)