@cloud-cli/dx 1.8.3 → 2.0.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.
package/README.md CHANGED
@@ -14,8 +14,8 @@ npm i @cloud-cli/dx
14
14
  cy dx.list
15
15
  cy dx.prune
16
16
  cy dx.pull --image node:latest
17
- cy dx.add --name node-app --image node:latest --volumes 'volume:/container/path' --ports '1234:80' --host 'foo.example.com'
18
- cy dx.start --name node-app --env 'FOO=bar' --ports '1234:80'
17
+ cy dx.add --name node-app --image node:latest --volumes 'volume:/container/path' --port '8080' --host 'foo.example.com'
18
+ cy dx.start --name node-app --env 'FOO=bar'
19
19
  cy dx.logs --name node-app
20
20
  cy dx.stop --name node-app
21
21
  cy dx.remove --name node-app
@@ -1,12 +1,11 @@
1
1
  import { ServerParams } from '@cloud-cli/cli';
2
+ import { ExtraOptions } from './types.js';
2
3
  export declare function getRunningContainers(): Promise<string[]>;
3
- interface GetLogsOptions {
4
- _?: string[];
4
+ interface GetLogsOptions extends ExtraOptions {
5
5
  name: string;
6
6
  lines?: string;
7
7
  }
8
- interface ContainerName {
9
- _?: string[];
8
+ interface ContainerName extends ExtraOptions {
10
9
  name: string;
11
10
  }
12
11
  export declare function getLogs(options: GetLogsOptions): Promise<string>;
@@ -1,7 +1,7 @@
1
1
  import { exec } from '@cloud-cli/exec';
2
+ import { getConfig } from '@cloud-cli/cli';
2
3
  import { findContainer, listContainers } from './store.js';
3
- import { getConfig } from './config.js';
4
- import { addExecFlag, getEnvVars, getListFromString, getPorts, readTargetName, } from './utils.js';
4
+ import { addExecFlag, getEnvVars, getListFromString, readTargetName } from './utils.js';
5
5
  export async function getRunningContainers() {
6
6
  const ps = await exec('docker', ['ps', '--format', '{{.Names}}']);
7
7
  if (!ps.ok) {
@@ -63,16 +63,19 @@ export async function startContainer(options, { run }) {
63
63
  const { env, envKeys } = await getEnvVars(vars);
64
64
  const container = await findContainer(name);
65
65
  const volumes = addExecFlag(container.volumes, 'v');
66
- const ports = getPorts([...container.ports.split(','), env.PORT + ':' + env.PORT]);
66
+ const ports = ['-p' + env.PORT + ':' + (container.port || env.PORT)];
67
67
  if (container.host) {
68
68
  const domain = container.host;
69
69
  await run('px.update', { domain, target: 'http://localhost:' + env.PORT });
70
70
  await run('dns.add', { domain });
71
- await run('dns.reload', {});
72
71
  }
73
- const dnsOption = [];
74
- if (getConfig('dns')) {
75
- dnsOption.push('--dns=' + getConfig('dns'));
72
+ const extraArgs = [];
73
+ const config = await getConfig('dx');
74
+ if (config.dns) {
75
+ extraArgs.push('--dns=' + config.dns);
76
+ }
77
+ if (config.dockerArgs) {
78
+ extraArgs.push(...config.dockerArgs);
76
79
  }
77
80
  const execArgs = [
78
81
  'run',
@@ -81,7 +84,7 @@ export async function startContainer(options, { run }) {
81
84
  'always',
82
85
  '--name',
83
86
  name,
84
- ...dnsOption,
87
+ ...extraArgs,
85
88
  ...volumes,
86
89
  ...ports,
87
90
  ...envKeys.map((e) => '-e' + e),
package/dist/images.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { exec } from '@cloud-cli/exec';
2
- import { readTargetImage } from './utils';
2
+ import { readTargetImage } from './utils.js';
3
3
  export async function pull(options) {
4
4
  readTargetImage(options);
5
5
  if (!options.image) {
package/dist/index.d.ts CHANGED
@@ -1,9 +1,7 @@
1
- import { init } from '@cloud-cli/cli';
2
- import { getRunningContainers, getLogs, startContainer, stopContainer, refreshContainer, startAll, restartContainer } from './containers.js';
3
- import { addContainer, removeContainer, listContainers, updateContainer, getContainer } from './store.js';
4
- import { pull, prune } from './images.js';
5
- import { Config } from './config.js';
6
- export type { Config } from './config.js';
1
+ import { getLogs, getRunningContainers, refreshContainer, restartContainer, startAll, startContainer, stopContainer } from './containers.js';
2
+ import { prune, pull } from './images.js';
3
+ import { addContainer, getContainer, listContainers, removeContainer, updateContainer } from './store.js';
4
+ export type { Config } from './types.js';
7
5
  declare const _default: {
8
6
  pull: typeof pull;
9
7
  prune: typeof prune;
@@ -19,6 +17,5 @@ declare const _default: {
19
17
  restart: typeof restartContainer;
20
18
  ps: typeof getRunningContainers;
21
19
  logs: typeof getLogs;
22
- [init](config?: Config): Promise<void>;
23
20
  };
24
21
  export default _default;
package/dist/index.js CHANGED
@@ -1,14 +1,6 @@
1
- import { init } from '@cloud-cli/cli';
2
- import { Resource, SQLiteDriver } from '@cloud-cli/store';
3
- import { Container } from './store.js';
4
- import { getRunningContainers, getLogs, startContainer, stopContainer, refreshContainer, startAll, restartContainer } from './containers.js';
5
- import { addContainer, removeContainer, listContainers, updateContainer, getContainer } from './store.js';
6
- import { pull, prune } from './images.js';
7
- import { setConfig } from './config.js';
8
- async function initResource() {
9
- Resource.use(new SQLiteDriver());
10
- await Resource.create(Container);
11
- }
1
+ import { getLogs, getRunningContainers, refreshContainer, restartContainer, startAll, startContainer, stopContainer } from './containers.js';
2
+ import { prune, pull } from './images.js';
3
+ import { addContainer, getContainer, listContainers, removeContainer, updateContainer } from './store.js';
12
4
  export default {
13
5
  pull,
14
6
  prune,
@@ -24,8 +16,4 @@ export default {
24
16
  restart: restartContainer,
25
17
  ps: getRunningContainers,
26
18
  logs: getLogs,
27
- async [init](config) {
28
- setConfig(config);
29
- await initResource();
30
- }
31
19
  };
package/dist/store.d.ts CHANGED
@@ -1,37 +1,18 @@
1
- import { Resource } from '@cloud-cli/store';
2
- export declare class Container extends Resource {
3
- id: number;
4
- name: string;
5
- image: string;
6
- host: string;
7
- ports: string;
8
- volumes: string;
9
- }
10
- interface ContainerName {
11
- _?: string[];
1
+ import type { Container, ExtraOptions } from './types.js';
2
+ interface ContainerName extends ExtraOptions {
12
3
  name: string;
13
4
  }
14
- interface AddContainerOptions extends ContainerName {
5
+ interface ContainerUpdateOptions extends ContainerName {
15
6
  image: string;
16
7
  host?: string;
17
8
  volumes?: string;
18
- ports?: string;
19
- }
20
- export declare function addContainer(options: AddContainerOptions): Promise<Container>;
21
- export declare function removeContainer(options: ContainerName): Promise<boolean>;
22
- export declare function getContainer(options: ContainerName): Promise<Container>;
23
- interface UpdateOptions extends ContainerName {
24
- ports?: string;
25
- volumes?: string;
26
- image?: string;
27
- host?: string;
28
- }
29
- export declare function updateContainer(options: UpdateOptions): Promise<Container>;
30
- interface ListOptions {
31
- name?: string;
32
- image?: string;
33
- host?: string;
9
+ port?: string;
34
10
  }
35
- export declare function listContainers(options: ListOptions): Promise<Container[]>;
36
- export declare function findContainer(name: string): Promise<Container | null>;
11
+ type ContainerListOptions = Partial<Container>;
12
+ export declare function addContainer(options: ContainerUpdateOptions): Container;
13
+ export declare function removeContainer(options: ContainerName): boolean;
14
+ export declare function getContainer(options: ContainerName): Container;
15
+ export declare function updateContainer(options: Partial<ContainerUpdateOptions>): Container;
16
+ export declare function listContainers(options?: ContainerListOptions): Container[];
17
+ export declare function findContainer(name: string): Container;
37
18
  export {};
package/dist/store.js CHANGED
@@ -1,43 +1,7 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { Model, Resource, Primary, NotNull, Property, Query } from '@cloud-cli/store';
8
- import { readTargetImage, readTargetName } from './utils';
9
- let Container = class Container extends Resource {
10
- };
11
- __decorate([
12
- Primary(),
13
- NotNull(),
14
- Property(Number)
15
- ], Container.prototype, "id", void 0);
16
- __decorate([
17
- NotNull(),
18
- Property(String)
19
- ], Container.prototype, "name", void 0);
20
- __decorate([
21
- NotNull(),
22
- Property(String)
23
- ], Container.prototype, "image", void 0);
24
- __decorate([
25
- NotNull(),
26
- Property(String)
27
- ], Container.prototype, "host", void 0);
28
- __decorate([
29
- NotNull(),
30
- Property(String)
31
- ], Container.prototype, "ports", void 0);
32
- __decorate([
33
- NotNull(),
34
- Property(String)
35
- ], Container.prototype, "volumes", void 0);
36
- Container = __decorate([
37
- Model('container')
38
- ], Container);
39
- export { Container };
40
- export async function addContainer(options) {
1
+ import { readTargetImage, readTargetName } from './utils.js';
2
+ import { getStorage } from '@cloud-cli/cli';
3
+ const { get, set, has, remove, getAll } = getStorage('dx');
4
+ export function addContainer(options) {
41
5
  readTargetName(options);
42
6
  readTargetImage(options);
43
7
  const { name, image } = options;
@@ -46,36 +10,34 @@ export async function addContainer(options) {
46
10
  if (!image)
47
11
  throw new Error('Image required');
48
12
  const volumes = options.volumes ? sanitiseVolumes(options.volumes) : '';
49
- const ports = options.ports ? sanitisePorts(options.ports) : '';
13
+ const port = options.port || '';
50
14
  const host = options.host;
51
- const container = new Container({ name, image, volumes, ports, host });
52
- const id = await container.save();
53
- container.id = Number(id);
15
+ const container = { name, image, volumes, port, host };
16
+ set(name, container);
54
17
  return container;
55
18
  }
56
- export async function removeContainer(options) {
19
+ export function removeContainer(options) {
57
20
  readTargetName(options);
58
- const found = await findContainer(options.name);
59
- if (found) {
60
- await found.remove();
21
+ if (has(options.name)) {
22
+ remove(options.name);
61
23
  return true;
62
24
  }
63
25
  throw new Error('Container not found: ' + options.name);
64
26
  }
65
- export async function getContainer(options) {
27
+ export function getContainer(options) {
66
28
  readTargetName(options);
67
- return await findContainer(options.name);
29
+ return get(options.name);
68
30
  }
69
31
  const optionSplitter = /,\s*/;
70
- export async function updateContainer(options) {
32
+ export function updateContainer(options) {
71
33
  readTargetName(options);
72
- let { ports, volumes, name, image, host } = options;
73
- const container = await findContainer(name);
34
+ let { port, volumes, name, image, host } = options;
35
+ const container = get(name);
74
36
  if (!container) {
75
37
  throw new Error('Container not found');
76
38
  }
77
- if (ports) {
78
- container.ports = sanitisePorts(ports);
39
+ if (port) {
40
+ container.port = port;
79
41
  }
80
42
  if (volumes) {
81
43
  container.volumes = sanitiseVolumes(volumes);
@@ -86,26 +48,22 @@ export async function updateContainer(options) {
86
48
  if (host) {
87
49
  container.host = host;
88
50
  }
89
- await container.save();
51
+ set(container.name, container);
90
52
  return container;
91
53
  }
92
- export async function listContainers(options) {
93
- const query = new Query();
94
- ['name', 'image', 'host'].forEach((key) => !options[key] || query.where(key).is(options[key]));
95
- const list = await Resource.find(Container, query);
96
- return list.sort((a, b) => Number(a.name > b.name) || -1);
54
+ export function listContainers(options = {}) {
55
+ const keys = ['name', 'image', 'host', 'port', 'volumes'];
56
+ const list = getAll();
57
+ const filtered = keys.reduce((list, key) => {
58
+ if (options[key]) {
59
+ return list.filter((c) => String(c[key]).toLowerCase().includes(options[key].toLowerCase()));
60
+ }
61
+ return list;
62
+ }, list);
63
+ return filtered.sort((a, b) => Number(a.name > b.name) || -1);
97
64
  }
98
- export async function findContainer(name) {
99
- const found = await Resource.find(Container, new Query().where('name').is(name));
100
- return found[0] || null;
101
- }
102
- const portTester = /^\d+:\d+$/;
103
- function sanitisePorts(ports) {
104
- return ports
105
- .split(optionSplitter)
106
- .map((s) => s.trim())
107
- .filter((s) => portTester.test(s))
108
- .join(',');
65
+ export function findContainer(name) {
66
+ return get(name);
109
67
  }
110
68
  const volumeTester = /^\S+:\S+$/;
111
69
  function sanitiseVolumes(volumes) {
@@ -0,0 +1,14 @@
1
+ export interface Config {
2
+ dns?: string;
3
+ dockerArgs?: string[];
4
+ }
5
+ export interface Container {
6
+ name: string;
7
+ image: string;
8
+ host: string;
9
+ port: string;
10
+ volumes: string;
11
+ }
12
+ export interface ExtraOptions {
13
+ _?: string[];
14
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/utils.d.ts CHANGED
@@ -2,10 +2,10 @@ export type EnvList = Array<{
2
2
  key: string;
3
3
  value: string;
4
4
  }>;
5
- export declare function getPorts(ports: string[]): string[];
6
5
  export declare function getEnvVars(vars: EnvList): Promise<{
7
6
  env: {
8
7
  [x: string]: string;
8
+ TZ?: string;
9
9
  };
10
10
  envKeys: string[];
11
11
  }>;
package/dist/utils.js CHANGED
@@ -1,12 +1,4 @@
1
1
  import getPort from 'get-port';
2
- export function getPorts(ports) {
3
- const map = {};
4
- ports.filter(Boolean).forEach((str) => {
5
- const [left, right] = str.split(':');
6
- map[left] = right;
7
- });
8
- return Object.entries(map).map(([left, right]) => '-p' + left + ':' + right);
9
- }
10
2
  export async function getEnvVars(vars) {
11
3
  const port = await getPort();
12
4
  const envKeys = [];
@@ -18,7 +10,7 @@ export async function getEnvVars(vars) {
18
10
  return { env, envKeys: [...new Set(envKeys)] };
19
11
  }
20
12
  export function getListFromString(string) {
21
- return string.trim().split('\n').filter(Boolean);
13
+ return String(string).trim().split('\n').filter(Boolean);
22
14
  }
23
15
  export function addExecFlag(string, flag) {
24
16
  return string
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@cloud-cli/dx",
3
- "version": "1.8.3",
3
+ "version": "2.0.0",
4
4
  "description": "Docker container management",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "type": "module",
8
8
  "prettier": "@cloud-cli/prettier-config",
9
9
  "scripts": {
10
- "test": "jest",
11
- "tdd": "jest --watch",
10
+ "test": "vitest",
12
11
  "build": "rm -rf ./dist/* && tsc"
13
12
  },
14
13
  "keywords": [
@@ -17,10 +16,11 @@
17
16
  "license": "MIT",
18
17
  "author": "Darlan Alves <me@darlanalv.es>",
19
18
  "devDependencies": {
20
- "@cloud-cli/jest-config": "^1.0.0",
21
19
  "@cloud-cli/prettier-config": "^1.0.0",
22
20
  "@cloud-cli/typescript-config": "^1.0.1",
23
- "typescript": "^4.4.2"
21
+ "@types/node": "^20.12.8",
22
+ "typescript": "^5.4.5",
23
+ "vitest": "^1.6.0"
24
24
  },
25
25
  "repository": {
26
26
  "type": "git",
@@ -30,9 +30,8 @@
30
30
  "access": "public"
31
31
  },
32
32
  "dependencies": {
33
- "@cloud-cli/cli": "^1.4.4",
33
+ "@cloud-cli/cli": "^1.9.0",
34
34
  "@cloud-cli/exec": "^1.0.4",
35
- "@cloud-cli/store": "^1.5.1",
36
- "get-port": "^6.1.2"
35
+ "get-port": "^7.1.0"
37
36
  }
38
37
  }
package/cloud.db DELETED
Binary file
package/dist/config.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export interface Config {
2
- dns?: string;
3
- }
4
- export declare function setConfig(newConfig: Config): void;
5
- export declare function getConfig(name: keyof Config): string;
package/dist/config.js DELETED
@@ -1,9 +0,0 @@
1
- const config = {};
2
- export function setConfig(newConfig) {
3
- if (newConfig) {
4
- Object.assign(config, newConfig);
5
- }
6
- }
7
- export function getConfig(name) {
8
- return config[name];
9
- }