| 19 | import { createRequire } from "node:module"; |
| 20 | |
| 21 | function getVersion(): string { |
| 22 | const req = createRequire(import.meta.url); |
| 23 | // In source layout (src/index.ts) package.json is two levels up. |
| 24 | // In published layout (dist/index.js) it's one level up. Try both so |
| 25 | // `--version` works regardless of how the binary was launched. |
| 26 | for (const relPath of ["../../package.json", "../package.json"]) { |
| 27 | try { |
| 28 | const pkg = req(relPath) as { version?: unknown }; |
| 29 | if (typeof pkg.version === "string" && pkg.version.length > 0) { |
| 30 | return pkg.version; |
| 31 | } |
| 32 | } catch { |
| 33 | // try next layout |
| 34 | } |
| 35 | } |
| 36 | return "0.0.0"; |
| 37 | } |
| 38 | |
| 39 | function valueAfter(args: string[], flag: string): string | null { |
| 40 | const index = args.indexOf(flag); |