Skip to content

Commit

Permalink
More helpful error message
Browse files Browse the repository at this point in the history
Add levenshtein helpfulness some day?
  • Loading branch information
mmkal committed May 29, 2024
1 parent c4581f9 commit 8a36dd8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ export const trpcCli = <R extends AnyRouter>({router, ...params}: TrpcCliParams<
const procedureInfo = command && procedureMap[command]

if (!procedureInfo) {
die('No command provided.')
const name = JSON.stringify(command || parsedArgv._[0])
const message = name ? `Command not found: ${name}.` : 'No command specified.'
return die(message)
}

if (Object.entries(unknownFlags).length > 0) {
Expand Down
34 changes: 34 additions & 0 deletions test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,40 @@ test('cli divide failure', async () => {
`)
})

test('cli non-existent command', async () => {
const output = await tsx('calculator', ['multiploo', '2', '3'])
expect(output).toMatchInlineSnapshot(`
"Command not found: "multiploo".
Commands:
add Add two numbers. Use this if you and your friend both have apples, and you want to know how many apples there are in total.
subtract Subtract two numbers. Useful if you have a number and you want to make it smaller.
multiply Multiply two numbers together. Useful if you want to count the number of tiles on your bathroom wall and are short on time.
divide Divide two numbers. Useful if you have a number and you want to make it smaller and \`subtract\` isn't quite powerful enough for you.
Flags:
-h, --help Show help
--verbose-errors Throw raw errors (by default errors are summarised)
"
`)
})

test('cli no command', async () => {
const output = await tsx('calculator', [])
expect(output).toMatchInlineSnapshot(`
"No command specified.
Commands:
add Add two numbers. Use this if you and your friend both have apples, and you want to know how many apples there are in total.
subtract Subtract two numbers. Useful if you have a number and you want to make it smaller.
multiply Multiply two numbers together. Useful if you want to count the number of tiles on your bathroom wall and are short on time.
divide Divide two numbers. Useful if you have a number and you want to make it smaller and \`subtract\` isn't quite powerful enough for you.
Flags:
-h, --help Show help
--verbose-errors Throw raw errors (by default errors are summarised)
"
`)
})

test('migrations help', async () => {
const output = await tsx('migrations', ['--help'])
expect(output).toMatchInlineSnapshot(`
Expand Down

0 comments on commit 8a36dd8

Please sign in to comment.