import { ChildProcess } from 'child_process' ; import * as shelljs from 'shelljs' ; import { Progress , ProgressLocation , window , workspace } from 'vscode' ; import { GitOpsExtensionConstants } from './extension' ; import { getExtensionContext } from './extensionContext' ; import { output } from './output' ; /**
 * Ignore `"vs-kubernetes.use-wsl" setting.
 * Always return false.
 *
 * Get setting value of Kubernetes extension
 * of whether or not to use wsl on windows.
 *
 * Undocumented setting:
 * https://github.com/Azure/vscode-kubernetes-tools/issues/695
 */ export function getUseWsl ( ) { return false ; } /**
 * User OS.
 */ export const enum Platform { Windows , MacOS , Linux , Unsupported } export type ExecCallback = shelljs . ExecCallback ; const WINDOWS = 'win32' ; export interface ShellResult { readonly code : number ; readonly stdout : string ; readonly stderr : string ; } export type ShellHandler = ( code : number , stdout : string , stderr : string ) => void ; /**
 * Return `true` when user has windows OS.
 */ function isWindows ( ) { return ( process . platform === WINDOWS ) && ! getUseWsl ( ) ; } /**
 * Return `true` when user has Unix OS.
 */ function isUnix ( ) { return ! isWindows ( ) ; } /**
 * Return user platform.
 * For WSL - return Linux.
 */ function platform ( ) { if ( getUseWsl ( ) ) { return Platform . Linux ; } switch ( process . platform ) { case 'win32' : return Platform . Windows ; case 'darwin' : return Platform . MacOS ; case 'linux' : return Platform . Linux ; default : return Platform . Unsupported ; } } function execOpts ( { cwd } = { } ) { let env = undefined ; if ( isWindows ( ) ) { const fluxPath = getExtensionContext ( ) . globalState . get ( GitOpsExtensionConstants . FluxPath ) ; if ( fluxPath ) { env = process . env ; env . Path += `;${ fluxPath }` ; } } const opts = { cwd : cwd , env : env , async : true , } ; return opts ; } async function exec ( cmd , { cwd } = { } ) { try { return await execCore ( cmd , execOpts ( { cwd } ) , null ) ; } catch ( e ) { console . error ( e ) ; window . showErrorMessage ( String ( e ) ) ; return undefined ; } } param cmd async function execWithOutput ( cmd , { revealOutputView = true , showProgress = true , cwd , } = { } ) { if ( showProgress ) { return window . withProgress ( { location : ProgressLocation . Notification , title : 'GitOps Running: ' , } , innerExec ) ; } else { const progressNoop = { report : ( ) => undefined , } ; return innerExec ( progressNoop ) ; } async function innerExec ( progress ) { return new Promise < ShellResult > ( resolve => { output . send ( `> ${ cmd }\n` , { addNewline : false , revealOutputView } ) ; const childProcess = shelljs . exec ( cmd , { async : true , cwd : cwd , env : execOpts ( ) . env , } ) ; let stdout = '' ; let stderr = '' ; childProcess . stdout ?. on ( 'data' , ( data ) => { stdout += data ; output . send ( data , { addNewline : false , revealOutputView : false } ) ; progress . report ( { message : data , } ) ; } ) ; childProcess . stderr ?. on ( 'data' , ( data ) => { stderr += data ; output . send ( data , { addNewline : false , revealOutputView : false } ) ; progress . report ( { message : data , } ) ; } ) ; childProcess . on ( 'exit' , ( code ) => { output . send ( '\n' , { addNewline : false , revealOutputView : false } ) ; resolve ( { code , stdout , stderr , } ) ; } ) ; } ) ; } } function execCore ( cmd , opts , callback ? , stdin ? ) { return new Promise < ShellResult > ( resolve => { if ( getUseWsl ( ) ) { cmd = `wsl ${ cmd }` ; } const proc = shelljs . exec ( cmd , opts , ( code , stdout , stderr ) => resolve ( { code : code , stdout : stdout , stderr : stderr } ) ) ; if ( stdin ) { proc . stdin ?. end ( stdin ) ; } if ( callback ) { callback ( proc ) ; } } ) ; } export const shell = { isWindows : isWindows , isUnix : isUnix , platform : platform , exec : exec , execWithOutput : execWithOutput , } ;