Skip to content

Commit a4ef6e3

Browse files
committed
feat: use commander to catch cli input
1 parent 83b1c3d commit a4ef6e3

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
"build": "tsup",
3232
"build:dev": "tsup --watch",
3333
"dev": "tsx watch --clear-screen=false src/index.ts",
34+
"dev:cli": "tsx watch --clear-screen=false src/cli.ts",
3435
"release": "bumpp",
3536
"release:publish": "pnpm release && pnpm publish"
3637
},
3738
"dependencies": {
38-
"chalk": "^5.3.0"
39+
"chalk": "^5.3.0",
40+
"commander": "^12.1.0"
3941
},
4042
"devDependencies": {
4143
"@dhzh/eslint-config": "^0.15.1",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
1+
import process from 'node:process';
2+
import { program } from 'commander';
13
import Server from '.';
4+
import { DEFAULT_PORT, DEFAULT_BASE_DIR } from './constants';
25

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+
});
435
server.start();

src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import process from 'node:process';
2+
3+
export const DEFAULT_PORT = 8080;
4+
export const DEFAULT_BASE_DIR = process.cwd();

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import process from 'node:process';
21
import * as http from 'node:http';
32
import * as os from 'node:os';
43
import * as fs from 'node:fs/promises';
54
import * as path from 'node:path';
65
import chalk from 'chalk';
6+
import { DEFAULT_PORT, DEFAULT_BASE_DIR } from './constants';
77
import type { ServerOptions } from './types';
88

99
export default class Server {
10-
port: number = 8080;
11-
baseDir: string = process.cwd();
10+
port: number = DEFAULT_PORT;
11+
baseDir: string = DEFAULT_BASE_DIR;
1212

1313
constructor(options?: ServerOptions) {
1414
if (options?.port) {
@@ -35,7 +35,7 @@ export default class Server {
3535
}
3636
});
3737
server.listen(this.port, () => {
38-
console.log(chalk.yellow('Server is running on:'));
38+
console.log(chalk.yellow('Server is running on:'), ` ${this.baseDir}`);
3939
this
4040
.getOsHosts()
4141
.forEach((host) => console.log(`\x20\x20${host}`));

0 commit comments

Comments
 (0)