|
| 1 | +import process from 'node:process'; |
| 2 | +import { program } from 'commander'; |
1 | 3 | import Server from '.'; |
| 4 | +import { DEFAULT_PORT, DEFAULT_BASE_DIR } from './constants'; |
2 | 5 |
|
3 | | -const server = new Server(); |
| 6 | +const cliOptions = [ |
| 7 | + { |
| 8 | + option: '-p, --port <port>', |
| 9 | + description: 'The port to listen on', |
| 10 | + defaultValue: DEFAULT_PORT, |
| 11 | + usage: `htttp-server -p ${DEFAULT_PORT}`, |
| 12 | + }, |
| 13 | + { |
| 14 | + option: '-d, --directory <directory>', |
| 15 | + description: 'The directory to serve', |
| 16 | + defaultValue: DEFAULT_BASE_DIR, |
| 17 | + usage: `htttp-server -d ${DEFAULT_BASE_DIR}`, |
| 18 | + }, |
| 19 | +]; |
| 20 | + |
| 21 | +cliOptions.forEach(({ option, description, defaultValue }) => program.option(option, description, defaultValue.toString())); |
| 22 | +program.on('--help', () => { |
| 23 | + console.log('Examples:'); |
| 24 | + cliOptions |
| 25 | + .map(({ usage }) => usage) |
| 26 | + .forEach((usage) => console.log(`\x20\x20${usage}`)); |
| 27 | +}); |
| 28 | +program.parse(process.argv); |
| 29 | +const opts = program.opts(); |
| 30 | + |
| 31 | +const server = new Server({ |
| 32 | + port: opts.port, |
| 33 | + baseDir: opts.directory, |
| 34 | +}); |
4 | 35 | server.start(); |
0 commit comments