-
Notifications
You must be signed in to change notification settings - Fork 623
fix(cli): better runtime handling for ui:start #3340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de5cdf0
feat: add runtime detector
cabljac 277c562
feat: add spawn config
cabljac 2e8e0d5
chore: format
cabljac a300b12
refactor: address review comments on best practices
cabljac ee5d329
fix(cli): fix ui:start on Windows (and machines with spaces in path) …
CorieW 953a322
refactor(cli): rename clashing runtime detector -> cli runtime detector
cabljac 97d3363
test: add tests for ui:start command
cabljac 74de57e
chore(cli): update license headers
cabljac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /** | ||
| * Copyright 2024 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { existsSync } from 'fs'; | ||
| import { basename, extname } from 'path'; | ||
|
|
||
| const RUNTIME_NODE = 'node'; | ||
| const RUNTIME_BUN = 'bun'; | ||
| const RUNTIME_COMPILED = 'compiled-binary'; | ||
|
|
||
| const NODE_PATTERNS = ['node', 'nodejs']; | ||
| const BUN_PATTERNS = ['bun']; | ||
|
|
||
| const SCRIPT_EXTENSIONS = ['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx']; | ||
|
|
||
| /** | ||
| * CLI runtime types supported by the detector | ||
| */ | ||
| export type CLIRuntimeType = 'node' | 'bun' | 'compiled-binary'; | ||
|
|
||
| /** | ||
| * Information about the CLI runtime environment | ||
| */ | ||
| export interface CLIRuntimeInfo { | ||
| /** Type of CLI runtime or execution mode */ | ||
| type: CLIRuntimeType; | ||
| /** Path to the executable (node, bun, or the compiled binary itself) */ | ||
| execPath: string; | ||
| /** Path to the script being executed (undefined for compiled binaries) */ | ||
| scriptPath?: string; | ||
| /** Whether this is a compiled binary (e.g., Bun-compiled) */ | ||
| isCompiledBinary: boolean; | ||
| /** Platform information */ | ||
| platform: NodeJS.Platform; | ||
| } | ||
|
|
||
| /** | ||
| * Safely checks if a file exists without throwing errors | ||
| * @param path - File path to check | ||
| * @returns true if the file exists, false otherwise | ||
| */ | ||
| function safeExistsSync(path: string | undefined): boolean { | ||
| if (!path) return false; | ||
| try { | ||
| return existsSync(path); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the given path has a recognized script file extension | ||
| * @param path - File path to check | ||
| * @returns true if the path ends with a known script extension | ||
| * @internal Kept for potential future use, though not currently used in detection logic | ||
| */ | ||
| function isLikelyScriptFile(path: string | undefined): boolean { | ||
| if (!path) return false; | ||
| const ext = extname(path).toLowerCase(); | ||
| return SCRIPT_EXTENSIONS.includes(ext); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if executable name contains any of the given patterns | ||
| * @param execName - Name of the executable | ||
| * @param patterns - Array of patterns to match against | ||
| * @returns true if any pattern is found in the executable name | ||
| */ | ||
| function matchesPatterns(execName: string, patterns: string[]): boolean { | ||
| const lowerExecName = execName.toLowerCase(); | ||
| return patterns.some((pattern) => lowerExecName.includes(pattern)); | ||
| } | ||
|
|
||
| /** | ||
| * Detects the current CLI runtime environment and execution context. | ||
| * This helps determine how to properly spawn child processes. | ||
| * | ||
| * @returns CLI runtime information including type, paths, and platform | ||
| * @throws Error if unable to determine CLI runtime executable path | ||
| */ | ||
| export function detectCLIRuntime(): CLIRuntimeInfo { | ||
| const platform = process.platform; | ||
| const execPath = process.execPath; | ||
|
|
||
| if (!execPath || execPath.trim() === '') { | ||
| throw new Error('Unable to determine CLI runtime executable path'); | ||
| } | ||
|
|
||
| const argv0 = process.argv[0]; | ||
| const argv1 = process.argv[1]; | ||
|
|
||
| const execBasename = basename(execPath); | ||
| const argv0Basename = argv0 ? basename(argv0) : ''; | ||
|
|
||
| const hasBunVersion = 'bun' in (process.versions || {}); | ||
| const hasNodeVersion = 'node' in (process.versions || {}); | ||
|
|
||
| const execMatchesBun = matchesPatterns(execBasename, BUN_PATTERNS); | ||
| const execMatchesNode = matchesPatterns(execBasename, NODE_PATTERNS); | ||
| const argv0MatchesBun = matchesPatterns(argv0Basename, BUN_PATTERNS); | ||
| const argv0MatchesNode = matchesPatterns(argv0Basename, NODE_PATTERNS); | ||
|
|
||
| const hasScriptArg = !!argv1; | ||
| const scriptExists = hasScriptArg && safeExistsSync(argv1); | ||
|
|
||
| let type: CLIRuntimeType; | ||
| let scriptPath: string | undefined; | ||
| let isCompiledBinary: boolean; | ||
|
|
||
| // Determine runtime type based on most reliable indicators | ||
| if (hasBunVersion || execMatchesBun || argv0MatchesBun) { | ||
| // Check if this is a Bun-compiled binary | ||
| // Bun compiled binaries have virtual paths like /$bunfs/root/... | ||
| if ( | ||
| argv1 && | ||
| (argv1.startsWith('/$bunfs/') || /^[A-Za-z]:[\\/]+~BUN[\\/]+/.test(argv1)) | ||
| ) { | ||
| // This is a Bun-compiled binary | ||
| type = RUNTIME_COMPILED; | ||
| scriptPath = undefined; | ||
| isCompiledBinary = true; | ||
| } else { | ||
| // Regular Bun runtime | ||
| type = RUNTIME_BUN; | ||
| scriptPath = argv1; | ||
| isCompiledBinary = false; | ||
| } | ||
| } else if (hasNodeVersion || execMatchesNode || argv0MatchesNode) { | ||
| // Definitely Node.js | ||
| type = RUNTIME_NODE; | ||
| scriptPath = argv1; | ||
| isCompiledBinary = false; | ||
| } else if (!hasScriptArg || !scriptExists) { | ||
| // No script argument or script doesn't exist - likely compiled binary | ||
| type = RUNTIME_COMPILED; | ||
| scriptPath = undefined; | ||
| isCompiledBinary = true; | ||
| } else { | ||
| // Have a script argument that exists but unknown runtime | ||
| // This handles cases like custom Node.js builds with unusual names | ||
| type = RUNTIME_NODE; | ||
| scriptPath = argv1; | ||
| isCompiledBinary = false; | ||
| } | ||
|
|
||
| return { | ||
| type, | ||
| execPath, | ||
| scriptPath, | ||
| isCompiledBinary, | ||
| platform, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.