14 lines
776 B
TypeScript
14 lines
776 B
TypeScript
import { posix } from 'node:path'
|
|
|
|
export function resolveContainerTerminal(inspectJson: string, moduleRoot: string): { id: string; cwd: string } {
|
|
const [container] = JSON.parse(inspectJson)
|
|
if (!container?.State?.Running) throw new Error('Module container is not running')
|
|
const root = posix.resolve(moduleRoot)
|
|
const mounts = (container.Mounts ?? []) as { Source: string; Destination: string }[]
|
|
const mount = mounts
|
|
.filter((item) => root === item.Source || root.startsWith(`${item.Source.replace(/\/$/, '')}/`))
|
|
.sort((a, b) => b.Source.length - a.Source.length)[0]
|
|
if (!mount) throw new Error('Module workspace is not mounted in this container')
|
|
return { id: container.Id, cwd: posix.join(mount.Destination, posix.relative(mount.Source, root)) }
|
|
}
|