import * as crypto from "crypto" ; import * as fse from 'fs-extra' ; import * as path from 'path' ; import { extensionName , sessionFolderKey } from '../constants' ; import { ext } from '../extensionVariables' ; export async function createTemporaryFile ( fileName ) { const defaultWorkspacePath = path . join ( ext . context . globalStoragePath , extensionName ) ; await fse . ensureDir ( defaultWorkspacePath ) ; const sessionFolder = getSessionWorkingFolderName ( ) ; const filePath = path . join ( defaultWorkspacePath , sessionFolder , fileName ) ; await fse . ensureFile ( filePath ) ; return filePath ; } export function getSessionWorkingFolderName ( ) { let sessionFolderName = ext . context . globalState . get ( sessionFolderKey ) ; if ( ! sessionFolderName ) { sessionFolderName = getRandomHexString ( ) ; ext . outputChannel . appendLine ( `Session working folder:${ sessionFolderName }` ) ; ext . context . globalState . update ( sessionFolderKey , sessionFolderName ) ; } return < string > sessionFolderName ; } export function getRandomHexString ( length = 10 ) { const buffer = crypto . randomBytes ( Math . ceil ( length / 2 ) ) ; return buffer . toString ( 'hex' ) . slice ( 0 , length ) ; } export function getDefaultWorkspacePath ( ) { return path . join ( ext . context . globalStoragePath , extensionName ) ; }