@augment-vir/node 31.43.3 → 31.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +0 -2
  2. package/dist/augments/npm/npm-deps.d.ts +38 -0
  3. package/dist/augments/npm/npm-deps.js +47 -0
  4. package/dist/augments/npm/package-json.d.ts +21 -0
  5. package/dist/augments/npm/package-json.js +48 -0
  6. package/dist/file-paths.mock.d.ts +0 -6
  7. package/dist/file-paths.mock.js +0 -6
  8. package/dist/index.d.ts +2 -1
  9. package/dist/index.js +2 -1
  10. package/package.json +4 -6
  11. package/src/augments/npm/npm-deps.ts +85 -0
  12. package/src/augments/npm/package-json.ts +75 -0
  13. package/src/file-paths.mock.ts +0 -7
  14. package/src/index.ts +2 -1
  15. package/dist/augments/docker.d.ts +0 -92
  16. package/dist/augments/docker.js +0 -87
  17. package/dist/docker/containers/container-info.d.ts +0 -57
  18. package/dist/docker/containers/container-info.js +0 -18
  19. package/dist/docker/containers/container-status.d.ts +0 -32
  20. package/dist/docker/containers/container-status.js +0 -68
  21. package/dist/docker/containers/copy-to-container.d.ts +0 -14
  22. package/dist/docker/containers/copy-to-container.js +0 -10
  23. package/dist/docker/containers/docker-command-inputs.d.ts +0 -74
  24. package/dist/docker/containers/docker-command-inputs.js +0 -45
  25. package/dist/docker/containers/kill-container.d.ts +0 -10
  26. package/dist/docker/containers/kill-container.js +0 -12
  27. package/dist/docker/containers/run-command.d.ts +0 -18
  28. package/dist/docker/containers/run-command.js +0 -24
  29. package/dist/docker/containers/run-container.d.ts +0 -23
  30. package/dist/docker/containers/run-container.js +0 -47
  31. package/dist/docker/containers/run-container.mock.d.ts +0 -2
  32. package/dist/docker/containers/run-container.mock.js +0 -16
  33. package/dist/docker/containers/try-or-kill-container.d.ts +0 -6
  34. package/dist/docker/containers/try-or-kill-container.js +0 -14
  35. package/dist/docker/docker-image.d.ts +0 -9
  36. package/dist/docker/docker-image.js +0 -50
  37. package/dist/docker/docker-startup.d.ts +0 -2
  38. package/dist/docker/docker-startup.js +0 -39
  39. package/dist/docker/run-docker-test.mock.d.ts +0 -2
  40. package/dist/docker/run-docker-test.mock.js +0 -23
  41. package/src/augments/docker.ts +0 -119
  42. package/src/docker/containers/container-info.ts +0 -83
  43. package/src/docker/containers/container-status.ts +0 -110
  44. package/src/docker/containers/copy-to-container.ts +0 -34
  45. package/src/docker/containers/docker-command-inputs.ts +0 -122
  46. package/src/docker/containers/kill-container.ts +0 -25
  47. package/src/docker/containers/run-command.ts +0 -51
  48. package/src/docker/containers/run-container.mock.ts +0 -22
  49. package/src/docker/containers/run-container.ts +0 -92
  50. package/src/docker/containers/try-or-kill-container.ts +0 -18
  51. package/src/docker/docker-image.ts +0 -61
  52. package/src/docker/docker-startup.ts +0 -49
  53. package/src/docker/run-docker-test.mock.ts +0 -26
