|
| 1 | +import type { ReverseProxyOption } from '../src/types' |
1 | 2 | import os from 'node:os' |
2 | | -import { log, CAC } from '@stacksjs/cli' |
| 3 | +import { CAC, log } from '@stacksjs/cli' |
3 | 4 | import { readFileSync, writeFileSync } from '@stacksjs/storage' |
4 | 5 | import { version } from '../package.json' |
5 | 6 | import { config } from '../src/config' |
6 | 7 | import { startProxy } from '../src/start' |
7 | | -import type { ReverseProxyOption } from '../src/types' |
8 | 8 |
|
9 | 9 | const cli = new CAC('reverse-proxy') |
10 | 10 |
|
11 | 11 | cli |
12 | 12 | .command('start', 'Start the Reverse Proxy Server') |
13 | | - .option('--from <from>', 'The URL to proxy from') |
14 | | - .option('--to <to>', 'The URL to proxy to') |
15 | | - .option('--keyPath <path>', 'Absolute path to the SSL key') |
16 | | - .option('--certPath <path>', 'Absolute path to the SSL certificate') |
17 | | - .option('--verbose', 'Enable verbose logging', { default: false }) |
18 | | - .example('reverse-proxy start --from localhost:3000 --to my-project.localhost') |
19 | | - .example('reverse-proxy start --from localhost:3001 --to my-project.localhost/api') |
| 13 | + .option('--from <from>', 'The URL to proxy from', { default: config.from }) |
| 14 | + .option('--to <to>', 'The URL to proxy to', { default: config.to }) |
| 15 | + .option('--key-path <path>', 'Absolute path to the SSL key', { default: config.keyPath }) |
| 16 | + .option('--cert-path <path>', 'Absolute path to the SSL certificate', { default: config.certPath }) |
| 17 | + .option('--verbose', 'Enable verbose logging', { default: config.verbose }) |
| 18 | + .example('reverse-proxy start --from localhost:5173 --to my-project.localhost') |
| 19 | + .example('reverse-proxy start --from localhost:3000 --to my-project.localhost/api') |
20 | 20 | .example('reverse-proxy start --from localhost:3000 --to localhost:3001') |
21 | | - .example( |
22 | | - 'reverse-proxy start --from localhost:3000 --to my-project.test --keyPath /absolute/path/to/key --certPath /absolute/path/to/cert', |
23 | | - ) |
| 21 | + .example('reverse-proxy start --from localhost:5173 --to my-project.test --key-path /absolute/path/to/key --cert-path /absolute/path/to/cert') |
24 | 22 | .action(async (options?: ReverseProxyOption) => { |
25 | | - if (options?.from || options?.to) { |
26 | | - startProxy({ |
27 | | - from: options?.from ?? 'localhost:3000', |
28 | | - to: options?.to ?? 'stacks.localhost', |
29 | | - keyPath: options?.keyPath, |
30 | | - certPath: options?.certPath, |
31 | | - }) |
32 | | - |
33 | | - return |
34 | | - } |
35 | | - |
36 | | - // loop over the config and start all the proxies |
37 | | - if (config) { |
38 | | - for (const [from, to] of Object.entries(config)) { |
39 | | - startProxy({ |
40 | | - from, |
41 | | - to, |
42 | | - keyPath: options?.keyPath, |
43 | | - certPath: options?.certPath, |
44 | | - }) |
45 | | - } |
46 | | - } else { |
47 | | - // eslint-disable-next-line no-console |
48 | | - console.log('No proxies found in the config') |
49 | | - } |
| 23 | + startProxy({ |
| 24 | + from: options?.from, |
| 25 | + to: options?.to, |
| 26 | + keyPath: options?.keyPath, |
| 27 | + certPath: options?.certPath, |
| 28 | + verbose: options?.verbose, |
| 29 | + }) |
50 | 30 | }) |
51 | 31 |
|
52 | 32 | cli |
|
82 | 62 | // If not, append it |
83 | 63 | currentHostsContent += `\n${entry}` |
84 | 64 | updated = true |
85 | | - } else { |
| 65 | + } |
| 66 | + else { |
86 | 67 | log.info(`Entry for ${host} already exists in the hosts file.`) |
87 | 68 | } |
88 | 69 | } |
89 | 70 |
|
90 | 71 | if (updated) { |
91 | 72 | writeFileSync(hostsFilePath, currentHostsContent, 'utf8') |
92 | 73 | log.success('Hosts file updated with latest proxy domains.') |
93 | | - } else { |
| 74 | + } |
| 75 | + else { |
94 | 76 | log.info('No new entries were added to the hosts file.') |
95 | 77 | } |
96 | | - } catch (error: unknown) { |
| 78 | + } |
| 79 | + catch (error: unknown) { |
97 | 80 | if ((error as NodeJS.ErrnoException).code === 'EACCES') |
98 | 81 | console.error('Permission denied. Please run this command with administrative privileges.') |
99 | 82 | else console.error(`An error occurred: ${(error as NodeJS.ErrnoException).message}`) |
100 | 83 | } |
101 | | - } else { |
102 | | - // eslint-disable-next-line no-console |
| 84 | + } |
| 85 | + else { |
103 | 86 | console.log('No proxies found. Is your config configured properly?') |
104 | 87 | } |
105 | 88 | }) |
106 | 89 |
|
107 | 90 | cli.command('version', 'Show the version of the Reverse Proxy CLI').action(() => { |
108 | | - // eslint-disable-next-line no-console |
109 | 91 | console.log(version) |
110 | 92 | }) |
111 | 93 |
|
|
0 commit comments