| 134 | } |
| 135 | |
| 136 | export class RemotePathMappedRuntime extends LocalRuntime { |
| 137 | private readonly localBase: string; |
| 138 | private readonly remoteBase: string; |
| 139 | private readonly localHomeForTildeRoot: string | null; |
| 140 | private readonly muxHomeOverride: string | null; |
| 141 | private readonly resolveToRemotePath: boolean; |
| 142 | public resolvePathCallCount = 0; |
| 143 | |
| 144 | constructor(localBase: string, remoteBase: string, options?: RemotePathMappedRuntimeOptions) { |
| 145 | super(localBase); |
| 146 | this.localBase = path.resolve(localBase); |
| 147 | this.remoteBase = remoteBase === "/" ? remoteBase : remoteBase.replace(/\/+$/u, ""); |
| 148 | this.muxHomeOverride = options?.muxHome ?? null; |
| 149 | this.resolveToRemotePath = options?.resolveToRemotePath ?? true; |
| 150 | |
| 151 | if (this.remoteBase === "~") { |
| 152 | this.localHomeForTildeRoot = this.localBase; |
| 153 | } else if (this.remoteBase.startsWith("~/")) { |
| 154 | const homeRelativeSuffix = this.remoteBase.slice(1); |
| 155 | const normalizedLocalRoot = this.localBase.replaceAll("\\", "/"); |
| 156 | if (normalizedLocalRoot.endsWith(homeRelativeSuffix)) { |
| 157 | const derivedHome = normalizedLocalRoot.slice( |
| 158 | 0, |
| 159 | normalizedLocalRoot.length - homeRelativeSuffix.length |
| 160 | ); |
| 161 | this.localHomeForTildeRoot = derivedHome.length > 0 ? derivedHome : "/"; |
| 162 | } else { |
| 163 | this.localHomeForTildeRoot = null; |
| 164 | } |
| 165 | } else { |
| 166 | this.localHomeForTildeRoot = null; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | private usesTildeWorkspaceRoot(): boolean { |
| 171 | return this.remoteBase === "~" || this.remoteBase.startsWith("~/"); |
| 172 | } |
| 173 | |
| 174 | protected toLocalPath(runtimePath: string): string { |
| 175 | const normalizedRuntimePath = runtimePath.replaceAll("\\", "/"); |
| 176 | |
| 177 | if (normalizedRuntimePath === this.remoteBase) { |
| 178 | return this.localBase; |
| 179 | } |
| 180 | |
| 181 | if (normalizedRuntimePath.startsWith(`${this.remoteBase}/`)) { |
| 182 | const suffix = normalizedRuntimePath.slice(this.remoteBase.length + 1); |
| 183 | return path.join(this.localBase, ...suffix.split("/")); |
| 184 | } |
| 185 | |
| 186 | return runtimePath; |
| 187 | } |
| 188 | |
| 189 | private toRemotePath(localPath: string): string { |
| 190 | const resolvedLocalPath = path.resolve(localPath); |
| 191 | |
| 192 | if (resolvedLocalPath === this.localBase) { |
| 193 | return this.remoteBase; |
nothing calls this directly
no outgoing calls
no test coverage detected