forked from devcontainers/cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding 'static' port forwarding support
- Loading branch information
Showing
2 changed files
with
26 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { DevContainerConfig, DevContainerFromDockerfileConfig, DevContainerFromImageConfig } from '../spec-configuration/configuration'; | ||
|
||
function getStaticPorts (ports: number | string | (number | string)[] | undefined): string[]{ | ||
ports = ports ?? []; | ||
ports = typeof ports === 'number' || typeof ports === 'string'? [ports] : ports; | ||
return ports.map((port) => typeof port === 'number'? `127.0.0.1:${port}:${port}`: port); | ||
} | ||
|
||
function appPorts (config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig): string[]{ | ||
return getStaticPorts(config.appPort); | ||
} | ||
|
||
function hasAppPorts(obj: unknown):obj is (DevContainerFromDockerfileConfig | DevContainerFromImageConfig) { | ||
return (obj as DevContainerFromDockerfileConfig | DevContainerFromImageConfig).appPort !== undefined; | ||
} | ||
export function applyStaticPorts (config: DevContainerConfig): string[] { | ||
let staticPorts: string[] = []; | ||
staticPorts = staticPorts.concat(...getStaticPorts(config.forwardPorts)); | ||
if(hasAppPorts(config)){ | ||
staticPorts = staticPorts.concat(...appPorts(config)); | ||
} | ||
return (<string[]>[]).concat(...staticPorts.map((port) => ['-p', port])); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters