Skip to content

Commit

Permalink
feat: hint when command not found
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Dec 27, 2023
1 parent e5cd507 commit b86a9ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/itchy-snakes-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"barnard59": patch
---

Suggest package to install when a CLI command is not found
19 changes: 18 additions & 1 deletion packages/cli/lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { program } from 'commander'
import isInstalledGlobally from 'is-installed-globally'
import runAction from './cli/runAction.js'
import * as monitoringOptions from './cli/monitoringOptions.js'
import * as commonOptions from './cli/commonOptions.js'
Expand Down Expand Up @@ -42,5 +43,21 @@ export default async function () {
.addOption(commonOptions.verbose)
.addOption(commonOptions.quiet)

await program.parseAsync(process.argv)
program.exitOverride()

try {
await program.parseAsync(process.argv)
} catch (error) {
const { groups } = /unknown command '(?<command>[^']+)'/.exec(error.message) || {}
if (groups && groups.command) {
/* eslint-disable no-console */
if (isInstalledGlobally) {
console.error(`Try running 'npm install (-g) barnard59-${groups.command}'`)
}

console.error(`Try running 'npm install barnard59-${groups.command}'`)
}

process.exit(1)
}
}

0 comments on commit b86a9ab

Please sign in to comment.