@cloud-cli/dc 1.3.0 → 1.3.1

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.
@@ -0,0 +1,25 @@
1
+ import { init, help } from "@cloud-cli/cli";
2
+ interface CliOptions {
3
+ name: string;
4
+ content?: string;
5
+ }
6
+ declare function stop(options: CliOptions): Promise<boolean>;
7
+ declare function start(options: CliOptions): Promise<boolean>;
8
+ declare function update(options: CliOptions): Promise<boolean>;
9
+ declare function remove(options: CliOptions): Promise<boolean>;
10
+ declare function list(): Promise<string[]>;
11
+ declare function show(options: CliOptions): Promise<{
12
+ name: any;
13
+ content: string;
14
+ }>;
15
+ declare const _default: {
16
+ [init](): Promise<void>;
17
+ [help]: () => string;
18
+ start: typeof start;
19
+ stop: typeof stop;
20
+ update: typeof update;
21
+ remove: typeof remove;
22
+ show: typeof show;
23
+ list: typeof list;
24
+ };
25
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,112 @@
1
+ import { getConfig, init, help } from "@cloud-cli/cli";
2
+ import { exec } from "@cloud-cli/exec";
3
+ import { existsSync, mkdirSync } from "node:fs";
4
+ import { readFile, readdir, rm, writeFile } from "node:fs/promises";
5
+ import { join } from "node:path";
6
+ const config = getConfig("dc", { storagePath: "docker-compose" });
7
+ const storagePath = join(process.cwd(), config.storagePath);
8
+ const notFoundError = new Error("Service not found");
9
+ const binaryAndArgs = {
10
+ binary: "",
11
+ args: [],
12
+ };
13
+ const getPath = (name) => join(storagePath, name + ".yml");
14
+ const checkName = (name) => {
15
+ if (!name) {
16
+ throw new Error("Name is required");
17
+ }
18
+ // TODO sanitize file name
19
+ return name;
20
+ };
21
+ async function stop(options) {
22
+ const name = checkName(options.name);
23
+ const path = getPath(name);
24
+ if (!existsSync(path)) {
25
+ throw notFoundError;
26
+ }
27
+ const out = await exec(binaryAndArgs.binary, [...binaryAndArgs.args, "-f", path, "down"].filter(Boolean));
28
+ if (out.ok) {
29
+ return true;
30
+ }
31
+ throw new Error(out.stderr);
32
+ }
33
+ async function start(options) {
34
+ const name = checkName(options.name);
35
+ const path = getPath(name);
36
+ if (!existsSync(path)) {
37
+ throw notFoundError;
38
+ }
39
+ const out = await exec(binaryAndArgs.binary, [...binaryAndArgs.args, "-f", path, "up", "-d"].filter(Boolean));
40
+ if (out.ok) {
41
+ return true;
42
+ }
43
+ throw new Error(out.stderr);
44
+ }
45
+ async function update(options) {
46
+ const name = checkName(options.name);
47
+ const { content } = options;
48
+ if (!content) {
49
+ throw new Error("docker compose content is required");
50
+ }
51
+ const path = getPath(name);
52
+ await writeFile(path, String(options.content));
53
+ return true;
54
+ }
55
+ async function remove(options) {
56
+ const name = checkName(options.name);
57
+ const path = getPath(name);
58
+ if (existsSync(path)) {
59
+ await rm(path);
60
+ }
61
+ return true;
62
+ }
63
+ async function list() {
64
+ const files = await readdir(storagePath, { withFileTypes: true });
65
+ return files.filter((f) => f.isFile()).map((f) => f.name.replace(".yml", ""));
66
+ }
67
+ async function show(options) {
68
+ const name = checkName(options.name);
69
+ const path = getPath(name);
70
+ if (existsSync(path)) {
71
+ const content = await readFile(path, "utf-8");
72
+ return { name, content };
73
+ }
74
+ throw notFoundError;
75
+ }
76
+ export default {
77
+ async [init]() {
78
+ mkdirSync(storagePath, { recursive: true });
79
+ const docker = await exec("docker", ["compose"]);
80
+ if (docker.ok) {
81
+ binaryAndArgs.binary = "docker";
82
+ binaryAndArgs.args.push("compose");
83
+ return;
84
+ }
85
+ const dockerCompose = await exec("docker-compose", ["--help"]);
86
+ if (dockerCompose.ok) {
87
+ binaryAndArgs.binary = "docker-compose";
88
+ return;
89
+ }
90
+ binaryAndArgs.binary = "exit";
91
+ binaryAndArgs.args.push("1");
92
+ },
93
+ [help]: () => `Manage Docker Compose services
94
+
95
+ Available commands:
96
+ dc.start --name <name> - Start a Compose service
97
+ dc.stop --name <name> - Stop a Compose service
98
+ dc.update --name <name> --content <yaml> - Create or update a Compose file
99
+ dc.remove --name <name> - Remove a Compose file
100
+ dc.show --name <name> - Show a Compose file
101
+ dc.list - List Compose files/services
102
+
103
+ Options:
104
+ name - Service/Compose file name
105
+ content - Docker Compose YAML content (for update command)`,
106
+ start,
107
+ stop,
108
+ update,
109
+ remove,
110
+ show,
111
+ list,
112
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dc",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "main": "./dist/index.js",
5
5
  "description": "Run services with docker compose",
6
6
  "type": "module",
@@ -25,7 +25,7 @@
25
25
  "@cloud-cli/exec": "latest"
26
26
  },
