@cloud-cli/dx 2.1.2 → 2.1.4

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.
@@ -1,7 +1,8 @@
1
1
  import { exec } from '@cloud-cli/exec';
2
- import { getConfig } from '@cloud-cli/cli';
2
+ import { getConfig, logInfo, logError } from '@cloud-cli/cli';
3
3
  import { findContainer, listContainers } from './store.js';
4
4
  import { addExecFlag, getEnvVars, getListFromString, readTargetName } from './utils.js';
5
+ import getPort from 'get-port';
5
6
  export async function getRunningContainers() {
6
7
  const ps = await exec('docker', ['ps', '--format', '{{.Names}}']);
7
8
  if (!ps.ok) {
@@ -67,11 +68,16 @@ export async function startContainer(options, { run }) {
67
68
  if (!name) {
68
69
  throw new Error('Name is required');
69
70
  }
71
+ const container = findContainer(name);
72
+ if (!container) {
73
+ throw new Error('Container not found: ' + name);
74
+ }
70
75
  const vars = (await run('env.show', { name }));
76
+ const port = String(container.port || (await getPort()));
77
+ vars.push({ key: 'PORT', value: port });
71
78
  const { env, envKeys } = await getEnvVars(vars);
72
- const container = findContainer(name);
73
- const volumes = addExecFlag(container.volumes, 'v');
74
- const ports = ['-p' + env.PORT + ':' + (container.port || env.PORT)];
79
+ const volumes = addExecFlag(container.volumes.split(','), 'v');
80
+ const ports = addExecFlag([`${port}:${port}`], 'p');
75
81
  if (container.domain) {
76
82
  const [domain] = container.domain.split('/');
77
83
  await run('px.update', { domain, target: 'http://localhost:' + env.PORT });
@@ -95,14 +101,17 @@ export async function startContainer(options, { run }) {
95
101
  ...extraArgs,
96
102
  ...volumes,
97
103
  ...ports,
98
- ...envKeys.map((e) => '-e' + e),
104
+ ...addExecFlag(envKeys, 'e'),
99
105
  container.image,
100
106
  ];
101
107
  const output = await exec('docker', execArgs.filter(Boolean), { env });
102
108
  if (!output.ok) {
109
+ const e = 'Failed to start container ' + name;
103
110
  console.log([output.stdout, output.stderr].join('\n\n'));
104
- throw new Error('Failed to start ' + name);
111
+ logError(e);
112
+ throw new Error(e);
105
113
  }
114
+ logInfo('Started container ' + name);
106
115
  return true;
107
116
  }
108
117
  export async function stopContainer(options, { run }) {
@@ -113,9 +122,10 @@ export async function stopContainer(options, { run }) {
113
122
  }
114
123
  await exec('docker', ['stop', '-t', '5', name]);
115
124
  await exec('docker', ['rm', name]);
116
- const containers = [findContainer(name)].filter(Boolean);
125
+ const containers = [findContainer(name)].filter(c => c && c.domain);
117
126
  for (const container of containers) {
118
127
  await run('dns.remove', { domain: container.domain });
119
128
  }
129
+ logInfo('Stopped container ' + name);
120
130
  return true;
121
131
  }
package/dist/utils.d.ts CHANGED
@@ -10,6 +10,6 @@ export declare function getEnvVars(vars: EnvList): Promise<{
10
10
  envKeys: string[];
11
11
  }>;
12
12
  export declare function getListFromString(string: string): string[];
13
- export declare function addExecFlag(string: string, flag: string): string[];
13
+ export declare function addExecFlag(list: string[], flag: string): string[];
14
14
  export declare function readTargetName(args: any): void;
15
15
  export declare function readTargetImage(args: any): void;
package/dist/utils.js CHANGED
@@ -1,9 +1,7 @@
1
- import getPort from 'get-port';
2
1
  export async function getEnvVars(vars) {
3
- const port = await getPort();
4
2
  const envKeys = [];
5
3
  const env = { ...process.env };
6
- vars.concat({ key: 'PORT', value: String(port) }).forEach(({ key, value }) => {
4
+ vars.forEach(({ key, value }) => {
7
5
  envKeys.push(key);
8
6
  env[key] = String(value);
9
7
  });
@@ -12,9 +10,8 @@ export async function getEnvVars(vars) {
12
10
  export function getListFromString(string) {
13
11
  return String(string).trim().split('\n').filter(Boolean);
14
12
  }
15
- export function addExecFlag(string, flag) {
16
- return string
17
- .split(',')
13
+ export function addExecFlag(list, flag) {
14
+ return list
18
15
  .filter(Boolean)
19
16
  .map((p) => `-${flag}${p}`);
20
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dx",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "Docker container management",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -19,6 +19,7 @@
19
19
  "@cloud-cli/prettier-config": "^1.0.0",
20
20
  "@cloud-cli/typescript-config": "^1.0.1",
21
21
  "@types/node": "^20.12.8",
22
+ "@vitest/coverage-v8": "^1.6.0",
22
23
  "typescript": "^5.4.5",
23
24
  "vitest": "^1.6.0"
24
25
  },