"use strict" ; import fs = require ( "fs" ) ; import path = require ( "path" ) ; import { ensurePathExists , checkIfFileExists } from 'coc-utils' ; export let PowerShellLanguageId = "powershell" ; export interface IEditorServicesSessionDetails { status : string ; reason : string ; detail : string ; powerShellVersion : string ; channel : string ; languageServicePort : number ; debugServicePort : number ; languageServicePipeName : string ; debugServicePipeName : string ; } export type IReadSessionFileCallback = ( details : IEditorServicesSessionDetails ) => void ; export type IWaitForSessionFileCallback = ( details : IEditorServicesSessionDetails , error : string ) => void ; const sessionsFolder = path . resolve ( __dirname , ".." , ".." , "sessions/" ) ; const sessionFilePathPrefix = path . resolve ( sessionsFolder , "PSES-VSCode-" + process . env . VSCODE_PID ) ; ensurePathExists ( sessionsFolder ) ; export function getSessionFilePath ( uniqueId ) { return `${ sessionFilePathPrefix }-${ uniqueId }` ; } export function getDebugSessionFilePath ( ) { return `${ sessionFilePathPrefix }-Debug` ; } export function writeSessionFile ( sessionFilePath , sessionDetails ) { ensurePathExists ( sessionsFolder ) ; const writeStream = fs . createWriteStream ( sessionFilePath ) ; writeStream . write ( JSON . stringify ( sessionDetails ) ) ; writeStream . close ( ) ; } export function waitForSessionFile ( sessionFilePath , callback ) { function innerTryFunc ( remainingTries , delayMilliseconds ) { if ( remainingTries === 0 ) { callback ( undefined , "Timed out waiting for session file to appear." ) ; } else if ( ! checkIfFileExists ( sessionFilePath ) ) { setTimeout ( ( ) => { innerTryFunc ( remainingTries - 1 , delayMilliseconds ) ; } , delayMilliseconds ) ; } else { callback ( readSessionFile ( sessionFilePath ) , undefined ) ; } } innerTryFunc ( 60 , 2000 ) ; } export function readSessionFile ( sessionFilePath ) { const fileContents = fs . readFileSync ( sessionFilePath , "utf-8" ) ; return JSON . parse ( fileContents ) ; } export function deleteSessionFile ( sessionFilePath ) { try { fs . unlinkSync ( sessionFilePath ) ; } catch ( e ) { } }