55} from "@t3tools/contracts" ;
66import { compareSemverVersions } from "@t3tools/shared/semver" ;
77import { resolveCommandPath } from "@t3tools/shared/shell" ;
8+ import { HostProcessPlatform } from "@t3tools/shared/hostProcess" ;
89import * as Config from "effect/Config" ;
910import * as Context from "effect/Context" ;
1011import * as DateTime from "effect/DateTime" ;
@@ -52,6 +53,7 @@ export interface ProviderMaintenanceCommandAction {
5253
5354export interface ProviderMaintenanceCapabilityResolutionOptions {
5455 readonly binaryPath ?: string | null ;
56+ readonly platform ?: NodeJS . Platform ;
5557 readonly env ?: NodeJS . ProcessEnv ;
5658 readonly resolvedCommandPath ?: string | null ;
5759 readonly realCommandPath ?: string | null ;
@@ -94,18 +96,34 @@ function nonEmptyString(value: unknown): string | null {
9496 return typeof value === "string" && value . trim ( ) . length > 0 ? value . trim ( ) : null ;
9597}
9698
99+ function quoteUpdateExecutable ( executable : string , platform : NodeJS . Platform ) : string {
100+ const safePath = platform === "win32" ? / ^ [ \w . / : \\ - ] + $ / : / ^ [ \w . / : - ] + $ / ;
101+ if ( safePath . test ( executable ) ) return executable ;
102+ // Windows terminals default to PowerShell, where a quoted executable needs &.
103+ return platform === "win32"
104+ ? `& '${ executable . replace ( / [ ' \u2018 \u2019 ] / g, "$&$&" ) } '`
105+ : `'${ executable . replaceAll ( "'" , "'\\''" ) } '` ;
106+ }
107+
97108export function makeProviderMaintenanceCapabilities ( input : {
98109 readonly provider : ProviderDriverKind ;
99110 readonly packageName : string | null ;
100111 readonly updateExecutable : string | null ;
101112 readonly updateArgs : ReadonlyArray < string > ;
102113 readonly updateLockKey : string | null ;
114+ readonly platform ?: NodeJS . Platform ;
103115} ) : ProviderMaintenanceCapabilities {
104116 const update =
105117 input . updateExecutable === null || input . updateLockKey === null
106118 ? null
107119 : {
108- command : [ input . updateExecutable , ...input . updateArgs ] . join ( " " ) ,
120+ command : [
121+ quoteUpdateExecutable (
122+ input . updateExecutable ,
123+ input . platform ?? HostProcessPlatform . defaultValue ( ) ,
124+ ) ,
125+ ...input . updateArgs ,
126+ ] . join ( " " ) ,
109127 executable : input . updateExecutable ,
110128 args : input . updateArgs ,
111129 lockKey : input . updateLockKey ,
@@ -210,6 +228,7 @@ function makeHomebrewProviderMaintenanceCapabilities(
210228function makeNativeProviderMaintenanceCapabilities (
211229 definition : PackageManagedProviderMaintenanceDefinition ,
212230 commandPath : string ,
231+ platform : NodeJS . Platform ,
213232) : ProviderMaintenanceCapabilities | null {
214233 if ( ! definition . nativeUpdate ) {
215234 return null ;
@@ -221,6 +240,7 @@ function makeNativeProviderMaintenanceCapabilities(
221240 updateExecutable : commandPath ,
222241 updateArgs : definition . nativeUpdate . args ,
223242 updateLockKey : definition . nativeUpdate . lockKey ,
243+ platform,
224244 } ) ;
225245}
226246
@@ -298,8 +318,11 @@ export function resolvePackageManagedProviderMaintenance(
298318 commandPaths . some ( ( commandPath ) => nativeUpdate . isCommandPath ( commandPath ) )
299319 ) {
300320 return (
301- makeNativeProviderMaintenanceCapabilities ( definition , resolvedCommandPath ) ??
302- makeNpmGlobalProviderMaintenanceCapabilities ( definition )
321+ makeNativeProviderMaintenanceCapabilities (
322+ definition ,
323+ resolvedCommandPath ,
324+ options ?. platform ?? HostProcessPlatform . defaultValue ( ) ,
325+ ) ?? makeNpmGlobalProviderMaintenanceCapabilities ( definition )
303326 ) ;
304327 }
305328 if ( commandPaths . some ( isVitePlusGlobalCommandPath ) ) {
@@ -361,8 +384,9 @@ export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
361384 options ?: Omit < ProviderMaintenanceCapabilityResolutionOptions , "realCommandPath" > ,
362385) {
363386 const binaryPath = nonEmptyString ( options ?. binaryPath ) ;
387+ const platform = options ?. platform ?? ( yield * HostProcessPlatform ) ;
364388 if ( ! binaryPath ) {
365- return resolver . resolve ( options ) ;
389+ return resolver . resolve ( { ... options , platform } ) ;
366390 }
367391
368392 const env = options ?. env ?? ( yield * readCommandLookupEnv ) ;
@@ -371,7 +395,7 @@ export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
371395 Effect . catchTag ( "CommandResolutionError" , ( ) => Effect . succeed ( null ) ) ,
372396 ) ) ?? ( hasPathSeparator ( binaryPath ) ? binaryPath : null ) ;
373397 if ( ! resolvedCommandPath ) {
374- return resolver . resolve ( options ) ;
398+ return resolver . resolve ( { ... options , platform } ) ;
375399 }
376400
377401 const fileSystem = yield * FileSystem . FileSystem ;
@@ -380,6 +404,7 @@ export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
380404 . pipe ( Effect . orElseSucceed ( ( ) => resolvedCommandPath ) ) ;
381405 return resolver . resolve ( {
382406 ...options ,
407+ platform,
383408 env,
384409 resolvedCommandPath,
385410 realCommandPath,
0 commit comments