Skip to content

Commit 9a6bd30

Browse files
committed
fix(cli): validate range and print help if package is missing
1 parent 4cc4708 commit 9a6bd30

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/cli/npm-fetch-changelog.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import chalk from 'chalk'
44
import yargs from 'yargs/yargs'
55
import path from 'path'
66
import fs from 'fs-extra'
7+
import semver from 'semver'
78
import { fetchChangelog } from '../index'
89
import { debug } from '../util/debug'
910

1011
/* eslint-env node */
11-
const { argv } = yargs(process.argv.slice(2))
12+
const yargsInstance = yargs(process.argv.slice(2))
1213
.usage(
1314
`Usage: $0 <pkg>[@<range>]
1415
@@ -20,6 +21,10 @@ Prints changelog entries for an npm package from GitHub.
2021
alias: 'r',
2122
describe: 'semver version range to get changelog entries for',
2223
type: 'string',
24+
coerce: (value: string): string => {
25+
if (!semver.valid(value)) throw new Error(`invalid --range: ${value}`)
26+
return value
27+
},
2328
},
2429
json: {
2530
describe: 'output json',
@@ -43,6 +48,7 @@ Prints changelog entries for an npm package from GitHub.
4348
.help()
4449

4550
async function go() {
51+
const { argv } = yargsInstance
4652
let {
4753
_: [pkg],
4854
range,
@@ -51,7 +57,10 @@ async function go() {
5157
debug('process.version:', process.version)
5258
debug('process.execPath:', process.execPath)
5359
debug(argv)
54-
if (!pkg) process.exit(1)
60+
if (!pkg) {
61+
yargsInstance.showHelp()
62+
process.exit(1)
63+
}
5564

5665
const filters = [
5766
...(argv.major ? ['major'] : []),

0 commit comments

Comments
 (0)