@cloud-cli/dc 1.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.
@@ -0,0 +1,10 @@
1
+ name: CICD
2
+ on:
3
+ push:
4
+
5
+ jobs:
6
+ release:
7
+ uses: cloud-cli/workflows/.github/workflows/npm-build-release.yml@main
8
+ secrets:
9
+ NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
10
+ GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # dc
2
+
3
+ Run services with docker compose and [Cloudy](https://github.com/cloud-cli/cli)
4
+
5
+ ## Usage
6
+
7
+ ```sh
8
+ cy dc.update --name demo --content @file:docker-compose-demo.yml
9
+ cy dc.list
10
+ cy dc.start --name demo
11
+ cy dc.stop --name demo
12
+ ```
@@ -0,0 +1,19 @@
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 const _default: {
12
+ [init](): void;
13
+ start: typeof start;
14
+ stop: typeof stop;
15
+ update: typeof update;
16
+ remove: typeof remove;
17
+ list: typeof list;
18
+ };
19
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,71 @@
1
+ import { getConfig, init } from "@cloud-cli/cli";
2
+ import { exec } from "@cloud-cli/exec";
3
+ import { existsSync, mkdirSync } from "node:fs";
4
+ import { 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 getPath = (name) => join(storagePath, name + ".yml");
9
+ const checkName = (name) => {
10
+ if (!name) {
11
+ throw new Error("Name is required");
12
+ }
13
+ // TODO sanitize file name
14
+ return name;
15
+ };
16
+ async function stop(options) {
17
+ const name = checkName(options.name);
18
+ const path = getPath(name);
19
+ if (!existsSync(path)) {
20
+ throw new Error("Service not found");
21
+ }
22
+ const out = await exec("docker compose", ["down", "-f", path]);
23
+ if (out.ok) {
24
+ return true;
25
+ }
26
+ throw new Error(out.stderr);
27
+ }
28
+ async function start(options) {
29
+ const name = checkName(options.name);
30
+ const path = getPath(name);
31
+ if (!existsSync(path)) {
32
+ throw new Error("Service not found");
33
+ }
34
+ const out = await exec("docker compose", ["up", "-f", path]);
35
+ if (out.ok) {
36
+ return true;
37
+ }
38
+ throw new Error(out.stderr);
39
+ }
40
+ async function update(options) {
41
+ const name = checkName(options.name);
42
+ const { content } = options;
43
+ if (!content) {
44
+ throw new Error("docker compose content is required");
45
+ }
46
+ const path = getPath(name);
47
+ await writeFile(path, String(options.content));
48
+ return true;
49
+ }
50
+ async function remove(options) {
51
+ const name = checkName(options.name);
52
+ const path = getPath(name);
53
+ if (existsSync(path)) {
54
+ await rm(path);
55
+ }
56
+ return true;
57
+ }
58
+ async function list() {
59
+ const files = await readdir(storagePath, { withFileTypes: true });
60
+ return files.filter((f) => f.isFile()).map((f) => f.name.replace(".yml", ""));
61
+ }
62
+ export default {
63
+ [init]() {
64
+ mkdirSync(storagePath, { recursive: true });
65
+ },
66
+ start,
67
+ stop,
68
+ update,
69
+ remove,
70
+ list,
71
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@cloud-cli/dc",
3
+ "version": "1.0.0",
4
+ "main": "./dist/index.js",
5
+ "description": "Run services with docker compose",
6
+ "type": "module",
7
+ "types": "./dist/index.d.ts",
8
+ "prettier": "@cloud-cli/prettier-config",
9
+ "scripts": {
10
+ "test": "echo true",
11
+ "build": "rm -rf ./dist/* && tsc"
12
+ },
13
+ "keywords": [
14
+ "cloudy"
15
+ ],
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/cloud-cli/dc.git"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "dependencies": {
25
+ "@cloud-cli/exec": "latest"
26
+ },
27
+ "peerDependencies": {
28
+ "@cloud-cli/cli": "latest"
29
+ },
30
+ "devDependencies": {
31
+ "@cloud-cli/prettier-config": "^1.0.0",
32
+ "@cloud-cli/typescript-config": "^1.0.1",
33
+ "@types/node": "^20.11.30",
34
+ "typescript": "^5.4.3"
35
+ }
36
+ }
package/src/index.ts ADDED
@@ -0,0 +1,97 @@
1
+ import { getConfig, init } from "@cloud-cli/cli";
2
+ import { exec } from "@cloud-cli/exec";
3
+ import { existsSync, mkdirSync } from "node:fs";
4
+ import { readdir, rm, writeFile } from "node:fs/promises";
5
+ import { join } from "node:path";
6
+
7
+ const config = getConfig("dc", { storagePath: "docker-compose" });
8
+ const storagePath = join(process.cwd(), config.storagePath);
9
+
10
+ interface CliOptions {
11
+ name: string;
12
+ content?: string;
13
+ }
14
+
15
+ const getPath = (name) => join(storagePath, name + ".yml");
16
+ const checkName = (name) => {
17
+ if (!name) {
18
+ throw new Error("Name is required");
19
+ }
20
+
21
+ // TODO sanitize file name
22
+ return name;
23
+ };
24
+
25
+ async function stop(options: CliOptions) {
26
+ const name = checkName(options.name);
27
+ const path = getPath(name);
28
+
29
+ if (!existsSync(path)) {
30
+ throw new Error("Service not found");
31
+ }
32
+
33
+ const out = await exec("docker compose", ["down", "-f", path]);
34
+
35
+ if (out.ok) {
36
+ return true;
37
+ }
38
+
39
+ throw new Error(out.stderr);
40
+ }
41
+
42
+ async function start(options: CliOptions) {
43
+ const name = checkName(options.name);
44
+ const path = getPath(name);
45
+
46
+ if (!existsSync(path)) {
47
+ throw new Error("Service not found");
48
+ }
49
+
50
+ const out = await exec("docker compose", ["up", "-f", path]);
51
+
52
+ if (out.ok) {
53
+ return true;
54
+ }
55
+
56
+ throw new Error(out.stderr);
57
+ }
58
+
59
+ async function update(options: CliOptions) {
60
+ const name = checkName(options.name);
61
+ const { content } = options;
62
+
63
+ if (!content) {
64
+ throw new Error("docker compose content is required");
65
+ }
66
+
67
+ const path = getPath(name);
68
+ await writeFile(path, String(options.content));
69
+ return true;
70
+ }
71
+
72
+ async function remove(options: CliOptions) {
73
+ const name = checkName(options.name);
74
+ const path = getPath(name);
75
+
76
+ if (existsSync(path)) {
77
+ await rm(path);
78
+ }
79
+
80
+ return true;
81
+ }
82
+
83
+ async function list() {
84
+ const files = await readdir(storagePath, { withFileTypes: true });
85
+ return files.filter((f) => f.isFile()).map((f) => f.name.replace(".yml", ""));
86
+ }
87
+
88
+ export default {
89
+ [init]() {
90
+ mkdirSync(storagePath, { recursive: true });
91
+ },
92
+ start,
93
+ stop,
94
+ update,
95
+ remove,
96
+ list,
97
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@cloud-cli/typescript-config",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "experimentalDecorators": true
6
+ },
7
+ "include": ["./src/**/*.ts"],
8
+ "exclude": ["**/node_modules/**"]
9
+ }