@@ -6,6 +6,44 @@ import run from './command/run/index.js'
66import solution from './command/solution/index.js'
77
88import { 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
4179export 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