Skip to content

Commit f10d826

Browse files
authored
Merge pull request #57 from chatbotkit/next
Next
2 parents c579b9a + 0f68932 commit f10d826

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

.changeset/mean-bees-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chatbotkit/cli': patch
3+
---
4+
5+
Correct handling of help command line flags when used with scripts.

packages/cli/src/command/run/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ export const command = new Command()
9797
.description('Run a JavaScript file with @chatbotkit/* packages available')
9898
.argument('<script>', 'Path to the JavaScript file to run')
9999
.argument('[args...]', 'Arguments to pass to the script')
100+
.helpOption(false) // Disable built-in help so it passes through to script
101+
.enablePositionalOptions() // Stop parsing options after first positional arg
102+
.passThroughOptions() // Options after <script> pass through to the script
100103
.allowUnknownOption(true)
101104
.allowExcessArguments(true)
102-
.helpOption('-h, --help', 'Display help for the run command')
103105
.addHelpText(
104106
'after',
105107
`

packages/cli/src/index.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,44 @@ import run from './command/run/index.js'
66
import solution from './command/solution/index.js'
77

88
import { Argument, Command, Option } from 'commander'
9+
import { readFileSync } from 'fs'
10+
import { dirname, join } from 'path'
11+
import { fileURLToPath } from 'url'
12+
13+
const __dirname = dirname(fileURLToPath(import.meta.url))
14+
15+
/**
16+
* Get the CLI version from package.json.
17+
*/
18+
function getVersion() {
19+
try {
20+
// Try source structure first (src/)
21+
22+
let pkgPath = join(__dirname, '..', 'package.json')
23+
let pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
24+
25+
if (pkg.version) {
26+
return pkg.version
27+
}
28+
} catch {
29+
// Ignore
30+
}
31+
32+
try {
33+
// Try dist structure (dist/esm/)
34+
35+
let pkgPath = join(__dirname, '..', '..', 'package.json')
36+
let pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
37+
38+
if (pkg.version) {
39+
return pkg.version
40+
}
41+
} catch {
42+
// Ignore
43+
}
44+
45+
return 'unknown'
46+
}
947

1048
// Re-export commander utilities for scripts to build their own CLIs
1149

@@ -41,7 +79,10 @@ Y88b d88P 888 d88P 888 Y88b
4179
export default async function cbk(argv = process.argv) {
4280
const program = new Command()
4381

44-
program.name('cbk').description('Command line tools for ChatBotKit')
82+
program
83+
.name('cbk')
84+
.description('Command line tools for ChatBotKit')
85+
.version(getVersion(), '-v, --version', 'Display the CLI version')
4586

4687
if (process.stdout.isTTY) {
4788
program.hook('preAction', () => {
@@ -55,5 +96,21 @@ export default async function cbk(argv = process.argv) {
5596
program.addCommand(run)
5697
program.addCommand(solution)
5798

99+
// Special handling for `cbk run --help` or `cbk run -h` without a script
100+
// Since run command has helpOption(false) to pass --help to scripts,
101+
// we need to manually show help when no script is provided
102+
103+
const args = argv.slice(2)
104+
105+
if (
106+
args[0] === 'run' &&
107+
args.length === 2 &&
108+
(args[1] === '--help' || args[1] === '-h')
109+
) {
110+
run.outputHelp()
111+
112+
return
113+
}
114+
58115
program.parse(argv)
59116
}

0 commit comments

Comments
 (0)