import { Disposable } from '../utils/dispose' ; import * as path from 'path' ; import { PathUtil } from '../utils/pathUtil' ; import * as fs from 'fs' ; import * as vscode from 'vscode' ; import { WorkspaceManager } from './workspaceManager' ; description export class EndpointManager extends Disposable { private validEndpointRoots = new Set < string > ( ) ; constructor ( private readonly _workspaceManager ) { super ( ) ; let i = 0 ; const workspaceDocuments = vscode . workspace . textDocuments ; while ( i < workspaceDocuments . length ) { if ( ! workspaceDocuments [ i ] . isUntitled && ! this . _workspaceManager . absPathInDefaultWorkspace ( workspaceDocuments [ i ] . fileName ) ) { this . encodeLooseFileEndpoint ( workspaceDocuments [ i ] . fileName ) ; } i ++ ; } } param location returns public encodeLooseFileEndpoint ( location ) { const parent = PathUtil . GetImmediateParentDir ( location ) ; let fullParent = PathUtil . GetParentDir ( location ) ; const child = PathUtil . GetFileName ( location , true ) ; fullParent = PathUtil . ConvertToUnixPath ( fullParent ) ; this . validEndpointRoots . add ( fullParent ) ; fullParent = PathUtil . EscapePathParts ( fullParent ) ; let endpoint_prefix = `/endpoint_unsaved` ; if ( parent != '.' ) { endpoint_prefix = `/${ fullParent }` ; } return path . join ( endpoint_prefix , child ) ; } public getEndpointParent ( urlPath ) { let endpoint = urlPath . endsWith ( '/' ) ? urlPath . substr ( 0 , urlPath . length - 1 ) : urlPath ; endpoint = endpoint . split ( '/' ) . pop ( ) ; if ( ! endpoint || endpoint == '/endpoint_unsaved' ) { return '.' ; } return unescape ( endpoint ) ; } param { string } urlPath returns { string | undefined } public decodeLooseFileEndpoint ( urlPath ) { const path = PathUtil . UnescapePathParts ( urlPath ) ; if ( this . validPath ( path ) && fs . existsSync ( path ) ) { return path ; } else { return undefined ; } } param { string } file returns { boolean } private validPath ( file ) { for ( const item of this . validEndpointRoots . values ( ) ) { if ( file . startsWith ( item ) ) { return true ; } } return false ; } }