@cloud-cli/dc 1.1.5 → 1.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dc",
3
- "version": "1.1.5",
3
+ "version": "1.2.0",
4
4
  "main": "./dist/index.js",
5
5
  "description": "Run services with docker compose",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -1,14 +1,14 @@
1
- import { getConfig, init } 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 } 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,10 +35,7 @@ async function stop(options: CliOptions) {
35
35
  throw notFoundError;
36
36
  }
37
37
 
38
- const out = await exec(
39
- binaryAndArgs.binary,
40
- [...binaryAndArgs.args, "-f", path, "down"].filter(Boolean)
41
- );
38
+ const out = await exec(binaryAndArgs.binary, [...binaryAndArgs.args, '-f', path, 'down'].filter(Boolean));
42
39
 
43
40
  if (out.ok) {
44
41
  return true;
@@ -55,10 +52,7 @@ async function start(options: CliOptions) {
55
52
  throw notFoundError;
56
53
  }
57
54
 
58
- const out = await exec(
59
- binaryAndArgs.binary,
60
- [...binaryAndArgs.args, "-f", path, "up", "-d"].filter(Boolean)
61
- );
55
+ const out = await exec(binaryAndArgs.binary, [...binaryAndArgs.args, '-f', path, 'up', '-d'].filter(Boolean));
62
56
 
63
57
  if (out.ok) {
64
58
  return true;
@@ -72,7 +66,7 @@ async function update(options: CliOptions) {
72
66
  const { content } = options;
73
67
 
74
68
  if (!content) {
75
- throw new Error("docker compose content is required");
69
+ throw new Error('docker compose content is required');
76
70
  }
77
71
 
78
72
  const path = getPath(name);
@@ -93,7 +87,7 @@ async function remove(options: CliOptions) {
93
87
 
94
88
  async function list() {
95
89
  const files = await readdir(storagePath, { withFileTypes: true });
96
- 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', ''));
97
91
  }
98
92
 
99
93
  async function show(options: CliOptions) {
@@ -101,7 +95,7 @@ async function show(options: CliOptions) {
101
95
  const path = getPath(name);
102
96
 
103
97
  if (existsSync(path)) {
104
- const content = await readFile(path, "utf-8");
98
+ const content = await readFile(path, 'utf-8');
105
99
  return { name, content };
106
100
  }
107
101
 
@@ -112,22 +106,37 @@ export default {
112
106
  async [init]() {
113
107
  mkdirSync(storagePath, { recursive: true });
114
108
 
115
- const docker = await exec("docker", ["compose"]);
109
+ const docker = await exec('docker', ['compose']);
116
110
  if (docker.ok) {
117
- binaryAndArgs.binary = "docker";
118
- binaryAndArgs.args.push("compose");
111
+ binaryAndArgs.binary = 'docker';
112
+ binaryAndArgs.args.push('compose');
119
113
  return;
120
114
  }
121
115
 
122
- const dockerCompose = await exec("docker-compose", ["--help"]);
116
+ const dockerCompose = await exec('docker-compose', ['--help']);
123
117
  if (dockerCompose.ok) {
124
- binaryAndArgs.binary = "docker-compose";
118
+ binaryAndArgs.binary = 'docker-compose';
125
119
  return;
126
120
  }
127
121
 
128
- binaryAndArgs.binary = "exit";
129
- binaryAndArgs.args.push("1");
122
+ binaryAndArgs.binary = 'exit';
123
+ binaryAndArgs.args.push('1');
130
124
  },
125
+ help: () => ({
126
+ description: 'Manage Docker Compose services',
127
+ commands: {
128
+ 'dc up [name]': 'Start services (optional name to start specific service)',
129
+ 'dc down [name]': 'Stop services (optional name to stop specific service)',
130
+ 'dc list': 'List Compose files/services',
131
+ 'dc show [name]': 'Show content of a Compose file',
132
+ 'dc set [name]': 'Update content of a Compose file',
133
+ 'dc remove [name]': 'Remove a Compose file',
134
+ },
135
+ options: {
136
+ name: 'Service/Compose file name',
137
+ content: 'Docker Compose YAML content (for set command)',
138
+ },
139
+ }),
131
140
  start,
132
141
  stop,
133
142
  update,
package/dist/index.d.ts DELETED
@@ -1,24 +0,0 @@
1
- import { init } 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
- start: typeof start;
18
- stop: typeof stop;
19
- update: typeof update;
20
- remove: typeof remove;
21
- show: typeof show;
22
- list: typeof list;
23
- };
24
- export default _default;
package/dist/index.js DELETED
@@ -1,99 +0,0 @@
1
- import { getConfig, init } 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
- start,
94
- stop,
95
- update,
96
- remove,
97
- show,
98
- list,
99
- };