Skip to content

Commit 6ce8217

Browse files
committed
feat: add start option
1 parent 3545054 commit 6ce8217

17 files changed

+652
-476
lines changed

CHANGELOG.md

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -326,27 +326,27 @@
326326

327327
## v0.0.1...main
328328

329-
[compare changes](https://github.com/stacksjs/reverse-proxy/compare/v0.0.1...main)
329+
[compare changes](https://github.com/stacksjs/rpx/compare/v0.0.1...main)
330330

331331
### 🏡 Chore
332332

333-
- Wip ([c5e31d5](https://github.com/stacksjs/reverse-proxy/commit/c5e31d5))
334-
- Wip ([6020df1](https://github.com/stacksjs/reverse-proxy/commit/6020df1))
335-
- Wip ([be3dde1](https://github.com/stacksjs/reverse-proxy/commit/be3dde1))
336-
- Wip ([e9e0feb](https://github.com/stacksjs/reverse-proxy/commit/e9e0feb))
337-
- Wip ([383854c](https://github.com/stacksjs/reverse-proxy/commit/383854c))
338-
- Wip ([363445b](https://github.com/stacksjs/reverse-proxy/commit/363445b))
339-
- Add `rp` ([beced3c](https://github.com/stacksjs/reverse-proxy/commit/beced3c))
340-
- Wip ([99cf0df](https://github.com/stacksjs/reverse-proxy/commit/99cf0df))
341-
- Wip ([c9341a4](https://github.com/stacksjs/reverse-proxy/commit/c9341a4))
342-
- Use `bun publish` ([6f6ae9d](https://github.com/stacksjs/reverse-proxy/commit/6f6ae9d))
343-
- Add tests ([da19c51](https://github.com/stacksjs/reverse-proxy/commit/da19c51))
344-
- Ignore docs cache ([81f2296](https://github.com/stacksjs/reverse-proxy/commit/81f2296))
345-
- Use bunx ([ec65717](https://github.com/stacksjs/reverse-proxy/commit/ec65717))
346-
- Adjust examples ([d4745fc](https://github.com/stacksjs/reverse-proxy/commit/d4745fc))
347-
- Adjust build process ([1e5c63c](https://github.com/stacksjs/reverse-proxy/commit/1e5c63c))
348-
- Remove postcompile ([52a9bc6](https://github.com/stacksjs/reverse-proxy/commit/52a9bc6))
349-
- Remove lint:fix-unsafe ([91ad947](https://github.com/stacksjs/reverse-proxy/commit/91ad947))
333+
- Wip ([c5e31d5](https://github.com/stacksjs/rpx/commit/c5e31d5))
334+
- Wip ([6020df1](https://github.com/stacksjs/rpx/commit/6020df1))
335+
- Wip ([be3dde1](https://github.com/stacksjs/rpx/commit/be3dde1))
336+
- Wip ([e9e0feb](https://github.com/stacksjs/rpx/commit/e9e0feb))
337+
- Wip ([383854c](https://github.com/stacksjs/rpx/commit/383854c))
338+
- Wip ([363445b](https://github.com/stacksjs/rpx/commit/363445b))
339+
- Add `rp` ([beced3c](https://github.com/stacksjs/rpx/commit/beced3c))
340+
- Wip ([99cf0df](https://github.com/stacksjs/rpx/commit/99cf0df))
341+
- Wip ([c9341a4](https://github.com/stacksjs/rpx/commit/c9341a4))
342+
- Use `bun publish` ([6f6ae9d](https://github.com/stacksjs/rpx/commit/6f6ae9d))
343+
- Add tests ([da19c51](https://github.com/stacksjs/rpx/commit/da19c51))
344+
- Ignore docs cache ([81f2296](https://github.com/stacksjs/rpx/commit/81f2296))
345+
- Use bunx ([ec65717](https://github.com/stacksjs/rpx/commit/ec65717))
346+
- Adjust examples ([d4745fc](https://github.com/stacksjs/rpx/commit/d4745fc))
347+
- Adjust build process ([1e5c63c](https://github.com/stacksjs/rpx/commit/1e5c63c))
348+
- Remove postcompile ([52a9bc6](https://github.com/stacksjs/rpx/commit/52a9bc6))
349+
- Remove lint:fix-unsafe ([91ad947](https://github.com/stacksjs/rpx/commit/91ad947))
350350

351351
### ❤️ Contributors
352352

@@ -357,35 +357,34 @@
357357

358358
### 🏡 Chore
359359

360-
- Initial commit ([beb4d96](https://github.com/stacksjs/reverse-proxy/commit/beb4d96))
361-
- Wip ([8e3fce6](https://github.com/stacksjs/reverse-proxy/commit/8e3fce6))
362-
- Wip ([bbb6783](https://github.com/stacksjs/reverse-proxy/commit/bbb6783))
363-
- Wip ([2cfb363](https://github.com/stacksjs/reverse-proxy/commit/2cfb363))
364-
- Wip ([6bb85ac](https://github.com/stacksjs/reverse-proxy/commit/6bb85ac))
365-
- Wip ([602c9e1](https://github.com/stacksjs/reverse-proxy/commit/602c9e1))
366-
- Wip ([5986105](https://github.com/stacksjs/reverse-proxy/commit/5986105))
367-
- Wip ([c2bfec6](https://github.com/stacksjs/reverse-proxy/commit/c2bfec6))
368-
- Wip ([454dd58](https://github.com/stacksjs/reverse-proxy/commit/454dd58))
369-
- Wip ([b55ae41](https://github.com/stacksjs/reverse-proxy/commit/b55ae41))
370-
- Wip ([e2f1350](https://github.com/stacksjs/reverse-proxy/commit/e2f1350))
371-
- Wip ([61b2aa5](https://github.com/stacksjs/reverse-proxy/commit/61b2aa5))
372-
- Wip ([a80283b](https://github.com/stacksjs/reverse-proxy/commit/a80283b))
373-
- Wip ([97b9b16](https://github.com/stacksjs/reverse-proxy/commit/97b9b16))
374-
- Wip ([fc8d633](https://github.com/stacksjs/reverse-proxy/commit/fc8d633))
375-
- Wip ([e924f9c](https://github.com/stacksjs/reverse-proxy/commit/e924f9c))
376-
- Wip ([249e8fa](https://github.com/stacksjs/reverse-proxy/commit/249e8fa))
377-
- Wip ([1045bb1](https://github.com/stacksjs/reverse-proxy/commit/1045bb1))
378-
- Wip ([c0a8bad](https://github.com/stacksjs/reverse-proxy/commit/c0a8bad))
379-
- Wip ([f28b118](https://github.com/stacksjs/reverse-proxy/commit/f28b118))
380-
- Wip ([712a5bf](https://github.com/stacksjs/reverse-proxy/commit/712a5bf))
381-
- Wip ([2786e8d](https://github.com/stacksjs/reverse-proxy/commit/2786e8d))
382-
- Wip ([1290366](https://github.com/stacksjs/reverse-proxy/commit/1290366))
383-
- Wip ([030a8b7](https://github.com/stacksjs/reverse-proxy/commit/030a8b7))
384-
- Wip ([2116f75](https://github.com/stacksjs/reverse-proxy/commit/2116f75))
385-
- Wip ([5352f7a](https://github.com/stacksjs/reverse-proxy/commit/5352f7a))
386-
- Wip ([1b99dd0](https://github.com/stacksjs/reverse-proxy/commit/1b99dd0))
360+
- Initial commit ([beb4d96](https://github.com/stacksjs/rpx/commit/beb4d96))
361+
- Wip ([8e3fce6](https://github.com/stacksjs/rpx/commit/8e3fce6))
362+
- Wip ([bbb6783](https://github.com/stacksjs/rpx/commit/bbb6783))
363+
- Wip ([2cfb363](https://github.com/stacksjs/rpx/commit/2cfb363))
364+
- Wip ([6bb85ac](https://github.com/stacksjs/rpx/commit/6bb85ac))
365+
- Wip ([602c9e1](https://github.com/stacksjs/rpx/commit/602c9e1))
366+
- Wip ([5986105](https://github.com/stacksjs/rpx/commit/5986105))
367+
- Wip ([c2bfec6](https://github.com/stacksjs/rpx/commit/c2bfec6))
368+
- Wip ([454dd58](https://github.com/stacksjs/rpx/commit/454dd58))
369+
- Wip ([b55ae41](https://github.com/stacksjs/rpx/commit/b55ae41))
370+
- Wip ([e2f1350](https://github.com/stacksjs/rpx/commit/e2f1350))
371+
- Wip ([61b2aa5](https://github.com/stacksjs/rpx/commit/61b2aa5))
372+
- Wip ([a80283b](https://github.com/stacksjs/rpx/commit/a80283b))
373+
- Wip ([97b9b16](https://github.com/stacksjs/rpx/commit/97b9b16))
374+
- Wip ([fc8d633](https://github.com/stacksjs/rpx/commit/fc8d633))
375+
- Wip ([e924f9c](https://github.com/stacksjs/rpx/commit/e924f9c))
376+
- Wip ([249e8fa](https://github.com/stacksjs/rpx/commit/249e8fa))
377+
- Wip ([1045bb1](https://github.com/stacksjs/rpx/commit/1045bb1))
378+
- Wip ([c0a8bad](https://github.com/stacksjs/rpx/commit/c0a8bad))
379+
- Wip ([f28b118](https://github.com/stacksjs/rpx/commit/f28b118))
380+
- Wip ([712a5bf](https://github.com/stacksjs/rpx/commit/712a5bf))
381+
- Wip ([2786e8d](https://github.com/stacksjs/rpx/commit/2786e8d))
382+
- Wip ([1290366](https://github.com/stacksjs/rpx/commit/1290366))
383+
- Wip ([030a8b7](https://github.com/stacksjs/rpx/commit/030a8b7))
384+
- Wip ([2116f75](https://github.com/stacksjs/rpx/commit/2116f75))
385+
- Wip ([5352f7a](https://github.com/stacksjs/rpx/commit/5352f7a))
386+
- Wip ([1b99dd0](https://github.com/stacksjs/rpx/commit/1b99dd0))
387387

388388
### ❤️ Contributors
389389

390390
- Chris <chrisbreuer93@gmail.com>
391-

README.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,26 @@ export interface CleanupConfig {
5151
certs: boolean // clean up certificates, defaults to false
5252
}
5353

54-
export interface ReverseProxyConfig {
55-
from: string // domain to proxy from, defaults to localhost:3000
54+
export interface ProxyConfig {
55+
from: string // domain to proxy from, defaults to localhost:5173
5656
to: string // domain to proxy to, defaults to stacks.localhost
5757
cleanUrls?: boolean // removes the .html extension from URLs, defaults to false
5858
https: boolean | TlsConfig // automatically uses https, defaults to true, also redirects http to https
5959
cleanup?: boolean | CleanupConfig // automatically cleans up /etc/hosts, defaults to false
60+
start?: StartOptions
6061
verbose: boolean // log verbose output, defaults to false
6162
}
6263

63-
const config: ReverseProxyOptions = {
64-
from: 'localhost:3000',
64+
const config: ProxyOptions = {
65+
from: 'localhost:5173',
6566
to: 'my-docs.localhost',
6667
cleanUrls: true,
6768
https: true,
6869
cleanup: false,
70+
start: {
71+
command: 'bun run dev:docs',
72+
lazy: true,
73+
}
6974
}
7075

7176
startProxy(config)
@@ -74,12 +79,12 @@ startProxy(config)
7479
In case you are trying to start multiple proxies, you may use this configuration:
7580

7681
```ts
77-
// reverse-proxy.config.{ts,js}
78-
import type { ReverseProxyOptions } from '@stacksjs/rpx'
82+
// rpx.config.{ts,js}
83+
import type { ProxyOptions } from '@stacksjs/rpx'
7984
import os from 'node:os'
8085
import path from 'node:path'
8186

82-
const config: ReverseProxyOptions = {
87+
const config: ProxyOptions = {
8388
https: { // https: true -> also works with sensible defaults
8489
caCertPath: path.join(os.homedir(), '.stacks', 'ssl', `stacks.localhost.ca.crt`),
8590
certPath: path.join(os.homedir(), '.stacks', 'ssl', `stacks.localhost.crt`),
@@ -96,6 +101,13 @@ const config: ReverseProxyOptions = {
96101
from: 'localhost:5173',
97102
to: 'my-app.localhost',
98103
cleanUrls: true,
104+
start: {
105+
command: 'bun run dev',
106+
cwd: '/path/to/my-app',
107+
env: {
108+
NODE_ENV: 'development',
109+
},
110+
},
99111
},
100112
{
101113
from: 'localhost:5174',
@@ -120,15 +132,15 @@ rpx --version
120132

121133
## Configuration
122134

123-
The Reverse Proxy can be configured using a `reverse-proxy.config.ts` _(or `reverse-proxy.config.js`)_ file and it will be automatically loaded when running the `reverse-proxy` command.
135+
The Reverse Proxy can be configured using a `rpx.config.ts` _(or `rpx.config.js`)_ file and it will be automatically loaded when running the `reverse-proxy` command.
124136

125137
```ts
126-
// reverse-proxy.config.{ts,js}
127-
import type { ReverseProxyOptions } from '@stacksjs/rpx'
138+
// rpx.config.{ts,js}
139+
import type { ProxyOptions } from '@stacksjs/rpx'
128140
import os from 'node:os'
129141
import path from 'node:path'
130142

131-
const config: ReverseProxyOptions = {
143+
const config: ProxyOptions = {
132144
from: 'localhost:5173',
133145
to: 'stacks.localhost',
134146

bin/cli.ts

Lines changed: 113 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1+
import type { ProxyOption, StartOptions } from '../src/types'
2+
import process from 'node:process'
13
import { CAC } from 'cac'
24
import { version } from '../package.json'
35
import { config } from '../src/config'
4-
import { httpsConfig } from '../src/https'
6+
import { processManager } from '../src/process-manager'
57
import { startProxies, startProxy } from '../src/start'
8+
import { isMultiProxyConfig } from '../src/utils'
69

7-
const cli = new CAC('reverse-proxy')
8-
9-
interface ReverseProxyOption {
10-
from: string
11-
to: string
12-
keyPath: string
13-
certPath: string
14-
caCertPath: string
15-
cleanup: {
16-
certs: boolean
17-
hosts: boolean
18-
}
19-
verbose: boolean
10+
const cli = new CAC('rpx')
11+
12+
// Define CLI options interface to match our core types
13+
interface CLIOptions {
14+
from?: string
15+
to?: string
16+
keyPath?: string
17+
certPath?: string
18+
caCertPath?: string
19+
hostsCleanup?: boolean
20+
certsCleanup?: boolean
21+
startCommand?: string
22+
startCwd?: string
23+
startEnv?: string
24+
verbose?: boolean
2025
}
2126

2227
cli
@@ -28,26 +33,107 @@ cli
2833
.option('--ca-cert-path <path>', 'Absolute path to the SSL CA certificate')
2934
.option('--hosts-cleanup', 'Cleanup /etc/hosts on exit')
3035
.option('--certs-cleanup', 'Cleanup SSL certificates on exit')
36+
.option('--start-command <command>', 'Command to start the dev server')
37+
.option('--start-cwd <path>', 'Current working directory for the dev server')
38+
.option('--start-env <env>', 'Environment variables for the dev server')
3139
.option('--verbose', 'Enable verbose logging')
32-
.example('reverse-proxy start --from localhost:5173 --to my-project.localhost')
33-
.example('reverse-proxy start --from localhost:3000 --to my-project.localhost/api')
34-
.example('reverse-proxy start --from localhost:3000 --to localhost:3001')
35-
.example('reverse-proxy start --from localhost:5173 --to my-project.test --key-path /absolute/path/to/key --cert-path /absolute/path/to/cert')
36-
.action(async (options?: ReverseProxyOption) => {
40+
.example('rpx start --from localhost:5173 --to my-project.localhost')
41+
.example('rpx start --from localhost:3000 --to my-project.localhost/api')
42+
.example('rpx start --from localhost:3000 --to localhost:3001')
43+
.example('rpx start --from localhost:5173 --to my-project.test --key-path /absolute/path/to/key --cert-path /absolute/path/to/cert')
44+
.action(async (options?: CLIOptions) => {
3745
if (!options?.from || !options.to) {
3846
return startProxies(config)
3947
}
4048

41-
return startProxy({
42-
from: options?.from,
43-
to: options?.to,
44-
https: httpsConfig(options),
49+
// Convert CLI options to ProxyOption
50+
const proxyOptions: ProxyOption = {
51+
from: options.from,
52+
to: options.to,
53+
https: {
54+
keyPath: options.keyPath,
55+
certPath: options.certPath,
56+
caCertPath: options.caCertPath,
57+
},
4558
cleanup: {
46-
certs: options?.cleanup.certs,
47-
hosts: options?.cleanup.hosts,
59+
certs: options.certsCleanup || false,
60+
hosts: options.hostsCleanup || false,
4861
},
49-
verbose: options?.verbose,
50-
})
62+
verbose: options.verbose || false,
63+
}
64+
65+
// Add start options if provided
66+
if (options.startCommand) {
67+
const startOptions: StartOptions = {
68+
command: options.startCommand,
69+
}
70+
if (options.startCwd)
71+
startOptions.cwd = options.startCwd
72+
if (options.startEnv) {
73+
try {
74+
startOptions.env = JSON.parse(options.startEnv)
75+
}
76+
catch (err) {
77+
console.error('Failed to parse start-env JSON:', err)
78+
process.exit(1)
79+
}
80+
}
81+
proxyOptions.start = startOptions
82+
}
83+
84+
return startProxy(proxyOptions)
85+
})
86+
87+
cli
88+
.command('watch:start <proxy>', 'Start the dev server for a specific proxy')
89+
.option('--verbose', 'Enable verbose logging')
90+
.action(async (proxyId: string, options: { verbose?: boolean }) => {
91+
// Find the proxy configuration
92+
const proxyConfig = isMultiProxyConfig(config)
93+
? config.proxies.find(p => p.to === proxyId || `${p.from}-${p.to}` === proxyId)
94+
: config.to === proxyId ? config : null
95+
96+
if (!proxyConfig?.start) {
97+
console.error(`No watch configuration found for proxy: ${proxyId}`)
98+
process.exit(1)
99+
}
100+
101+
try {
102+
await processManager.startProcess(proxyId, proxyConfig.start, options.verbose)
103+
console.log(`Started dev server for ${proxyId}`)
104+
}
105+
catch (err) {
106+
console.error(`Failed to start dev server for ${proxyId}:`, err)
107+
process.exit(1)
108+
}
109+
})
110+
111+
cli
112+
.command('watch:stop <proxy>', 'Stop the dev server for a specific proxy')
113+
.option('--verbose', 'Enable verbose logging')
114+
.action(async (proxyId: string, options: { verbose?: boolean }) => {
115+
try {
116+
await processManager.stopProcess(proxyId, options.verbose)
117+
console.log(`Stopped dev server for ${proxyId}`)
118+
}
119+
catch (err) {
120+
console.error(`Failed to stop dev server for ${proxyId}:`, err)
121+
process.exit(1)
122+
}
123+
})
124+
125+
cli
126+
.command('watch:stopall', 'Stop all running dev servers')
127+
.option('--verbose', 'Enable verbose logging')
128+
.action(async (options: { verbose?: boolean }) => {
129+
try {
130+
await processManager.stopAll(options.verbose)
131+
console.log('Stopped all dev servers')
132+
}
133+
catch (err) {
134+
console.error('Failed to stop all dev servers:', err)
135+
process.exit(1)
136+
}
51137
})
52138

53139
cli.command('version', 'Show the version of the Reverse Proxy CLI').action(() => {

0 commit comments

Comments
 (0)