Skip to content

Commit 9e864c3

Browse files
committed
fix: handle unknown options in cli
1 parent 3d2998e commit 9e864c3

File tree

1 file changed

+84
-56
lines changed

1 file changed

+84
-56
lines changed

src/bin.ts

Lines changed: 84 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,72 +14,100 @@ import { createApp } from './app.js'
1414
import { Observer } from './observer.js'
1515
import { Data } from './service.js'
1616

17-
// Parse args
18-
const { values, positionals } = parseArgs({
19-
args: process.argv.slice(2),
20-
options: {
21-
port: {
22-
type: 'string',
23-
short: 'p',
24-
},
25-
host: {
26-
type: 'string',
27-
short: 'h',
28-
},
29-
static: {
30-
type: 'string',
31-
short: 's',
32-
multiple: true,
33-
},
34-
help: {
35-
type: 'boolean',
36-
},
37-
version: {
38-
type: 'boolean',
39-
},
40-
// Deprecated
41-
watch: {
42-
type: 'boolean',
43-
short: 'w',
44-
},
45-
},
46-
allowPositionals: true,
47-
})
48-
49-
// --help
50-
if (values.help || positionals.length === 0) {
17+
function help() {
5118
console.log(`Usage: json-server [options] <file>
5219
Options:
5320
-p, --port <port> Port (default: 3000)
5421
-h, --host <host> Host (default: localhost)
5522
-s, --static <dir> Static files directory (multiple allowed)
56-
--help Show this message
23+
--help Show this message
24+
--version Show version number
5725
`)
58-
process.exit()
5926
}
6027

61-
// --version
62-
if (values.version) {
63-
const pkg = JSON.parse(
64-
readFileSync(join(__dirname, '../package.json'), 'utf8'),
65-
) as PackageJson
66-
console.log(pkg.version)
67-
process.exit()
68-
}
28+
// Parse args
29+
function args(): {
30+
file: string
31+
port: number
32+
host: string
33+
static: string[]
34+
} {
35+
try {
36+
const { values, positionals } = parseArgs({
37+
options: {
38+
port: {
39+
type: 'string',
40+
short: 'p',
41+
default: process.env['PORT'] ?? '3000',
42+
},
43+
host: {
44+
type: 'string',
45+
short: 'h',
46+
default: process.env['HOST'] ?? 'localhost',
47+
},
48+
static: {
49+
type: 'string',
50+
short: 's',
51+
multiple: true,
52+
default: [],
53+
},
54+
help: {
55+
type: 'boolean',
56+
},
57+
version: {
58+
type: 'boolean',
59+
},
60+
// Deprecated
61+
watch: {
62+
type: 'boolean',
63+
short: 'w',
64+
},
65+
},
66+
allowPositionals: true,
67+
})
68+
69+
// --version
70+
if (values.version) {
71+
const pkg = JSON.parse(
72+
readFileSync(join(__dirname, '../package.json'), 'utf8'),
73+
) as PackageJson
74+
console.log(pkg.version)
75+
process.exit()
76+
}
6977

70-
// Handle --watch
71-
if (values.watch) {
72-
console.log(
73-
chalk.yellow(
74-
'--watch/-w can be omitted, JSON Server 1+ watches for file changes by default',
75-
),
76-
)
78+
// Handle --watch
79+
if (values.watch) {
80+
console.log(
81+
chalk.yellow(
82+
'--watch/-w can be omitted, JSON Server 1+ watches for file changes by default',
83+
),
84+
)
85+
}
86+
87+
if (values.help || positionals.length === 0) {
88+
help()
89+
process.exit()
90+
}
91+
92+
// App args and options
93+
return {
94+
file: positionals[0] ?? '',
95+
port: parseInt(values.port as string),
96+
host: values.host as string,
97+
static: values.static as string[],
98+
}
99+
} catch (e) {
100+
if ((e as NodeJS.ErrnoException).code === 'ERR_PARSE_ARGS_UNKNOWN_OPTION') {
101+
console.log(chalk.red((e as NodeJS.ErrnoException).message.split('.')[0]))
102+
help()
103+
process.exit(1)
104+
} else {
105+
throw e
106+
}
107+
}
77108
}
78109

79-
// App args and options
80-
const file = positionals[0] ?? ''
81-
const port = parseInt(values.port ?? process.env['PORT'] ?? '3000')
82-
const host = values.host ?? process.env['HOST'] ?? 'localhost'
110+
const { file, port, host, static: staticArr } = args()
83111

84112
if (!existsSync(file)) {
85113
console.log(chalk.red(`File ${file} not found`))
@@ -102,7 +130,7 @@ const db = new Low<Data>(observer, {})
102130
await db.read()
103131

104132
// Create app
105-
const app = createApp(db, { logger: false, static: values.static })
133+
const app = createApp(db, { logger: false, static: staticArr })
106134

107135
function logRoutes(data: Data) {
108136
console.log(

0 commit comments

Comments
 (0)