27
27
  "peerDependencies": {
28
- "@cloud-cli/cli": "latest"
28
+ "@cloud-cli/cli": "^2.2.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@cloud-cli/prettier-config": "^1.0.0",
package/src/index.ts CHANGED
@@ -1,14 +1,14 @@
1
- import { getConfig, init, help } from '@cloud-cli/cli';
2
- import { exec } from '@cloud-cli/exec';
3
- import { existsSync, mkdirSync } from 'node:fs';
4
- import { readFile, readdir, rm, writeFile } from 'node:fs/promises';
5
- import { join } from 'node:path';
1
+ import { getConfig, init, help } from "@cloud-cli/cli";
2
+ import { exec } from "@cloud-cli/exec";
3
+ import { existsSync, mkdirSync } from "node:fs";
4
+ import { readFile, readdir, rm, writeFile } from "node:fs/promises";
5
+ import { join } from "node:path";
6
6
 
7
- const config = getConfig('dc', { storagePath: 'docker-compose' });
7
+ const config = getConfig("dc", { storagePath: "docker-compose" });
8
8
  const storagePath = join(process.cwd(), config.storagePath);
9
- const notFoundError = new Error('Service not found');
9
+ const notFoundError = new Error("Service not found");
10
10
  const binaryAndArgs = {
11
- binary: '',
11
+ binary: "",
12
12
  args: [],
13
13
  };
14
14
 
@@ -17,10 +17,10 @@ interface CliOptions {
17
17
  content?: string;
18
18
  }
19
19
 
20
- const getPath = (name) => join(storagePath, name + '.yml');
20
+ const getPath = (name) => join(storagePath, name + ".yml");
21
21
  const checkName = (name) => {
22
22
  if (!name) {
23
- throw new Error('Name is required');
23
+ throw new Error("Name is required");
24
24
  }
25
25
 
26
26
  // TODO sanitize file name
@@ -35,7 +35,7 @@ async function stop(options: CliOptions) {
35
35
  throw notFoundError;
36
36
  }
37
37
 
38
- const out = await exec(binaryAndArgs.binary, [...binaryAndArgs.args, '-f', path, 'down'].filter(Boolean));
38
+ const out = await exec(binaryAndArgs.binary, [...binaryAndArgs.args, "-f", path, "down"].filter(Boolean));
39
39
 
40
40
  if (out.ok) {
41
41
  return true;
@@ -52,7 +52,7 @@ async function start(options: CliOptions) {
52
52
  throw notFoundError;
53
53
  }
54
54
 
55
- const out = await exec(binaryAndArgs.binary, [...binaryAndArgs.args, '-f', path, 'up', '-d'].filter(Boolean));
55
+ const out = await exec(binaryAndArgs.binary, [...binaryAndArgs.args, "-f", path, "up", "-d"].filter(Boolean));
56
56
 
57
57
  if (out.ok) {
58
58
  return true;
@@ -66,7 +66,7 @@ async function update(options: CliOptions) {
66
66
  const { content } = options;
67
67
 
68
68
  if (!content) {
69
- throw new Error('docker compose content is required');
69
+ throw new Error("docker compose content is required");
70
70
  }
71
71
 
72
72
  const path = getPath(name);
@@ -87,7 +87,7 @@ async function remove(options: CliOptions) {
87
87
 
88
88
  async function list() {
89
89
  const files = await readdir(storagePath, { withFileTypes: true });
90
- return files.filter((f) => f.isFile()).map((f) => f.name.replace('.yml', ''));
90
+ return files.filter((f) => f.isFile()).map((f) => f.name.replace(".yml", ""));
91
91
  }
92
92
 
93
93
  async function show(options: CliOptions) {
@@ -95,7 +95,7 @@ async function show(options: CliOptions) {
95
95
  const path = getPath(name);
96
96
 
97
97
  if (existsSync(path)) {
98
- const content = await readFile(path, 'utf-8');
98
+ const content = await readFile(path, "utf-8");
99
99
  return { name, content };
100
100
  }
101
101
 
@@ -106,31 +106,31 @@ export default {
106
106
  async [init]() {
107
107
  mkdirSync(storagePath, { recursive: true });
108
108
 
109
- const docker = await exec('docker', ['compose']);
109
+ const docker = await exec("docker", ["compose"]);
110
110
  if (docker.ok) {
111
- binaryAndArgs.binary = 'docker';
112
- binaryAndArgs.args.push('compose');
111
+ binaryAndArgs.binary = "docker";
112
+ binaryAndArgs.args.push("compose");
113
113
  return;
114
114
  }
115
115
 
116
- const dockerCompose = await exec('docker-compose', ['--help']);
116
+ const dockerCompose = await exec("docker-compose", ["--help"]);
117
117
  if (dockerCompose.ok) {
118
- binaryAndArgs.binary = 'docker-compose';
118
+ binaryAndArgs.binary = "docker-compose";
119
119
  return;
120
120
  }
121
121
 
122
- binaryAndArgs.binary = 'exit';
123
- binaryAndArgs.args.push('1');
122
+ binaryAndArgs.binary = "exit";
123
+ binaryAndArgs.args.push("1");
124
124
  },
125
125
  [help]: () => `Manage Docker Compose services
126
126
 
127
127
  Available commands:
128
- start - Start services (optional name to start specific service)
129
- stop - Stop services (optional name to stop specific service)
130
- update - Update Docker Compose services
131
- remove - Remove a Compose file
132
- show - Show content of a Compose file
133
- list - List Compose files/services
128
+ dc.start --name <name> - Start a Compose service
129
+ dc.stop --name <name> - Stop a Compose service
130
+ dc.update --name <name> --content <yaml> - Create or update a Compose file
131
+ dc.remove --name <name> - Remove a Compose file
132
+ dc.show --name <name> - Show a Compose file
133
+ dc.list - List Compose files/services
134
134
 
135
135
  Options:
136
136
  name - Service/Compose file name