@dongnanyang/deploy-cli 0.0.0-stage → 0.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/README.md CHANGED
@@ -1,3 +1,73 @@
1
- # Temporary Holding Version
1
+ # Deploy CLI
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 本地 TUI 工具,管理 SSH 隧道和部署脚本。
4
+
5
+ ## 功能
6
+
7
+ - **SSH 隧道** - 在本地开启端口转发,Chat2DB/DBeaver 等工具直连 `localhost:port`
8
+ - **部署脚本** - 自动扫描项目的 `deploy/` 目录,一键执行部署脚本
9
+ - **实时日志** - 隧道事件和部署输出实时显示
10
+
11
+ ## 技术栈
12
+
13
+ - [vue-tui](https://github.com/vuejs-ai/vue-tui) - Vue 3 终端 UI 框架
14
+ - [ssh2](https://github.com/mscdex/ssh2) - SSH 客户端
15
+ - Vite HMR 开发 + tsdown 生产构建
16
+
17
+ ## 快速开始
18
+
19
+ ```bash
20
+ npm install
21
+ npm run dev # 开发模式 (HMR)
22
+ npm run build # 生产构建
23
+ npm start # 运行生产构建
24
+ ```
25
+
26
+ ## 配置
27
+
28
+ 编辑 `~/.deploy-cli/config.json`:
29
+
30
+ ```json
31
+ {
32
+ "servers": [
33
+ {
34
+ "name": "prod",
35
+ "host": "example.com",
36
+ "port": 22,
37
+ "username": "ubuntu",
38
+ "privateKey": "~/.ssh/id_rsa"
39
+ }
40
+ ],
41
+ "tunnels": [
42
+ {
43
+ "name": "MySQL",
44
+ "server": "prod",
45
+ "localPort": 3306,
46
+ "remoteHost": "127.0.0.1",
47
+ "remotePort": 3306,
48
+ "autoStart": false
49
+ }
50
+ ],
51
+ "projects": [
52
+ {
53
+ "name": "my-app",
54
+ "path": "~/work/my-app",
55
+ "deployDir": "deploy",
56
+ "server": "prod"
57
+ }
58
+ ]
59
+ }
60
+ ```
61
+
62
+ ## 按键
63
+
64
+ | 键 | 作用 |
65
+ |---|---|
66
+ | `j`/`k` | 上下选择 |
67
+ | `s` | 启动隧道 |
68
+ | `x` | 停止隧道 |
69
+ | `a` | 启动全部隧道 |
70
+ | `Enter` | 执行部署脚本 |
71
+ | `Tab` | 切换面板焦点 |
72
+ | `1`/`2`/`3` | 跳转到 Tunnels/Deploy/Output |
73
+ | `q` | 退出 |
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import '../dist/bin.js';
package/dist/bin.js ADDED
@@ -0,0 +1,9 @@
1
+ //#region src/bin.ts
2
+ const argv = process.argv.slice(2);
3
+ if (argv.length === 0 || argv[0] === "tui") await import("./main.js");
4
+ else {
5
+ const cli = await import("./cli-g63_QDLc.js");
6
+ process.exitCode = await cli.main(argv);
7
+ }
8
+ //#endregion
9
+ export {};
@@ -0,0 +1,193 @@
1
+ import { accessSync, constants } from "node:fs";
2
+ import { spawn } from "node:child_process";
3
+ //#region src/core/exec.ts
4
+ /** 仅 spawn 失败(ENOENT)与超时抛出 */
5
+ var ExecError = class extends Error {
6
+ result;
7
+ constructor(message, result) {
8
+ super(message);
9
+ this.name = "ExecError";
10
+ this.result = result;
11
+ }
12
+ };
13
+ /** 超时 SIGTERM 后等待子进程退出的宽限时长,超过则 SIGKILL */
14
+ const KILL_GRACE_MS = 3e3;
15
+ /** 按 \n 切行缓冲,结束时补发无结尾换行的残行 */
16
+ function createLineBuffer(stream, onLine) {
17
+ let buffer = "";
18
+ let text = "";
19
+ /** 回调抛异常跳过该次调用即可,不得影响数据捕获与后续行回调 */
20
+ const emit = (line) => {
21
+ try {
22
+ onLine?.(line, stream);
23
+ } catch {}
24
+ };
25
+ return {
26
+ push(chunk) {
27
+ text += chunk;
28
+ buffer += chunk;
29
+ let index = buffer.indexOf("\n");
30
+ while (index !== -1) {
31
+ const line = buffer.slice(0, index);
32
+ buffer = buffer.slice(index + 1);
33
+ emit(line);
34
+ index = buffer.indexOf("\n");
35
+ }
36
+ },
37
+ /** flush 残行并返回累计全文 */
38
+ flush() {
39
+ if (buffer.length > 0) {
40
+ emit(buffer);
41
+ buffer = "";
42
+ }
43
+ return text;
44
+ }
45
+ };
46
+ }
47
+ /** 执行命令并捕获退出码与输出 */
48
+ async function run(cmd, args, opts = {}) {
49
+ const cmdLine = [cmd, ...args].join(" ");
50
+ return new Promise((resolve, reject) => {
51
+ const child = spawn(cmd, args, {
52
+ cwd: opts.cwd,
53
+ env: opts.env,
54
+ stdio: [
55
+ "ignore",
56
+ "pipe",
57
+ "pipe"
58
+ ]
59
+ });
60
+ const out = createLineBuffer("stdout", opts.onLine);
61
+ const err = createLineBuffer("stderr", opts.onLine);
62
+ let settled = false;
63
+ let timedOut = false;
64
+ let timeoutNote = "";
65
+ let killTimer;
66
+ const timeoutTimer = opts.timeoutMs ? setTimeout(() => {
67
+ timedOut = true;
68
+ timeoutNote = `timeout: ${cmdLine}\n`;
69
+ child.kill("SIGTERM");
70
+ killTimer = setTimeout(() => child.kill("SIGKILL"), KILL_GRACE_MS);
71
+ }, opts.timeoutMs) : void 0;
72
+ child.stdout?.setEncoding("utf-8");
73
+ child.stderr?.setEncoding("utf-8");
74
+ child.stdout?.on("data", (chunk) => out.push(chunk));
75
+ child.stderr?.on("data", (chunk) => err.push(chunk));
76
+ child.on("error", (error) => {
77
+ if (settled) return;
78
+ settled = true;
79
+ clearTimeout(timeoutTimer);
80
+ clearTimeout(killTimer);
81
+ const message = `spawn 失败: ${cmdLine}: ${error.message}`;
82
+ reject(new ExecError(message, {
83
+ code: 127,
84
+ stdout: out.flush(),
85
+ stderr: `${message}\n`
86
+ }));
87
+ });
88
+ child.on("close", (code) => {
89
+ if (settled) return;
90
+ settled = true;
91
+ clearTimeout(timeoutTimer);
92
+ clearTimeout(killTimer);
93
+ const result = {
94
+ code: code ?? (timedOut ? 124 : 1),
95
+ stdout: out.flush(),
96
+ stderr: err.flush() + timeoutNote
97
+ };
98
+ if (timedOut) {
99
+ reject(new ExecError(`timeout: ${cmdLine}`, result));
100
+ return;
101
+ }
102
+ resolve(result);
103
+ });
104
+ });
105
+ }
106
+ //#endregion
107
+ //#region src/cli/commands/doctor.ts
108
+ const SITES_AVAILABLE = "/etc/nginx/sites-available";
109
+ const MIN_NODE_MAJOR = 20;
110
+ /** 单项检查超时,防 docker/nginx 卡死挂死 doctor */
111
+ const PROBE_TIMEOUT_MS = 1e4;
112
+ async function probe(runner, cmd, args) {
113
+ try {
114
+ const result = await runner(cmd, args, { timeoutMs: PROBE_TIMEOUT_MS });
115
+ const detail = (result.code === 0 ? result.stdout || result.stderr : result.stderr || result.stdout).trim().split("\n")[0] ?? "";
116
+ return {
117
+ ok: result.code === 0,
118
+ detail
119
+ };
120
+ } catch (error) {
121
+ return {
122
+ ok: false,
123
+ detail: error instanceof Error ? error.message : String(error)
124
+ };
125
+ }
126
+ }
127
+ function defaultCanWrite(path) {
128
+ try {
129
+ accessSync(path, constants.W_OK);
130
+ return true;
131
+ } catch {
132
+ return false;
133
+ }
134
+ }
135
+ /** 环境自检:返回退出码(存在失败项 1,否则 0;警告不影响退出码) */
136
+ async function doctor(options = {}) {
137
+ const runner = options.runner ?? run;
138
+ const log = options.log ?? ((line) => console.log(line));
139
+ const canWrite = options.canWrite ?? defaultCanWrite;
140
+ let failed = false;
141
+ const pass = (line) => log(`[✓] ${line}`);
142
+ const fail = (line) => {
143
+ failed = true;
144
+ log(`[✗] ${line}`);
145
+ };
146
+ const warn = (line) => log(`[警告] ${line}`);
147
+ log("deploy-cli doctor 环境自检");
148
+ if (Number(process.versions.node.split(".")[0]) >= MIN_NODE_MAJOR) pass(`Node.js v${process.versions.node}`);
149
+ else fail(`Node.js v${process.versions.node} 过低,需要 >= ${MIN_NODE_MAJOR}`);
150
+ const dockerVersion = await probe(runner, "docker", ["--version"]);
151
+ if (!dockerVersion.ok) fail(`docker 不可用(docker --version 失败): ${dockerVersion.detail}`);
152
+ else {
153
+ pass(`docker 可用(${dockerVersion.detail})`);
154
+ const dockerInfo = await probe(runner, "docker", ["info"]);
155
+ if (dockerInfo.ok) pass("docker daemon 运行中");
156
+ else fail(`docker daemon 未运行(docker info 失败): ${dockerInfo.detail}`);
157
+ }
158
+ const nginx = await probe(runner, "nginx", ["-v"]);
159
+ if (nginx.ok) pass(`nginx 可用(${nginx.detail})`);
160
+ else fail(`nginx 不可用(nginx -v 失败): ${nginx.detail}`);
161
+ if (canWrite(SITES_AVAILABLE)) pass(`${SITES_AVAILABLE} 可写`);
162
+ else fail(`${SITES_AVAILABLE} 不可写`);
163
+ const acme = await probe(runner, "acme.sh", ["--version"]);
164
+ const certbot = await probe(runner, "certbot", ["--version"]);
165
+ const certTools = [acme.ok && "acme.sh", certbot.ok && "certbot"].filter(Boolean).join(" / ");
166
+ if (certTools) pass(`证书工具可用(${certTools})`);
167
+ else warn("未检测到 acme.sh / certbot,HTTPS 证书签发/续期不可用(仅警告,不影响退出码)");
168
+ return failed ? 1 : 0;
169
+ }
170
+ //#endregion
171
+ //#region src/cli/index.ts
172
+ const USAGE = `用法:
173
+ deploy-cli 启动交互式 TUI
174
+ deploy-cli tui 启动交互式 TUI
175
+ deploy-cli doctor 环境自检`;
176
+ /** CLI 子命令分发,返回退出码 */
177
+ async function main(argv) {
178
+ const [command] = argv;
179
+ switch (command) {
180
+ case "doctor": return await doctor();
181
+ case "help":
182
+ case "--help":
183
+ case "-h":
184
+ console.log(USAGE);
185
+ return 0;
186
+ default:
187
+ console.error(`未知命令: ${command ?? ""}`);
188
+ console.error(USAGE);
189
+ return 1;
190
+ }
191
+ }
192
+ //#endregion
193
+ export { main };