import fs = require ( 'fs' ) ; import os = require ( 'os' ) ; import path = require ( 'path' ) ; export let PowerShellLanguageId = 'powershell' ; export function ensurePathExists ( targetPath ) { try { fs . mkdirSync ( targetPath ) ; } catch ( e ) { if ( e . code != 'EEXIST' ) { throw e ; } } } export interface EditorServicesSessionDetails { status : string ; reason : string ; detail : string ; powerShellVersion : string ; channel : string ; languageServicePort : number ; debugServicePort : number ; } export interface ReadSessionFileCallback { ( details ) : void ; } export interface WaitForSessionFileCallback { ( details , error ) : void ; } let sessionsFolder = path . resolve ( __dirname , ".." , "sessions/" ) ; let sessionFilePathPrefix = path . resolve ( sessionsFolder , "PSES-Atom-" + process . pid ) ; ensurePathExists ( sessionsFolder ) ; export function getSessionFilePath ( uniqueId ) { return `${ sessionFilePathPrefix }-${ uniqueId }` ; } export function getDebugSessionFilePath ( ) { return `${ sessionFilePathPrefix }-Debug` ; } export function writeSessionFile ( sessionFilePath , sessionDetails ) { ensurePathExists ( sessionsFolder ) ; var 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 ( function ( ) { innerTryFunc ( remainingTries - 1 , delayMilliseconds ) ; } , delayMilliseconds ) ; } else { callback ( readSessionFile ( sessionFilePath ) , undefined ) ; } } innerTryFunc ( 60 , 1000 ) ; } export function readSessionFile ( sessionFilePath ) { let fileContents = fs . readFileSync ( sessionFilePath , "utf-8" ) ; return JSON . parse ( fileContents ) ; } export function deleteSessionFile ( sessionFilePath ) { try { fs . unlinkSync ( sessionFilePath ) ; } catch ( e ) { } } export function checkIfFileExists ( filePath ) { try { fs . accessSync ( filePath , fs . constants . R_OK ) ; return true ; } catch ( e ) { return false ; } } export function getTimestampString ( ) { var time = new Date ( ) ; return `[${ time . getHours ( ) }:${ time . getMinutes ( ) }:${ time . getSeconds ( ) }]` ; } export function isWindowsOS ( ) { return os . platform ( ) == "win32" ; }