import { platform } from 'os' ; import isWsl from 'is-wsl' ; import { ExecutableOptions , ShellOptions , Shell , Converters , Converter } from 'child-shell' ; export enum PSExecutableType { PowerShellWin = 'powershell' , PowerShellCore = 'pwsh' , PowerShellCorePreview = 'pwsh-preview' } export type PSExecutableOptions = ExecutableOptions & Partial < { '-PSConsoleFile' : string ; '-Version' : '2.0' | '3.0' ; '-NoLogo' : boolean ; '-NoExit' : boolean ; '-Sta' : boolean ; '-Mta' : boolean ; '-NoProfile' : boolean ; '-NonInteractive' : boolean ; '-InputFormat' : 'Text' | 'XML' ; '-OutputFormat' : 'Text' | 'XML' ; '-WindowStyle' : string ; '-ConfigurationName' : string ; '-ExecutionPolicy' : 'AllSigned' | 'Bypass' | 'Default' | 'RemoteSigned' | 'Restricted' | 'Undefined' | 'Unrestricted' | string ; } > ; export type PowerShellOptions = ShellOptions & { pwsh ? : boolean ; pwshPrev ? : boolean ; executable ? : PSExecutableType | string ; executableOptions ? : PSExecutableOptions ; } ; const isWin = platform ( ) === 'win32' || isWsl ; const nullConverter = ( ) => '$Null' ; const booleanConverter = ( object ) => ( ( object as boolean ) ? '$True' : '$False' ) ; const objectConverter = ( object ) => `@${ JSON . stringify ( object ) . replace ( /:/g , '=' ) . replace ( /,/g , ';' ) }` ; const dateConverter = ( object ) => ( object as Date ) . toLocaleString ( ) ; const PS_CONVERTERS = new Map ( [ [ 'null' , nullConverter ] , [ 'boolean' , booleanConverter ] , [ 'object' , objectConverter ] , [ 'date' , dateConverter ] , ] ) ; export class PowerShell extends Shell { static converters = new Map ( [ ... Shell . converters , ... PS_CONVERTERS ] ) ; constructor ( options = { } ) { super ( { ... options , executableOptions : { '-NoLogo' : true , ... options . executableOptions , '-NoExit' : true , '-Command' : '-' , } , } ) ; } protected setExecutable ( { pwsh = false , pwshPrev = false , executable , } ) { const { PowerShellWin , PowerShellCore , PowerShellCorePreview } = PSExecutableType ; if ( process . env . NODE_POWERSHELL ) { return process . env . NODE_POWERSHELL as PSExecutableType ; } if ( pwsh ) { return PowerShellCore ; } if ( pwshPrev ) { return PowerShellCorePreview ; } if ( ! executable ) { return ! isWin ? PowerShellCore : PowerShellWin ; } switch ( executable ) { case PowerShellWin : return PowerShellWin ; case PowerShellCore : return PowerShellCore ; case PowerShellCorePreview : return PowerShellCore ; default : return super . setExecutable ( { executable } ) ; } } protected writeToOutput ( input ) { return `[Console]::Out.WriteLine("${ input }")` ; } protected writeToError ( input ) { return `[Console]::Error.WriteLine("${ input }")` ; } }