|
| 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 | + ``` |
0 commit comments