Skip to content

Commit 953a322

Browse files
committed
refactor(cli): rename clashing runtime detector -> cli runtime detector
1 parent ee5d329 commit 953a322

File tree

5 files changed

+112
-101
lines changed

5 files changed

+112
-101
lines changed

genkit-tools/cli/src/commands/ui-start.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import fs from 'fs/promises';
3131
import getPort, { makeRange } from 'get-port';
3232
import open from 'open';
3333
import path from 'path';
34-
import { detectRuntime } from '../utils/runtime-detector';
34+
import { detectCLIRuntime } from '../utils/runtime-detector';
3535
import {
3636
buildServerHarnessSpawnConfig,
3737
validateExecutablePath,
@@ -133,15 +133,17 @@ async function startAndWaitUntilHealthy(
133133
serversDir: string
134134
): Promise<ChildProcess> {
135135
// Detect runtime environment
136-
const runtime = detectRuntime();
137-
logger.debug(`Detected runtime: ${runtime.type} at ${runtime.execPath}`);
138-
if (runtime.scriptPath) {
139-
logger.debug(`Script path: ${runtime.scriptPath}`);
136+
const cliRuntime = detectCLIRuntime();
137+
logger.debug(
138+
`Detected CLI runtime: ${cliRuntime.type} at ${cliRuntime.execPath}`
139+
);
140+
if (cliRuntime.scriptPath) {
141+
logger.debug(`Script path: ${cliRuntime.scriptPath}`);
140142
}
141143

142144
// Build spawn configuration
143145
const logPath = path.join(serversDir, 'devui.log');
144-
const spawnConfig = buildServerHarnessSpawnConfig(runtime, port, logPath);
146+
const spawnConfig = buildServerHarnessSpawnConfig(cliRuntime, port, logPath);
145147

146148
// Validate executable path
147149
const isExecutable = await validateExecutablePath(spawnConfig.command);

genkit-tools/cli/src/utils/runtime-detector.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ const BUN_PATTERNS = ['bun'];
2727
const SCRIPT_EXTENSIONS = ['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx'];
2828

2929
/**
30-
* Runtime types supported by the detector
30+
* CLI runtime types supported by the detector
3131
*/
32-
export type RuntimeType = 'node' | 'bun' | 'compiled-binary';
32+
export type CLIRuntimeType = 'node' | 'bun' | 'compiled-binary';
3333

3434
/**
35-
* Information about the runtime environment
35+
* Information about the CLI runtime environment
3636
*/
37-
export interface RuntimeInfo {
38-
/** Type of runtime or execution mode */
39-
type: RuntimeType;
37+
export interface CLIRuntimeInfo {
38+
/** Type of CLI runtime or execution mode */
39+
type: CLIRuntimeType;
4040
/** Path to the executable (node, bun, or the compiled binary itself) */
4141
execPath: string;
4242
/** Path to the script being executed (undefined for compiled binaries) */
@@ -85,18 +85,18 @@ function matchesPatterns(execName: string, patterns: string[]): boolean {
8585
}
8686

8787
/**
88-
* Detects the current runtime environment and execution context.
88+
* Detects the current CLI runtime environment and execution context.
8989
* This helps determine how to properly spawn child processes.
9090
*
91-
* @returns Runtime information including type, paths, and platform
92-
* @throws Error if unable to determine executable path
91+
* @returns CLI runtime information including type, paths, and platform
92+
* @throws Error if unable to determine CLI runtime executable path
9393
*/
94-
export function detectRuntime(): RuntimeInfo {
94+
export function detectCLIRuntime(): CLIRuntimeInfo {
9595
const platform = process.platform;
9696
const execPath = process.execPath;
9797

9898
if (!execPath || execPath.trim() === '') {
99-
throw new Error('Unable to determine executable path');
99+
throw new Error('Unable to determine CLI runtime executable path');
100100
}
101101

102102
const argv0 = process.argv[0];
@@ -116,7 +116,7 @@ export function detectRuntime(): RuntimeInfo {
116116
const hasScriptArg = !!argv1;
117117
const scriptExists = hasScriptArg && safeExistsSync(argv1);
118118

119-
let type: RuntimeType;
119+
let type: CLIRuntimeType;
120120
let scriptPath: string | undefined;
121121
let isCompiledBinary: boolean;
122122

genkit-tools/cli/src/utils/spawn-config.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { type SpawnOptions } from 'child_process';
1818
import { access, constants } from 'fs/promises';
1919
import { SERVER_HARNESS_COMMAND } from '../commands/server-harness';
20-
import { type RuntimeInfo } from './runtime-detector';
20+
import { type CLIRuntimeInfo } from './runtime-detector';
2121

2222
/**
2323
* Configuration for spawning a child process
@@ -60,30 +60,30 @@ function isValidPort(port: number): boolean {
6060
/**
6161
* Builds spawn configuration for the server harness based on runtime info
6262
*
63-
* @param runtime - Runtime information from detector
63+
* @param cliRuntime - CLI runtime information from detector
6464
* @param port - Port number for the server (must be valid port 0-65535)
6565
* @param logPath - Path to the log file
6666
* @returns Spawn configuration for child_process.spawn
6767
* @throws Error if port is invalid or runtime info is missing required fields
6868
*
6969
* @example
7070
* ```typescript
71-
* const runtime = detectRuntime();
72-
* const config = buildServerHarnessSpawnConfig(runtime, 4000, '/path/to/log');
71+
* const cliRuntime = detectRuntime();
72+
* const config = buildServerHarnessSpawnConfig(cliRuntime, 4000, '/path/to/log');
7373
* const child = spawn(config.command, config.args, config.options);
7474
* ```
7575
*/
7676
export function buildServerHarnessSpawnConfig(
77-
runtime: RuntimeInfo,
77+
cliRuntime: CLIRuntimeInfo,
7878
port: number,
7979
logPath: string
8080
): SpawnConfig {
8181
// Validate inputs
82-
if (!runtime) {
83-
throw new Error('Runtime info is required');
82+
if (!cliRuntime) {
83+
throw new Error('CLI runtime info is required');
8484
}
85-
if (!runtime.execPath) {
86-
throw new Error('Runtime execPath is required');
85+
if (!cliRuntime.execPath) {
86+
throw new Error('CLI runtime execPath is required');
8787
}
8888
if (!isValidPort(port)) {
8989
throw new Error(
@@ -94,16 +94,21 @@ export function buildServerHarnessSpawnConfig(
9494
throw new Error('Log path is required');
9595
}
9696

97-
let command = runtime.execPath;
97+
let command = cliRuntime.execPath;
9898
let args: string[];
9999

100-
if (runtime.type === 'compiled-binary') {
100+
if (cliRuntime.type === 'compiled-binary') {
101101
// For compiled binaries, execute directly with arguments
102102
args = [SERVER_HARNESS_COMMAND, port.toString(), logPath];
103103
} else {
104104
// For interpreted runtimes (Node.js, Bun), include script path if available
105-
args = runtime.scriptPath
106-
? [runtime.scriptPath, SERVER_HARNESS_COMMAND, port.toString(), logPath]
105+
args = cliRuntime.scriptPath
106+
? [
107+
cliRuntime.scriptPath,
108+
SERVER_HARNESS_COMMAND,
109+
port.toString(),
110+
logPath,
111+
]
107112
: [SERVER_HARNESS_COMMAND, port.toString(), logPath];
108113
}
109114

@@ -112,11 +117,11 @@ export function buildServerHarnessSpawnConfig(
112117
stdio: ['ignore', 'ignore', 'ignore'] as const,
113118
detached: false,
114119
// Use shell on Windows for better compatibility with paths containing spaces
115-
shell: runtime.platform === 'win32',
120+
shell: cliRuntime.platform === 'win32',
116121
};
117122

118123
// Handles spaces in the command and arguments on Windows
119-
if (runtime.platform === 'win32') {
124+
if (cliRuntime.platform === 'win32') {
120125
command = `"${command}"`;
121126
args = args.map((arg) => `"${arg}"`);
122127
}

0 commit comments

Comments
 (0)