@dongnanyang/deploy-cli 0.2.0 → 0.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.
- package/dist/bin.js +1 -1
- package/dist/cli-BmCyXTVr.js +437 -0
- package/dist/config-CVqFEtQB.js +65 -0
- package/dist/main.js +2 -63
- package/package.json +1 -1
- package/dist/cli-g63_QDLc.js +0 -193
package/dist/bin.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const argv = process.argv.slice(2);
|
|
3
3
|
if (argv.length === 0 || argv[0] === "tui") await import("./main.js");
|
|
4
4
|
else {
|
|
5
|
-
const cli = await import("./cli-
|
|
5
|
+
const cli = await import("./cli-BmCyXTVr.js");
|
|
6
6
|
process.exitCode = await cli.main(argv);
|
|
7
7
|
}
|
|
8
8
|
//#endregion
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import { n as loadConfig } from "./config-CVqFEtQB.js";
|
|
2
|
+
import { accessSync, constants } from "node:fs";
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
//#region src/core/exec.ts
|
|
5
|
+
/** 仅 spawn 失败(ENOENT)与超时抛出 */
|
|
6
|
+
var ExecError = class extends Error {
|
|
7
|
+
result;
|
|
8
|
+
constructor(message, result) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "ExecError";
|
|
11
|
+
this.result = result;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
/** 超时 SIGTERM 后等待子进程退出的宽限时长,超过则 SIGKILL */
|
|
15
|
+
const KILL_GRACE_MS = 3e3;
|
|
16
|
+
/** 按 \n 切行缓冲,结束时补发无结尾换行的残行 */
|
|
17
|
+
function createLineBuffer(stream, onLine) {
|
|
18
|
+
let buffer = "";
|
|
19
|
+
let text = "";
|
|
20
|
+
/** 回调抛异常跳过该次调用即可,不得影响数据捕获与后续行回调 */
|
|
21
|
+
const emit = (line) => {
|
|
22
|
+
try {
|
|
23
|
+
onLine?.(line, stream);
|
|
24
|
+
} catch {}
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
push(chunk) {
|
|
28
|
+
text += chunk;
|
|
29
|
+
buffer += chunk;
|
|
30
|
+
let index = buffer.indexOf("\n");
|
|
31
|
+
while (index !== -1) {
|
|
32
|
+
const line = buffer.slice(0, index);
|
|
33
|
+
buffer = buffer.slice(index + 1);
|
|
34
|
+
emit(line);
|
|
35
|
+
index = buffer.indexOf("\n");
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
/** flush 残行并返回累计全文 */
|
|
39
|
+
flush() {
|
|
40
|
+
if (buffer.length > 0) {
|
|
41
|
+
emit(buffer);
|
|
42
|
+
buffer = "";
|
|
43
|
+
}
|
|
44
|
+
return text;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** 执行命令并捕获退出码与输出 */
|
|
49
|
+
async function run(cmd, args, opts = {}) {
|
|
50
|
+
const cmdLine = [cmd, ...args].join(" ");
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
const child = spawn(cmd, args, {
|
|
53
|
+
cwd: opts.cwd,
|
|
54
|
+
env: opts.env,
|
|
55
|
+
stdio: [
|
|
56
|
+
"ignore",
|
|
57
|
+
"pipe",
|
|
58
|
+
"pipe"
|
|
59
|
+
]
|
|
60
|
+
});
|
|
61
|
+
const out = createLineBuffer("stdout", opts.onLine);
|
|
62
|
+
const err = createLineBuffer("stderr", opts.onLine);
|
|
63
|
+
let settled = false;
|
|
64
|
+
let timedOut = false;
|
|
65
|
+
let timeoutNote = "";
|
|
66
|
+
let killTimer;
|
|
67
|
+
const timeoutTimer = opts.timeoutMs ? setTimeout(() => {
|
|
68
|
+
timedOut = true;
|
|
69
|
+
timeoutNote = `timeout: ${cmdLine}\n`;
|
|
70
|
+
child.kill("SIGTERM");
|
|
71
|
+
killTimer = setTimeout(() => child.kill("SIGKILL"), KILL_GRACE_MS);
|
|
72
|
+
}, opts.timeoutMs) : void 0;
|
|
73
|
+
child.stdout?.setEncoding("utf-8");
|
|
74
|
+
child.stderr?.setEncoding("utf-8");
|
|
75
|
+
child.stdout?.on("data", (chunk) => out.push(chunk));
|
|
76
|
+
child.stderr?.on("data", (chunk) => err.push(chunk));
|
|
77
|
+
child.on("error", (error) => {
|
|
78
|
+
if (settled) return;
|
|
79
|
+
settled = true;
|
|
80
|
+
clearTimeout(timeoutTimer);
|
|
81
|
+
clearTimeout(killTimer);
|
|
82
|
+
const message = `spawn 失败: ${cmdLine}: ${error.message}`;
|
|
83
|
+
reject(new ExecError(message, {
|
|
84
|
+
code: 127,
|
|
85
|
+
stdout: out.flush(),
|
|
86
|
+
stderr: `${message}\n`
|
|
87
|
+
}));
|
|
88
|
+
});
|
|
89
|
+
child.on("close", (code) => {
|
|
90
|
+
if (settled) return;
|
|
91
|
+
settled = true;
|
|
92
|
+
clearTimeout(timeoutTimer);
|
|
93
|
+
clearTimeout(killTimer);
|
|
94
|
+
const result = {
|
|
95
|
+
code: code ?? (timedOut ? 124 : 1),
|
|
96
|
+
stdout: out.flush(),
|
|
97
|
+
stderr: err.flush() + timeoutNote
|
|
98
|
+
};
|
|
99
|
+
if (timedOut) {
|
|
100
|
+
reject(new ExecError(`timeout: ${cmdLine}`, result));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
resolve(result);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/cli/commands/doctor.ts
|
|
109
|
+
const SITES_AVAILABLE = "/etc/nginx/sites-available";
|
|
110
|
+
const MIN_NODE_MAJOR = 20;
|
|
111
|
+
/** 单项检查超时,防 docker/nginx 卡死挂死 doctor */
|
|
112
|
+
const PROBE_TIMEOUT_MS = 1e4;
|
|
113
|
+
async function probe(runner, cmd, args) {
|
|
114
|
+
try {
|
|
115
|
+
const result = await runner(cmd, args, { timeoutMs: PROBE_TIMEOUT_MS });
|
|
116
|
+
const detail = (result.code === 0 ? result.stdout || result.stderr : result.stderr || result.stdout).trim().split("\n")[0] ?? "";
|
|
117
|
+
return {
|
|
118
|
+
ok: result.code === 0,
|
|
119
|
+
detail
|
|
120
|
+
};
|
|
121
|
+
} catch (error) {
|
|
122
|
+
return {
|
|
123
|
+
ok: false,
|
|
124
|
+
detail: error instanceof Error ? error.message : String(error)
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function defaultCanWrite(path) {
|
|
129
|
+
try {
|
|
130
|
+
accessSync(path, constants.W_OK);
|
|
131
|
+
return true;
|
|
132
|
+
} catch {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/** 环境自检:返回退出码(存在失败项 1,否则 0;警告不影响退出码) */
|
|
137
|
+
async function doctor(options = {}) {
|
|
138
|
+
const runner = options.runner ?? run;
|
|
139
|
+
const log = options.log ?? ((line) => console.log(line));
|
|
140
|
+
const canWrite = options.canWrite ?? defaultCanWrite;
|
|
141
|
+
let failed = false;
|
|
142
|
+
const pass = (line) => log(`[✓] ${line}`);
|
|
143
|
+
const fail = (line) => {
|
|
144
|
+
failed = true;
|
|
145
|
+
log(`[✗] ${line}`);
|
|
146
|
+
};
|
|
147
|
+
const warn = (line) => log(`[警告] ${line}`);
|
|
148
|
+
log("deploy-cli doctor 环境自检");
|
|
149
|
+
if (Number(process.versions.node.split(".")[0]) >= MIN_NODE_MAJOR) pass(`Node.js v${process.versions.node}`);
|
|
150
|
+
else fail(`Node.js v${process.versions.node} 过低,需要 >= ${MIN_NODE_MAJOR}`);
|
|
151
|
+
const dockerVersion = await probe(runner, "docker", ["--version"]);
|
|
152
|
+
if (!dockerVersion.ok) fail(`docker 不可用(docker --version 失败): ${dockerVersion.detail}`);
|
|
153
|
+
else {
|
|
154
|
+
pass(`docker 可用(${dockerVersion.detail})`);
|
|
155
|
+
const dockerInfo = await probe(runner, "docker", ["info"]);
|
|
156
|
+
if (dockerInfo.ok) pass("docker daemon 运行中");
|
|
157
|
+
else fail(`docker daemon 未运行(docker info 失败): ${dockerInfo.detail}`);
|
|
158
|
+
}
|
|
159
|
+
const nginx = await probe(runner, "nginx", ["-v"]);
|
|
160
|
+
if (nginx.ok) pass(`nginx 可用(${nginx.detail})`);
|
|
161
|
+
else fail(`nginx 不可用(nginx -v 失败): ${nginx.detail}`);
|
|
162
|
+
if (canWrite(SITES_AVAILABLE)) pass(`${SITES_AVAILABLE} 可写`);
|
|
163
|
+
else fail(`${SITES_AVAILABLE} 不可写`);
|
|
164
|
+
const acme = await probe(runner, "acme.sh", ["--version"]);
|
|
165
|
+
const certbot = await probe(runner, "certbot", ["--version"]);
|
|
166
|
+
const certTools = [acme.ok && "acme.sh", certbot.ok && "certbot"].filter(Boolean).join(" / ");
|
|
167
|
+
if (certTools) pass(`证书工具可用(${certTools})`);
|
|
168
|
+
else warn("未检测到 acme.sh / certbot,HTTPS 证书签发/续期不可用(仅警告,不影响退出码)");
|
|
169
|
+
return failed ? 1 : 0;
|
|
170
|
+
}
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/core/docker.ts
|
|
173
|
+
/** docker logs 的完整参数(CLI 报错展示与实际执行共用同一构造,保证命令原文一致) */
|
|
174
|
+
function logsArgs(idOrName, o) {
|
|
175
|
+
const args = ["logs"];
|
|
176
|
+
if (o?.tail !== void 0) args.push("--tail", String(o.tail));
|
|
177
|
+
if (o?.follow) args.push("--follow");
|
|
178
|
+
args.push(idOrName);
|
|
179
|
+
return args;
|
|
180
|
+
}
|
|
181
|
+
/** 失败信息:命令原文 + exit 码 + stderr 摘要 */
|
|
182
|
+
function execFailureMessage(cmd, args, result) {
|
|
183
|
+
const summary = result.stderr.trim().split("\n").filter((line) => line.trim() !== "").slice(0, 3).join(" ");
|
|
184
|
+
return `命令失败: ${[cmd, ...args].join(" ")}(exit ${result.code})${summary ? `: ${summary}` : "(stderr 无输出)"}`;
|
|
185
|
+
}
|
|
186
|
+
/** 把 Runner 的 (line, stream) 回调适配为 DockerApi 的 (l) => void */
|
|
187
|
+
function adaptOnLine(onLine) {
|
|
188
|
+
return onLine ? (line) => onLine(line) : void 0;
|
|
189
|
+
}
|
|
190
|
+
/** docker ps 一行 json → ContainerInfo;兼容真实 docker 的 ID 大写键与 brief 的 Id */
|
|
191
|
+
function parseContainerLine(line) {
|
|
192
|
+
let raw;
|
|
193
|
+
try {
|
|
194
|
+
raw = JSON.parse(line);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
197
|
+
throw new Error(`解析 docker ps 输出失败: ${line}: ${reason}`);
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
id: String(raw.ID ?? raw.Id ?? ""),
|
|
201
|
+
name: String(raw.Names ?? ""),
|
|
202
|
+
image: String(raw.Image ?? ""),
|
|
203
|
+
state: String(raw.State ?? ""),
|
|
204
|
+
status: String(raw.Status ?? ""),
|
|
205
|
+
ports: String(raw.Ports ?? "")
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/** 以 shell-out 方式实现的 docker 操作集合 */
|
|
209
|
+
function createDocker(runner = run) {
|
|
210
|
+
return {
|
|
211
|
+
async listContainers(o) {
|
|
212
|
+
const args = ["ps"];
|
|
213
|
+
if (o?.all) args.push("-a");
|
|
214
|
+
if (o?.name) args.push("--filter", `name=${o.name}`);
|
|
215
|
+
args.push("--format", "{{json .}}");
|
|
216
|
+
const result = await runner("docker", args);
|
|
217
|
+
if (result.code !== 0) throw new Error(execFailureMessage("docker", args, result));
|
|
218
|
+
return result.stdout.split("\n").filter((line) => line.trim() !== "").map(parseContainerLine);
|
|
219
|
+
},
|
|
220
|
+
async logs(idOrName, o) {
|
|
221
|
+
return await runner("docker", logsArgs(idOrName, o), { onLine: adaptOnLine(o?.onLine) });
|
|
222
|
+
},
|
|
223
|
+
async composeUp(cwd, composeFile, o) {
|
|
224
|
+
const args = [
|
|
225
|
+
"compose",
|
|
226
|
+
"-f",
|
|
227
|
+
composeFile,
|
|
228
|
+
"up",
|
|
229
|
+
"-d"
|
|
230
|
+
];
|
|
231
|
+
if (o?.build) args.push("--build");
|
|
232
|
+
return await runner("docker", args, {
|
|
233
|
+
cwd,
|
|
234
|
+
onLine: adaptOnLine(o?.onLine)
|
|
235
|
+
});
|
|
236
|
+
},
|
|
237
|
+
async composeDown(cwd, composeFile, o) {
|
|
238
|
+
return await runner("docker", [
|
|
239
|
+
"compose",
|
|
240
|
+
"-f",
|
|
241
|
+
composeFile,
|
|
242
|
+
"down"
|
|
243
|
+
], {
|
|
244
|
+
cwd,
|
|
245
|
+
onLine: adaptOnLine(o?.onLine)
|
|
246
|
+
});
|
|
247
|
+
},
|
|
248
|
+
async composeBuild(cwd, composeFile, o) {
|
|
249
|
+
return await runner("docker", [
|
|
250
|
+
"compose",
|
|
251
|
+
"-f",
|
|
252
|
+
composeFile,
|
|
253
|
+
"build"
|
|
254
|
+
], {
|
|
255
|
+
cwd,
|
|
256
|
+
onLine: adaptOnLine(o?.onLine)
|
|
257
|
+
});
|
|
258
|
+
},
|
|
259
|
+
async restart(idOrName) {
|
|
260
|
+
return await runner("docker", ["restart", idOrName]);
|
|
261
|
+
},
|
|
262
|
+
async stop(idOrName) {
|
|
263
|
+
return await runner("docker", ["stop", idOrName]);
|
|
264
|
+
},
|
|
265
|
+
async remove(idOrName, o) {
|
|
266
|
+
const args = ["rm"];
|
|
267
|
+
if (o?.force) args.push("--force");
|
|
268
|
+
args.push(idOrName);
|
|
269
|
+
return await runner("docker", args);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
//#endregion
|
|
274
|
+
//#region src/cli/commands/logs.ts
|
|
275
|
+
const USAGE$2 = `用法: deploy-cli logs <容器名> [--tail <行数>] [--follow]
|
|
276
|
+
|
|
277
|
+
选项:
|
|
278
|
+
--tail <行数> 只显示最后 N 行(默认全部)
|
|
279
|
+
--follow 持续跟随日志输出(Ctrl-C 退出)
|
|
280
|
+
|
|
281
|
+
示例:
|
|
282
|
+
deploy-cli logs deploy-cli-test-echo --tail 5
|
|
283
|
+
deploy-cli logs my-app --follow`;
|
|
284
|
+
/** 查看容器日志,逐行输出到 stdout;返回退出码 */
|
|
285
|
+
async function logs(args, options = {}) {
|
|
286
|
+
const log = options.log ?? ((line) => console.log(line));
|
|
287
|
+
const error = options.error ?? ((line) => console.error(line));
|
|
288
|
+
let tail;
|
|
289
|
+
let follow = false;
|
|
290
|
+
const positionals = [];
|
|
291
|
+
for (let i = 0; i < args.length; i++) {
|
|
292
|
+
const arg = args[i];
|
|
293
|
+
if (arg === "-h" || arg === "--help") {
|
|
294
|
+
log(USAGE$2);
|
|
295
|
+
return 0;
|
|
296
|
+
}
|
|
297
|
+
if (arg === "--tail") {
|
|
298
|
+
const value = args[++i];
|
|
299
|
+
const parsed = Number(value);
|
|
300
|
+
if (value === void 0 || value === "" || !Number.isInteger(parsed) || parsed < 0) {
|
|
301
|
+
error(`--tail 需要非负整数参数: ${value ?? ""}`);
|
|
302
|
+
error(USAGE$2);
|
|
303
|
+
return 1;
|
|
304
|
+
}
|
|
305
|
+
tail = parsed;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (arg === "--follow") {
|
|
309
|
+
follow = true;
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (arg.startsWith("-")) {
|
|
313
|
+
error(`未知参数: ${arg}`);
|
|
314
|
+
error(USAGE$2);
|
|
315
|
+
return 1;
|
|
316
|
+
}
|
|
317
|
+
positionals.push(arg);
|
|
318
|
+
}
|
|
319
|
+
if (positionals.length !== 1) {
|
|
320
|
+
error(positionals.length === 0 ? "缺少容器名参数" : `只支持一个容器名参数: ${positionals.join(" ")}`);
|
|
321
|
+
error(USAGE$2);
|
|
322
|
+
return 1;
|
|
323
|
+
}
|
|
324
|
+
const name = positionals[0];
|
|
325
|
+
const docker = options.docker ?? createDocker();
|
|
326
|
+
const logOpts = {
|
|
327
|
+
tail,
|
|
328
|
+
follow
|
|
329
|
+
};
|
|
330
|
+
try {
|
|
331
|
+
const result = await docker.logs(name, {
|
|
332
|
+
...logOpts,
|
|
333
|
+
onLine: (line) => log(line)
|
|
334
|
+
});
|
|
335
|
+
if (result.code !== 0) {
|
|
336
|
+
error(execFailureMessage("docker", logsArgs(name, logOpts), result));
|
|
337
|
+
return 1;
|
|
338
|
+
}
|
|
339
|
+
return 0;
|
|
340
|
+
} catch (err) {
|
|
341
|
+
error(err instanceof Error ? err.message : String(err));
|
|
342
|
+
return 1;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
//#endregion
|
|
346
|
+
//#region src/cli/commands/ps.ts
|
|
347
|
+
const USAGE$1 = `用法: deploy-cli ps [--all] [--project <项目名>]
|
|
348
|
+
|
|
349
|
+
选项:
|
|
350
|
+
--all 含已停止的容器
|
|
351
|
+
--project <项目名> 按配置注册表中的项目名过滤容器
|
|
352
|
+
|
|
353
|
+
示例:
|
|
354
|
+
deploy-cli ps
|
|
355
|
+
deploy-cli ps --all
|
|
356
|
+
deploy-cli ps --project my-app`;
|
|
357
|
+
/** 列出容器:NAME\tIMAGE\tSTATE\tPORTS;返回退出码 */
|
|
358
|
+
async function ps(args, options = {}) {
|
|
359
|
+
const log = options.log ?? ((line) => console.log(line));
|
|
360
|
+
const error = options.error ?? ((line) => console.error(line));
|
|
361
|
+
let all = false;
|
|
362
|
+
let projectName;
|
|
363
|
+
for (let i = 0; i < args.length; i++) {
|
|
364
|
+
const arg = args[i];
|
|
365
|
+
if (arg === "-h" || arg === "--help") {
|
|
366
|
+
log(USAGE$1);
|
|
367
|
+
return 0;
|
|
368
|
+
}
|
|
369
|
+
if (arg === "--all") {
|
|
370
|
+
all = true;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (arg === "--project") {
|
|
374
|
+
const value = args[++i];
|
|
375
|
+
if (value === void 0 || value === "") {
|
|
376
|
+
error("缺少 --project 的项目名参数");
|
|
377
|
+
error(USAGE$1);
|
|
378
|
+
return 1;
|
|
379
|
+
}
|
|
380
|
+
projectName = value;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
error(`未知参数: ${arg}`);
|
|
384
|
+
error(USAGE$1);
|
|
385
|
+
return 1;
|
|
386
|
+
}
|
|
387
|
+
const docker = options.docker ?? createDocker();
|
|
388
|
+
try {
|
|
389
|
+
let nameFilter;
|
|
390
|
+
if (projectName !== void 0) {
|
|
391
|
+
const project = (options.projects ?? (() => loadConfig().projects))().find((p) => p.name === projectName);
|
|
392
|
+
if (!project) {
|
|
393
|
+
error(`未找到项目: ${projectName}(请检查 ~/.deploy-cli/config.json 的 projects 注册表)`);
|
|
394
|
+
return 1;
|
|
395
|
+
}
|
|
396
|
+
nameFilter = project.name;
|
|
397
|
+
}
|
|
398
|
+
const listOpts = {};
|
|
399
|
+
if (all) listOpts.all = true;
|
|
400
|
+
if (nameFilter) listOpts.name = nameFilter;
|
|
401
|
+
const containers = await docker.listContainers(listOpts);
|
|
402
|
+
log("NAME IMAGE STATE PORTS");
|
|
403
|
+
for (const container of containers) log(`${container.name}\t${container.image}\t${container.state}\t${container.ports}`);
|
|
404
|
+
return 0;
|
|
405
|
+
} catch (err) {
|
|
406
|
+
error(err instanceof Error ? err.message : String(err));
|
|
407
|
+
return 1;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
//#endregion
|
|
411
|
+
//#region src/cli/index.ts
|
|
412
|
+
const USAGE = `用法:
|
|
413
|
+
deploy-cli 启动交互式 TUI
|
|
414
|
+
deploy-cli tui 启动交互式 TUI
|
|
415
|
+
deploy-cli doctor 环境自检
|
|
416
|
+
deploy-cli ps 列出容器(--help 看示例)
|
|
417
|
+
deploy-cli logs 查看容器日志(--help 看示例)`;
|
|
418
|
+
/** CLI 子命令分发,返回退出码 */
|
|
419
|
+
async function main(argv) {
|
|
420
|
+
const [command] = argv;
|
|
421
|
+
switch (command) {
|
|
422
|
+
case "doctor": return await doctor();
|
|
423
|
+
case "ps": return await ps(argv.slice(1));
|
|
424
|
+
case "logs": return await logs(argv.slice(1));
|
|
425
|
+
case "help":
|
|
426
|
+
case "--help":
|
|
427
|
+
case "-h":
|
|
428
|
+
console.log(USAGE);
|
|
429
|
+
return 0;
|
|
430
|
+
default:
|
|
431
|
+
console.error(`未知命令: ${command ?? ""}`);
|
|
432
|
+
console.error(USAGE);
|
|
433
|
+
return 1;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
//#endregion
|
|
437
|
+
export { main };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
//#region src/core/config.ts
|
|
5
|
+
const CONFIG_DIR = resolve(homedir(), ".deploy-cli");
|
|
6
|
+
const CONFIG_PATH = resolve(CONFIG_DIR, "config.json");
|
|
7
|
+
const DEFAULT_CONFIG = {
|
|
8
|
+
servers: [],
|
|
9
|
+
tunnels: [],
|
|
10
|
+
projects: []
|
|
11
|
+
};
|
|
12
|
+
/** 读取配置,不存在则创建默认配置;projects 缺 deployDir 时补默认值 'deploy' */
|
|
13
|
+
function loadConfig() {
|
|
14
|
+
if (!existsSync(CONFIG_PATH)) {
|
|
15
|
+
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
16
|
+
saveConfig(DEFAULT_CONFIG);
|
|
17
|
+
console.log(`[config] 已创建默认配置: ${CONFIG_PATH}`);
|
|
18
|
+
return {
|
|
19
|
+
servers: [],
|
|
20
|
+
tunnels: [],
|
|
21
|
+
projects: []
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const raw = readFileSync(CONFIG_PATH, "utf-8");
|
|
25
|
+
let parsed;
|
|
26
|
+
try {
|
|
27
|
+
parsed = JSON.parse(raw);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
30
|
+
throw new Error(`配置文件损坏(JSON 解析失败): ${CONFIG_PATH}: ${reason}`);
|
|
31
|
+
}
|
|
32
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error(`配置文件损坏(应为 JSON 对象): ${CONFIG_PATH}`);
|
|
33
|
+
const config = parsed;
|
|
34
|
+
return {
|
|
35
|
+
servers: asArray(config.servers, "servers"),
|
|
36
|
+
tunnels: asArray(config.tunnels, "tunnels"),
|
|
37
|
+
projects: asArray(config.projects, "projects").map((project) => normalizeProject(project))
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** 字段缺省视为空数组;非数组抛配置错误(含路径),避免 TypeError 崩溃 */
|
|
41
|
+
function asArray(value, key) {
|
|
42
|
+
if (value === void 0 || value === null) return [];
|
|
43
|
+
if (!Array.isArray(value)) throw new Error(`配置文件损坏(${key} 应为数组): ${CONFIG_PATH}`);
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
/** 写入配置 */
|
|
47
|
+
function saveConfig(config) {
|
|
48
|
+
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
49
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2) + "\n", "utf-8");
|
|
50
|
+
}
|
|
51
|
+
/** 展开 ~ 路径 */
|
|
52
|
+
function expandPath(p) {
|
|
53
|
+
if (p.startsWith("~/")) return resolve(homedir(), p.slice(2));
|
|
54
|
+
if (p === "~") return homedir();
|
|
55
|
+
return resolve(p);
|
|
56
|
+
}
|
|
57
|
+
/** 读入的项目元素缺 deployDir 时补默认值(配置文件可省略该字段) */
|
|
58
|
+
function normalizeProject(project) {
|
|
59
|
+
return {
|
|
60
|
+
...project,
|
|
61
|
+
deployDir: project.deployDir ?? "deploy"
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
export { loadConfig as n, saveConfig as r, expandPath as t };
|
package/dist/main.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import { n as loadConfig, r as saveConfig, t as expandPath } from "./config-CVqFEtQB.js";
|
|
1
2
|
import { Box, Text, createApp, useApp, useInput } from "@vue-tui/runtime";
|
|
2
3
|
import { Fragment, createBlock, createCommentVNode, createElementBlock, createTextVNode, createVNode, defineComponent, nextTick, onMounted, onUnmounted, openBlock, ref, renderList, shallowRef, toDisplayString, watch, withCtx } from "vue";
|
|
3
4
|
import { ScrollBox } from "@vue-tui/components";
|
|
4
5
|
import { createServer } from "net";
|
|
5
|
-
import {
|
|
6
|
+
import { readFileSync, readdirSync } from "fs";
|
|
6
7
|
import { Client } from "ssh2";
|
|
7
8
|
import { dirname, join, resolve } from "path";
|
|
8
|
-
import { homedir } from "os";
|
|
9
9
|
import { spawn } from "child_process";
|
|
10
10
|
//#region \0/plugin-vue/export-helper
|
|
11
11
|
var export_helper_default = (sfc, props) => {
|
|
@@ -930,67 +930,6 @@ function _sfc_render$1(_ctx, _cache, $props, $setup, $data, $options) {
|
|
|
930
930
|
}
|
|
931
931
|
var ConfigPanel_default = /*#__PURE__*/ export_helper_default(_sfc_main$1, [["render", _sfc_render$1], ["__file", "/Users/yangdongnan/work/deploy-cli/src/components/ConfigPanel.vue"]]);
|
|
932
932
|
//#endregion
|
|
933
|
-
//#region src/core/config.ts
|
|
934
|
-
const CONFIG_DIR = resolve(homedir(), ".deploy-cli");
|
|
935
|
-
const CONFIG_PATH = resolve(CONFIG_DIR, "config.json");
|
|
936
|
-
const DEFAULT_CONFIG = {
|
|
937
|
-
servers: [],
|
|
938
|
-
tunnels: [],
|
|
939
|
-
projects: []
|
|
940
|
-
};
|
|
941
|
-
/** 读取配置,不存在则创建默认配置;projects 缺 deployDir 时补默认值 'deploy' */
|
|
942
|
-
function loadConfig() {
|
|
943
|
-
if (!existsSync(CONFIG_PATH)) {
|
|
944
|
-
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
945
|
-
saveConfig(DEFAULT_CONFIG);
|
|
946
|
-
console.log(`[config] 已创建默认配置: ${CONFIG_PATH}`);
|
|
947
|
-
return {
|
|
948
|
-
servers: [],
|
|
949
|
-
tunnels: [],
|
|
950
|
-
projects: []
|
|
951
|
-
};
|
|
952
|
-
}
|
|
953
|
-
const raw = readFileSync(CONFIG_PATH, "utf-8");
|
|
954
|
-
let parsed;
|
|
955
|
-
try {
|
|
956
|
-
parsed = JSON.parse(raw);
|
|
957
|
-
} catch (error) {
|
|
958
|
-
const reason = error instanceof Error ? error.message : String(error);
|
|
959
|
-
throw new Error(`配置文件损坏(JSON 解析失败): ${CONFIG_PATH}: ${reason}`);
|
|
960
|
-
}
|
|
961
|
-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error(`配置文件损坏(应为 JSON 对象): ${CONFIG_PATH}`);
|
|
962
|
-
const config = parsed;
|
|
963
|
-
return {
|
|
964
|
-
servers: asArray(config.servers, "servers"),
|
|
965
|
-
tunnels: asArray(config.tunnels, "tunnels"),
|
|
966
|
-
projects: asArray(config.projects, "projects").map((project) => normalizeProject(project))
|
|
967
|
-
};
|
|
968
|
-
}
|
|
969
|
-
/** 字段缺省视为空数组;非数组抛配置错误(含路径),避免 TypeError 崩溃 */
|
|
970
|
-
function asArray(value, key) {
|
|
971
|
-
if (value === void 0 || value === null) return [];
|
|
972
|
-
if (!Array.isArray(value)) throw new Error(`配置文件损坏(${key} 应为数组): ${CONFIG_PATH}`);
|
|
973
|
-
return value;
|
|
974
|
-
}
|
|
975
|
-
/** 写入配置 */
|
|
976
|
-
function saveConfig(config) {
|
|
977
|
-
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
978
|
-
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2) + "\n", "utf-8");
|
|
979
|
-
}
|
|
980
|
-
/** 展开 ~ 路径 */
|
|
981
|
-
function expandPath(p) {
|
|
982
|
-
if (p.startsWith("~/")) return resolve(homedir(), p.slice(2));
|
|
983
|
-
if (p === "~") return homedir();
|
|
984
|
-
return resolve(p);
|
|
985
|
-
}
|
|
986
|
-
/** 读入的项目元素缺 deployDir 时补默认值(配置文件可省略该字段) */
|
|
987
|
-
function normalizeProject(project) {
|
|
988
|
-
return {
|
|
989
|
-
...project,
|
|
990
|
-
deployDir: project.deployDir ?? "deploy"
|
|
991
|
-
};
|
|
992
|
-
}
|
|
993
|
-
//#endregion
|
|
994
933
|
//#region src/lib/tunnel.ts
|
|
995
934
|
const activeTunnels = /* @__PURE__ */ new Map();
|
|
996
935
|
/** 读取私钥文件内容 */
|
package/package.json
CHANGED
package/dist/cli-g63_QDLc.js
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
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 };
|