Files
jackwener__opencli/src/runtime-detect.ts
T
AlexYue 6d1fb6d219 feat(runtime): add Bun runtime compatibility (#459)
* feat(runtime): add runtime detection utility for Bun/Node.js

Add runtime-detect.ts module that detects whether opencli is running
under Bun or Node.js via globalThis.Bun check. Includes helper
functions for version string and label formatting.

Add corresponding unit tests that work correctly under both runtimes.

* feat(runtime): integrate Bun runtime support into CLI tooling

- doctor: show runtime label (e.g. 'node v22.13.0') in diagnostic output
- package.json: add dev:bun, start:bun, test:bun convenience scripts
- E2E helpers: support OPENCLI_TEST_RUNTIME env var for runtime selection

* ci: add Bun compatibility test job and document runtime support

- ci.yml: add bun-test job using oven-sh/setup-bun@v2
- README.md: update Prerequisites to mention Bun, add Runtime Support
  section with usage examples for dev:bun, start:bun, test:bun

* ci: pin Bun version in compatibility job

---------

Co-authored-by: jackwener <jakevingoo@gmail.com>
2026-03-26 19:10:31 +08:00

37 lines
1.1 KiB
TypeScript

/**
* Runtime detection — identify whether opencli is running under Node.js or Bun.
*
* Bun injects `globalThis.Bun` at startup, making detection trivial.
* This module centralises the check so other code can adapt behaviour
* (e.g. logging, diagnostics) without littering runtime sniffing everywhere.
*/
export type Runtime = 'bun' | 'node';
/**
* Detect the current JavaScript runtime.
*/
export function detectRuntime(): Runtime {
// Bun always exposes globalThis.Bun (including Bun.version)
if (typeof (globalThis as any).Bun !== 'undefined') return 'bun';
return 'node';
}
/**
* Return a human-readable version string for the current runtime.
* Examples: "v22.13.0" (Node), "1.1.42" (Bun)
*/
export function getRuntimeVersion(): string {
if (detectRuntime() === 'bun') {
return (globalThis as any).Bun.version as string;
}
return process.version; // e.g. "v22.13.0"
}
/**
* Return a combined label like "node v22.13.0" or "bun 1.1.42".
*/
export function getRuntimeLabel(): string {
return `${detectRuntime()} ${getRuntimeVersion()}`;
}