@@ -1,119 +0,0 @@
1
- import {getContainerInfo} from '../docker/containers/container-info.js';
2
- import {
3
- getContainerLogs,
4
- getContainerStatus,
5
- waitUntilContainerExited,
6
- waitUntilContainerRemoved,
7
- waitUntilContainerRunning,
8
- } from '../docker/containers/container-status.js';
9
- import {copyToContainer} from '../docker/containers/copy-to-container.js';
10
- import {
11
- makeEnvFlags,
12
- makePortMapFlags,
13
- makeVolumeFlags,
14
- } from '../docker/containers/docker-command-inputs.js';
15
- import {killContainer} from '../docker/containers/kill-container.js';
16
- import {runContainerCommand} from '../docker/containers/run-command.js';
17
- import {runContainer} from '../docker/containers/run-container.js';
18
- import {tryOrKillContainer} from '../docker/containers/try-or-kill-container.js';
19
- import {
20
- isImageInLocalRegistry,
21
- removeImageFromLocalRegistry,
22
- updateImage,
23
- } from '../docker/docker-image.js';
24
- import {isDockerRunning, startDocker} from '../docker/docker-startup.js';
25
-
26
- export {
27
- type DockerContainerInfo,
28
- type DockerContainerInfoState,
29
- } from '../docker/containers/container-info.js';
30
- export {
31
- DockerContainerStatus,
32
- exitedDockerContainerStatuses,
33
- } from '../docker/containers/container-status.js';
34
- export {type CopyToDockerContainerParams} from '../docker/containers/copy-to-container.js';
35
- export {
36
- type DockerEnvMap,
37
- type DockerPortMap,
38
- type DockerVolumeMap,
39
- type DockerVolumeMappingType,
40
- } from '../docker/containers/docker-command-inputs.js';
41
- export {type RunDockerContainerCommandParams} from '../docker/containers/run-command.js';
42
- export {type RunDockerContainerParams} from '../docker/containers/run-container.js';
43
-
44
- /**
45
- * Centralized Docker API.
46
- *
47
- * @deprecated Use the [docker-vir](https://www.npmjs.com/package/docker-vir) package instead.
48
- * @category Node : Docker
49
- * @category Package : @augment-vir/node
50
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
51
- */
52
- export const docker = {
53
- /** Detects if the Docker service is running. */
54
- isRunning: isDockerRunning,
55
- /**
56
- * Tries to start Docker based ont he current operating system's supported commands. The success
57
- * of this operation is heavily dependent on how you have Docker setup on your system.
58
- */
59
- start: startDocker,
60
- image: {
61
- /** Downloads an image if it is missing from the local registry. */
62
- update: updateImage,
63
- /** Removes an image from the local registry. */
64
- remove: removeImageFromLocalRegistry,
65
- /** Detects if an image exists in the local registry. */
66
- exists: isImageInLocalRegistry,
67
- },
68
- container: {
69
- /**
70
- * Get the current status of a container. If the container does not exist at all, the status
71
- * will be {@link DockerContainerStatus.Removed}.
72
- */
73
- getStatus: getContainerStatus,
74
- /** Wait until a container is running and responsive. */
75
- waitUntilRunning: waitUntilContainerRunning,
76
- /**
77
- * Wait until a container has a status that can be classified as "exited".
78
- *
79
- * @see {@link exitedDockerContainerStatuses}
80
- */
81
- waitUntilExited: waitUntilContainerExited,
82
- /** Wait until a container is completely removed. */
83
- waitUntilRemoved: waitUntilContainerRemoved,
84
- /**
85
- * Runs a callback (which presumably will run a command within the given `containerName`)
86
- * and kills the container if the callback fails.
87
- */
88
- tryOrKill: tryOrKillContainer,
89
- /** Run a container that isn't already running. */
90
- run: runContainer,
91
- /** Kill a container. */
92
- kill: killContainer,
93
- /** Copy a file or directory to a container. */
94
- copyTo: copyToContainer,
95
- /** Run a command on a container that is already running. */
96
- runCommand: runContainerCommand,
97
- /** Run `docker inspect` on a container and return its output. */
98
- getInfo: getContainerInfo,
99
- /** Get a container's logs. */
100
- getLogs: getContainerLogs,
101
- },
102
- util: {
103
- /**
104
- * Manually create a string of volume mapping flags. This is automatically done already
105
- * inside the run container methods.
106
- */
107
- makeVolumeFlags,
108
- /**
109
- * Manually create a string of port mapping flags. This is automatically done already inside
110
- * the run container methods.
111
- */
112
- makePortMapFlags,
113
- /**
114
- * Manually create a string of env mapping flags. This is automatically done already inside
115
- * the run container methods.
116
- */
117
- makeEnvFlags,
118
- },
119
- };
@@ -1,83 +0,0 @@
1
- import {type JsonCompatibleArray, type JsonCompatibleObject} from '@augment-vir/common';
2
- import {runShellCommand} from '../../augments/terminal/shell.js';
3
- import {type DockerContainerStatus} from './container-status.js';
4
-
5
- /**
6
- * Properties on {@link DockerContainerInfo}.State, retrieved from `docker.container.getInfo()`.
7
- *
8
- * @category Node : Docker : Util
9
- * @category Package : @augment-vir/node
10
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
11
- */
12
- export type DockerContainerInfoState = {
13
- Status: DockerContainerStatus;
14
- Running: boolean;
15
- Paused: boolean;
16
- Restarting: boolean;
17
- OOMKilled: boolean;
18
- Dead: boolean;
19
- Pid: number;
20
- ExitCode: number;
21
- Error: string;
22
- StartedAt: string;
23
- FinishedAt: string;
24
- };
25
-
26
- /** This type signature is incomplete. Add to it as necessary. */
27
-
28
- /**
29
- * Properties on the output from `docker.container.getInfo()`. Not all these properties are filled
30
- * in all the way, particularly most of properties with nested objects.
31
- *
32
- * @category Node : Docker : Util
33
- * @category Package : @augment-vir/node
34
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
35
- */
36
- export type DockerContainerInfo = Readonly<{
37
- Id: string;
38
- Created: string;
39
- Path: string;
40
- Args: ReadonlyArray<string>;
41
- State: DockerContainerInfoState;
42
- Image: string;
43
- ResolvConfPath: string;
44
- HostnamePath: string;
45
- HostsPath: string;
46
- LogPath: string;
47
- Name: string;
48
- RestartCount: number;
49
- Driver: string;
50
- Platform: string;
51
- MountLabel: string;
52
- ProcessLabel: string;
53
- AppArmorProfile: string;
54
- ExecIDs: unknown;
55
- HostConfig: JsonCompatibleObject;
56
- GraphDriver: JsonCompatibleObject;
57
- Mounts: JsonCompatibleArray;
58
- Config: JsonCompatibleObject;
59
- NetworkSettings: JsonCompatibleObject;
60
- }>;
61
-
62
- export async function getContainerInfo(
63
- containerNameOrId: string,
64
- ): Promise<DockerContainerInfo | undefined> {
65
- const command = `docker inspect '${containerNameOrId}'`;
66
- const output = await runShellCommand(command);
67
-
68
- if (output.stderr.includes('Error: No such object')) {
69
- return undefined;
70
- }
71
-
72
- const parsedOutput = JSON.parse(output.stdout) as ReadonlyArray<DockerContainerInfo>;
73
-
74
- /** Edge cases that I don't know how to intentionally trigger. */
75
- /* node:coverage ignore next 5 */
76
- if (parsedOutput.length === 0) {
77
- throw new Error(`Got no output from "${command}"`);
78
- } else if (parsedOutput.length > 1) {
79
- throw new Error(`Got more than one output from "${command}"`);
80
- }
81
-
82
- return parsedOutput[0];
83
- }
@@ -1,110 +0,0 @@
1
- import {assert, waitUntil} from '@augment-vir/assert';
2
- import {runShellCommand} from '../../augments/terminal/shell.js';
3
-
4
- export async function getContainerLogs(
5
- containerNameOrId: string,
6
- latestLineCount?: number,
7
- ): Promise<string> {
8
- const latestLinesArg = latestLineCount == undefined ? '' : `--tail ${latestLineCount}`;
9
- const logResult = await runShellCommand(
10
- `docker logs ${latestLinesArg} '${containerNameOrId}'`,
11
- {rejectOnError: true},
12
- );
13
- return logResult.stdout;
14
- }
15
-
16
- /**
17
- * All possible statuses for an existing container.
18
- *
19
- * @category Node : Docker : Util
20
- * @category Package : @augment-vir/node
21
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
22
- */
23
- export enum DockerContainerStatus {
24
- Created = 'created',
25
- Running = 'running',
26
- Paused = 'paused',
27
- Restarting = 'restarting',
28
- Exited = 'exited',
29
- Removing = 'removing',
30
- Dead = 'dead',
31
-
32
- /** This is not a native Docker status but indicates that the container does not exist. */
33
- Removed = 'removed',
34
- }
35
-
36
- /**
37
- * Statuses from {@link DockerContainerStatus} that indicate that a container has been exited in some
38
- * way.
39
- *
40
- * @category Node : Docker : Util
41
- * @category Package : @augment-vir/node
42
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
43
- */
44
- export const exitedDockerContainerStatuses = [
45
- DockerContainerStatus.Dead,
46
- DockerContainerStatus.Removed,
47
- DockerContainerStatus.Exited,
48
- ];
49
-
50
- export async function getContainerStatus(
51
- containerNameOrId: string,
52
- ): Promise<DockerContainerStatus> {
53
- const statusResult = await runShellCommand(
54
- `docker container inspect -f '{{.State.Status}}' '${containerNameOrId}'`,
55
- );
56
-
57
- if (statusResult.exitCode !== 0) {
58
- return DockerContainerStatus.Removed;
59
- }
60
-
61
- const status = statusResult.stdout.trim();
62
- assert.isEnumValue(status, DockerContainerStatus, 'Unexpected container status value');
63
-
64
- return status;
65
- }
66
-
67
- export async function waitUntilContainerRunning(
68
- containerNameOrId: string,
69
- failureMessage?: string | undefined,
70
- ): Promise<void> {
71
- await waitUntil.isTrue(
72
- async (): Promise<boolean> => {
73
- /** Check if logs can be accessed yet. */
74
- await getContainerLogs(containerNameOrId, 1);
75
- const status = await getContainerStatus(containerNameOrId);
76
-
77
- return status === DockerContainerStatus.Running;
78
- },
79
- {},
80
- failureMessage,
81
- );
82
- }
83
-
84
- export async function waitUntilContainerRemoved(
85
- containerNameOrId: string,
86
- failureMessage?: string | undefined,
87
- ): Promise<void> {
88
- await waitUntil.isTrue(
89
- async (): Promise<boolean> => {
90
- const status = await getContainerStatus(containerNameOrId);
91
- return status === DockerContainerStatus.Removed;
92
- },
93
- {},
94
- failureMessage,
95
- );
96
- }
97
-
98
- export async function waitUntilContainerExited(
99
- containerNameOrId: string,
100
- failureMessage?: string | undefined,
101
- ): Promise<void> {
102
- await waitUntil.isTrue(
103
- async (): Promise<boolean> => {
104
- const status = await getContainerStatus(containerNameOrId);
105
- return exitedDockerContainerStatuses.includes(status);
106
- },
107
- {},
108
- failureMessage,
109
- );
110
- }
@@ -1,34 +0,0 @@
1
- import {addSuffix} from '@augment-vir/common';
2
- import {stat} from 'node:fs/promises';
3
- import {runShellCommand} from '../../augments/terminal/shell.js';
4
-
5
- /**
6
- * Parameters for `docker.container.copyTo`.
7
- *
8
- * @category Node : Docker : Util
9
- * @category Package : @augment-vir/node
10
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
11
- */
12
- export type CopyToDockerContainerParams = {
13
- hostPath: string;
14
- containerAbsolutePath: string;
15
- containerNameOrId: string;
16
- dockerFlags?: ReadonlyArray<string>;
17
- };
18
-
19
- export async function copyToContainer({
20
- containerAbsolutePath,
21
- hostPath,
22
- containerNameOrId,
23
- dockerFlags = [],
24
- }: CopyToDockerContainerParams): Promise<void> {
25
- const isDir = (await stat(hostPath)).isDirectory();
26
- const suffix = isDir ? '/.' : '';
27
- const fullHostPath = addSuffix({value: hostPath, suffix});
28
- const extraInputs: string = dockerFlags.join(' ');
29
-
30
- await runShellCommand(
31
- `docker cp ${extraInputs} '${fullHostPath}' '${containerNameOrId}:${containerAbsolutePath}'`,
32
- {rejectOnError: true},
33
- );
34
- }
@@ -1,122 +0,0 @@
1
- import {wrapString} from '@augment-vir/common';
2
-
3
- /**
4
- * Used for `type` in {@link DockerVolumeMap}. These types are apparently only relevant for running
5
- * Docker on macOS and are potentially irrelevant now. It's likely best to leave the `type` property
6
- * empty (`undefined`).
7
- *
8
- * @category Node : Docker : Util
9
- * @category Package : @augment-vir/node
10
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
11
- */
12
- export enum DockerVolumeMappingType {
13
- Cached = 'cached',
14
- Delegated = 'delegated',
15
- }
16
-
17
- /**
18
- * A mapping of a single docker volume for mounting host files to a container.
19
- *
20
- * @category Node : Docker : Util
21
- * @category Package : @augment-vir/node
22
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
23
- */
24
- export type DockerVolumeMap = {
25
- hostAbsolutePath: string;
26
- containerAbsolutePath: string;
27
- type?: DockerVolumeMappingType | undefined;
28
- };
29
-
30
- export function makeVolumeFlags(volumeMapping?: ReadonlyArray<DockerVolumeMap>): string {
31
- if (!volumeMapping) {
32
- return '';
33
- }
34
-
35
- const parts = volumeMapping.map((volume) => {
36
- const mountType = volume.type ? `:${volume.type}` : '';
37
- return `-v '${volume.hostAbsolutePath}':'${volume.containerAbsolutePath}'${mountType}`;
38
- });
39
-
40
- return parts.join(' ');
41
- }
42
- /**
43
- * A single docker container port mapping. This is usually used in an array.
44
- *
45
- * @category Node : Docker : Util
46
- * @category Package : @augment-vir/node
47
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
48
- */
49
- export type DockerPortMap = {
50
- hostPort: number;
51
- containerPort: number;
52
- };
53
-
54
- /**
55
- * A set of environment mappings for a docker container.
56
- *
57
- * - Each key in this object represents the env var name within the Docker container.
58
- * - Each `value` property can be either the value that the env var should be set to _or_ an existing
59
- * env var's interpolation into the value.
60
- * - If the value string is meant to be interpolated within the shell context, make sure to set
61
- * `allowInterpolation` to `true`. Otherwise, it's best to leave it as `false`.
62
- *
63
- * @category Node : Docker : Util
64
- * @category Package : @augment-vir/node
65
- * @example
66
- *
67
- * ```ts
68
- * const envMapping: DockerEnvMap = {
69
- * VAR_1: {
70
- * value: 'hi',
71
- * // set to false because this is a raw string value that is not meant to be interpolated
72
- * allowInterpolation: false,
73
- * },
74
- * VAR_2: {
75
- * // the value here will be interpolated from the current shell's value for `EXISTING_VAR`
76
- * value: '$EXISTING_VAR',
77
- * // set to true to allow '$EXISTING_VAR' to be interpolated by the shell
78
- * allowInterpolation: true,
79
- * },
80
- * };
81
- * ```
82
- *
83
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
84
- */
85
- export type DockerEnvMap<RequiredKeys extends string = string> = Readonly<
86
- Record<
87
- RequiredKeys | string,
88
- {
89
- value: string;
90
- allowInterpolation: boolean;
91
- }
92
- >
93
- >;
94
-
95
- export function makePortMapFlags(portMapping?: ReadonlyArray<DockerPortMap> | undefined): string {
96
- if (!portMapping) {
97
- return '';
98
- }
99
-
100
- return portMapping
101
- .map((portMap) => {
102
- return `-p ${portMap.hostPort}:${portMap.containerPort}`;
103
- })
104
- .join(' ');
105
- }
106
-
107
- export function makeEnvFlags(envMapping?: DockerEnvMap | undefined): string {
108
- if (!envMapping) {
109
- return '';
110
- }
111
- const flags: ReadonlyArray<string> = Object.entries(envMapping).map(
112
- ([
113
- key,
114
- {value, allowInterpolation},
115
- ]) => {
116
- const quote = allowInterpolation ? '"' : "'";
117
- return `-e ${key}=${wrapString({value, wrapper: quote})}`;
118
- },
119
- );
120
-
121
- return flags.join(' ');
122
- }
@@ -1,25 +0,0 @@
1
- import {type PartialWithUndefined} from '@augment-vir/core';
2
- import {runShellCommand} from '../../augments/terminal/shell.js';
3
- import {waitUntilContainerExited, waitUntilContainerRemoved} from './container-status.js';
4
-
5
- export async function killContainer(
6
- containerNameOrId: string,
7
- options: PartialWithUndefined<{
8
- /**
9
- * If set to `true`, the container will be killed but won't be removed. (You'll still see it
10
- * in your Docker Dashboard but it'll have a status of exited.)
11
- *
12
- * @default false
13
- */
14
- keepContainer: boolean;
15
- }> = {},
16
- ): Promise<void> {
17
- await runShellCommand(`docker kill '${containerNameOrId}'`);
18
-
19
- if (options.keepContainer) {
20
- await waitUntilContainerExited(containerNameOrId);
21
- } else {
22
- await runShellCommand(`docker rm '${containerNameOrId}'`);
23
- await waitUntilContainerRemoved(containerNameOrId);
24
- }
25
- }
@@ -1,51 +0,0 @@
1
- import {check} from '@augment-vir/assert';
2
- import {runShellCommand} from '../../augments/terminal/shell.js';
3
- import {type DockerEnvMap, makeEnvFlags} from './docker-command-inputs.js';
4
-
5
- /**
6
- * Parameters for `docker.container.runCommand`.
7
- *
8
- * @category Node : Docker : Util
9
- * @category Package : @augment-vir/node
10
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
11
- */
12
- export type RunDockerContainerCommandParams = {
13
- /** Creates an interactive shell connection. */
14
- tty?: boolean | undefined;
15
- containerNameOrId: string;
16
- command: string;
17
- envMapping?: DockerEnvMap | undefined;
18
- executionEnv?: Record<string, string> | undefined;
19
- dockerFlags?: ReadonlyArray<string> | undefined;
20
- };
21
-
22
- export async function runContainerCommand({
23
- tty,
24
- containerNameOrId,
25
- command,
26
- envMapping,
27
- executionEnv,
28
- dockerFlags = [],
29
- }: RunDockerContainerCommandParams) {
30
- const envFlags = makeEnvFlags(envMapping);
31
- /** Can't test tty in automated tests. */
32
- /* node:coverage ignore next 1 */
33
- const ttyFlag = tty ? '-it' : '';
34
-
35
- const fullCommand = [
36
- 'docker',
37
- 'exec',
38
- ttyFlag,
39
- envFlags,
40
- ...dockerFlags,
41
- containerNameOrId,
42
- command,
43
- ]
44
- .filter(check.isTruthy)
45
- .join(' ');
46
-
47
- return await runShellCommand(fullCommand, {
48
- env: executionEnv,
49
- rejectOnError: true,
50
- });
51
- }
@@ -1,22 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-deprecated */
2
-
3
- import {docker} from '../../augments/docker.js';
4
- import {type RunDockerContainerParams} from './run-container.js';
5
-
6
- export async function runMockLongLivingContainer(
7
- containerName: string,
8
- args: Partial<RunDockerContainerParams> = {},
9
- ) {
10
- await docker.container.run({
11
- containerName,
12
- detach: true,
13
- imageName: 'alpine:3.20.2',
14
- dockerFlags: [
15
- '-i',
16
- '-t',
17
- ],
18
- command: 'sh',
19
- platform: 'amd64',
20
- ...args,
21
- });
22
- }
@@ -1,92 +0,0 @@
1
- import {check} from '@augment-vir/assert';
2
- import {runShellCommand} from '../../augments/terminal/shell.js';
3
- import {updateImage} from '../docker-image.js';
4
- import {waitUntilContainerRunning} from './container-status.js';
5
- import {
6
- type DockerEnvMap,
7
- type DockerPortMap,
8
- type DockerVolumeMap,
9
- makeEnvFlags,
10
- makePortMapFlags,
11
- makeVolumeFlags,
12
- } from './docker-command-inputs.js';
13
- import {killContainer} from './kill-container.js';
14
-
15
- /**
16
- * Parameters for `docker.container.run`.
17
- *
18
- * @category Node : Docker : Util
19
- * @category Package : @augment-vir/node
20
- * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
21
- */
22
- export type RunDockerContainerParams = {
23
- imageName: string;
24
- detach: boolean;
25
- command?: string;
26
- containerName: string;
27
- volumeMapping?: ReadonlyArray<DockerVolumeMap> | undefined;
28
- portMapping?: ReadonlyArray<DockerPortMap> | undefined;
29
- envMapping?: DockerEnvMap | undefined;
30
- executionEnv?: Record<string, string>;
31
- removeWhenDone?: boolean;
32
- dockerFlags?: ReadonlyArray<string>;
33
- useCurrentUser?: boolean;
34
- platform?: string;
35
- };
36
-
37
- export async function runContainer({
38
- containerName,
39
- imageName,
40
- detach,
41
- command,
42
- portMapping,
43
- volumeMapping,
44
- envMapping,
45
- executionEnv,
46
- removeWhenDone,
47
- useCurrentUser,
48
- dockerFlags = [],
49
- platform,
50
- }: RunDockerContainerParams) {
51
- try {
52
- const portMapFlags = makePortMapFlags(portMapping);
53
- const envFlags = makeEnvFlags(envMapping);
54
- const detachFlag = detach ? '-d' : '';
55
- const volumeMapFlags = makeVolumeFlags(volumeMapping);
56
- const rmFlag = removeWhenDone ? '--rm' : '';
57
- const userFlag = useCurrentUser ? '--user "$(id -u)":"$(id -g)"' : '';
58
-
59
- await updateImage(imageName, platform);
60
-
61
- const fullCommand = [
62
- 'docker',
63
- 'run',
64
- portMapFlags,
65
- userFlag,
66
- volumeMapFlags,
67
- envFlags,
68
- rmFlag,
69
- detachFlag,
70
- `--name='${containerName}'`,
71
- ...dockerFlags,
72
- imageName,
73
- command,
74
- ]
75
- .filter(check.isTruthy)
76
- .join(' ');
77
-
78
- await runShellCommand(fullCommand, {
79
- env: executionEnv,
80
- rejectOnError: true,
81
- });
82
-
83
- if (removeWhenDone) {
84
- await killContainer(containerName);
85
- } else if (detach) {
86
- await waitUntilContainerRunning(containerName);
87
- }
88
- } catch (error) {
89
- await killContainer(containerName);
90
- throw error;
91
- }
92
- }
@@ -1,18 +0,0 @@
1
- import {type MaybePromise} from '@augment-vir/core';
2
- import {killContainer} from './kill-container.js';
3
-
4
- /**
5
- * Runs a callback (which presumably runs a command within the given `containerName`) and kills the
6
- * given `containerName` container if the callback fails.
7
- */
8
- export async function tryOrKillContainer<T>(
9
- containerNameOrId: string,
10
- callback: (containerNameOrId: string) => MaybePromise<T>,
11
- ): Promise<Awaited<T>> {
12
- try {
13
- return await callback(containerNameOrId);
14
- } catch (error) {
15
- await killContainer(containerNameOrId);
16
- throw error;
17
- }
18
- }