@dongnanyang/deploy-cli 0.3.0 → 0.4.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/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-Bxxkr9Qr.js");
5
+ const cli = await import("./cli-rULr9mal.js");
6
6
  process.exitCode = await cli.main(argv);
7
7
  }
8
8
  //#endregion
@@ -1,6 +1,9 @@
1
- import { n as loadConfig } from "./config-CVqFEtQB.js";
1
+ import { n as loadConfig, r as saveConfig, t as expandPath } from "./config-CVqFEtQB.js";
2
+ import { readFileSync } from "fs";
3
+ import { resolve } from "path";
2
4
  import { accessSync, constants } from "node:fs";
3
5
  import { spawn } from "node:child_process";
6
+ import { parse } from "yaml";
4
7
  //#region src/core/exec.ts
5
8
  /** 仅 spawn 失败(ENOENT)与超时抛出 */
6
9
  var ExecError = class extends Error {
@@ -272,7 +275,7 @@ function createDocker(runner = run) {
272
275
  }
273
276
  //#endregion
274
277
  //#region src/cli/commands/logs.ts
275
- const USAGE$2 = `用法: deploy-cli logs <容器名> [--tail <行数>] [--follow]
278
+ const USAGE$3 = `用法: deploy-cli logs <容器名> [--tail <行数>] [--follow]
276
279
 
277
280
  选项:
278
281
  --tail <行数> 只显示最后 N 行(默认全部)
@@ -291,7 +294,7 @@ async function logs(args, options = {}) {
291
294
  for (let i = 0; i < args.length; i++) {
292
295
  const arg = args[i];
293
296
  if (arg === "-h" || arg === "--help") {
294
- log(USAGE$2);
297
+ log(USAGE$3);
295
298
  return 0;
296
299
  }
297
300
  if (arg === "--tail") {
@@ -299,7 +302,7 @@ async function logs(args, options = {}) {
299
302
  const parsed = Number(value);
300
303
  if (value === void 0 || value === "" || !Number.isInteger(parsed) || parsed < 0) {
301
304
  error(`--tail 需要非负整数参数: ${value ?? ""}`);
302
- error(USAGE$2);
305
+ error(USAGE$3);
303
306
  return 1;
304
307
  }
305
308
  tail = parsed;
@@ -311,14 +314,14 @@ async function logs(args, options = {}) {
311
314
  }
312
315
  if (arg.startsWith("-")) {
313
316
  error(`未知参数: ${arg}`);
314
- error(USAGE$2);
317
+ error(USAGE$3);
315
318
  return 1;
316
319
  }
317
320
  positionals.push(arg);
318
321
  }
319
322
  if (positionals.length !== 1) {
320
323
  error(positionals.length === 0 ? "缺少容器名参数" : `只支持一个容器名参数: ${positionals.join(" ")}`);
321
- error(USAGE$2);
324
+ error(USAGE$3);
322
325
  return 1;
323
326
  }
324
327
  const name = positionals[0];
@@ -343,6 +346,190 @@ async function logs(args, options = {}) {
343
346
  }
344
347
  }
345
348
  //#endregion
349
+ //#region src/core/project.ts
350
+ const VALID_TYPES = [
351
+ "compose",
352
+ "image",
353
+ "static"
354
+ ];
355
+ const DEFAULT_COMPOSE_FILE = "docker-compose.yml";
356
+ const VALID_SSL = [
357
+ "none",
358
+ "acme",
359
+ "custom"
360
+ ];
361
+ /** deploy.yaml 配置错误;field 指向出问题的字段(如 'type'、'image'、'nginx.domain') */
362
+ var ProjectConfigError = class extends Error {
363
+ field;
364
+ constructor(message, field) {
365
+ super(message);
366
+ this.name = "ProjectConfigError";
367
+ this.field = field;
368
+ }
369
+ };
370
+ /** 读取 <dir>/deploy.yaml 并校验,失败抛 ProjectConfigError */
371
+ function loadProject(dir) {
372
+ const yamlPath = resolve(dir, "deploy.yaml");
373
+ let raw;
374
+ try {
375
+ raw = readFileSync(yamlPath, "utf-8");
376
+ } catch (error) {
377
+ const reason = error instanceof Error ? error.message : String(error);
378
+ if (error?.code === "ENOENT") throw new ProjectConfigError(`缺少 deploy.yaml: ${yamlPath}`);
379
+ throw new ProjectConfigError(`读取 deploy.yaml 失败: ${yamlPath}: ${reason}`);
380
+ }
381
+ let parsed;
382
+ try {
383
+ parsed = parse(raw);
384
+ } catch (error) {
385
+ throw new ProjectConfigError(`deploy.yaml 解析失败: ${yamlPath}: ${error instanceof Error ? error.message : String(error)}`);
386
+ }
387
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new ProjectConfigError(`deploy.yaml 应为 YAML 对象: ${yamlPath}`);
388
+ return validate(parsed);
389
+ }
390
+ /** 校验并规范化 deploy.yaml 模型:name → type 合法性 → 类型必填字段 → nginx.domain 非空 */
391
+ function validate(raw) {
392
+ const name = raw.name;
393
+ if (typeof name !== "string" || name.trim() === "") throw new ProjectConfigError("缺少必填字段: name(项目名不能为空)", "name");
394
+ const type = raw.type;
395
+ if (type === void 0 || type === null) throw new ProjectConfigError("缺少必填字段: type", "type");
396
+ if (typeof type !== "string" || !VALID_TYPES.includes(type)) throw new ProjectConfigError(`type 非法: ${JSON.stringify(type)},合法值: compose | image | static`, "type");
397
+ if (type === "image" && (typeof raw.image !== "string" || raw.image === "")) throw new ProjectConfigError("image 型缺少必填字段: image", "image");
398
+ if (type === "static" && (typeof raw.root !== "string" || raw.root === "")) throw new ProjectConfigError("static 型缺少必填字段: root", "root");
399
+ if (raw.nginx !== void 0 && raw.nginx !== null) {
400
+ const nginx = raw.nginx;
401
+ if (typeof nginx !== "object" || Array.isArray(nginx)) throw new ProjectConfigError("nginx 应为对象", "nginx");
402
+ const n = nginx;
403
+ if (typeof n.domain !== "string" || n.domain.trim() === "") throw new ProjectConfigError("nginx.domain 不能为空", "nginx.domain");
404
+ if (typeof n.ssl !== "string" || !VALID_SSL.includes(n.ssl)) throw new ProjectConfigError("nginx.ssl 缺失或非法,合法值: none | acme | custom", "nginx.ssl");
405
+ }
406
+ const d = raw;
407
+ if (d.type === "compose" && (d.composeFile === void 0 || d.composeFile === null)) d.composeFile = DEFAULT_COMPOSE_FILE;
408
+ return d;
409
+ }
410
+ /** 列出注册表项目;单个坏配置降级 {config:null,error},不中断其余项目 */
411
+ function listProjects(cfg) {
412
+ return cfg.projects.map((project) => {
413
+ try {
414
+ return {
415
+ name: project.name,
416
+ path: project.path,
417
+ config: loadProject(project.path)
418
+ };
419
+ } catch (error) {
420
+ return {
421
+ name: project.name,
422
+ path: project.path,
423
+ config: null,
424
+ error: error instanceof Error ? error.message : String(error)
425
+ };
426
+ }
427
+ });
428
+ }
429
+ /** 读 <dir>/deploy.yaml 取 name 注册进 cfg.projects(不落盘,由调用方 saveConfig) */
430
+ function addProject(cfg, dir) {
431
+ const config = loadProject(dir);
432
+ const byName = cfg.projects.find((p) => p.name === config.name);
433
+ if (byName) throw new Error(`项目已注册: ${config.name}(${byName.path}),请先 remove 再 add`);
434
+ const byPath = cfg.projects.find((p) => p.path === dir);
435
+ if (byPath) throw new Error(`路径已注册为项目 ${byPath.name}: ${dir}`);
436
+ const entry = {
437
+ name: config.name,
438
+ path: dir,
439
+ deployDir: "deploy"
440
+ };
441
+ return {
442
+ ...cfg,
443
+ projects: [...cfg.projects, entry]
444
+ };
445
+ }
446
+ /** 从 cfg.projects 注销指定项目(不落盘,由调用方 saveConfig) */
447
+ function removeProject(cfg, name) {
448
+ const projects = cfg.projects.filter((p) => p.name !== name);
449
+ if (projects.length === cfg.projects.length) throw new Error(`未找到项目: ${name}`);
450
+ return {
451
+ ...cfg,
452
+ projects
453
+ };
454
+ }
455
+ //#endregion
456
+ //#region src/cli/commands/project.ts
457
+ const USAGE$2 = `用法: deploy-cli project <list|add|remove> [参数]
458
+
459
+ 子命令:
460
+ list 列出已注册项目(NAME/PATH/TYPE/ERROR)
461
+ add <path> 读取 <path>/deploy.yaml 并注册项目
462
+ remove <name> 注销项目
463
+
464
+ 示例:
465
+ deploy-cli project list
466
+ deploy-cli project add ~/my-app
467
+ deploy-cli project remove my-app`;
468
+ /** 项目注册表管理:list / add / remove;返回退出码 */
469
+ async function project(args, options = {}) {
470
+ const log = options.log ?? ((line) => console.log(line));
471
+ const error = options.error ?? ((line) => console.error(line));
472
+ const load = options.load ?? loadConfig;
473
+ const save = options.save ?? saveConfig;
474
+ const [sub, ...rest] = args;
475
+ if (sub === "-h" || sub === "--help") {
476
+ log(USAGE$2);
477
+ return 0;
478
+ }
479
+ if (sub === void 0) {
480
+ error("缺少子命令");
481
+ error(USAGE$2);
482
+ return 1;
483
+ }
484
+ try {
485
+ switch (sub) {
486
+ case "list": {
487
+ if (rest.length > 0) {
488
+ error(`未知参数: ${rest.join(" ")}`);
489
+ error(USAGE$2);
490
+ return 1;
491
+ }
492
+ const rows = listProjects(load());
493
+ log("NAME PATH TYPE ERROR");
494
+ for (const row of rows) log(`${row.name}\t${row.path}\t${row.config?.type ?? "-"}\t${row.error ?? ""}`);
495
+ return 0;
496
+ }
497
+ case "add": {
498
+ const raw = rest[0];
499
+ if (raw === void 0 || rest.length > 1) {
500
+ error("add 需要且仅需要一个 <path> 参数");
501
+ error(USAGE$2);
502
+ return 1;
503
+ }
504
+ const dir = expandPath(raw);
505
+ const cfg = addProject(load(), dir);
506
+ save(cfg);
507
+ const entry = cfg.projects[cfg.projects.length - 1];
508
+ log(`已注册项目: ${entry.name} → ${entry.path}`);
509
+ return 0;
510
+ }
511
+ case "remove": {
512
+ const name = rest[0];
513
+ if (name === void 0 || rest.length > 1) {
514
+ error("remove 需要且仅需要一个 <name> 参数");
515
+ error(USAGE$2);
516
+ return 1;
517
+ }
518
+ save(removeProject(load(), name));
519
+ log(`已注销项目: ${name}`);
520
+ return 0;
521
+ }
522
+ default:
523
+ error(`未知子命令: ${sub}`);
524
+ error(USAGE$2);
525
+ return 1;
526
+ }
527
+ } catch (err) {
528
+ error(err instanceof Error ? err.message : String(err));
529
+ return 1;
530
+ }
531
+ }
532
+ //#endregion
346
533
  //#region src/cli/commands/ps.ts
347
534
  const USAGE$1 = `用法: deploy-cli ps [--all] [--project <项目名>]
348
535
 
@@ -384,20 +571,20 @@ async function ps(args, options = {}) {
384
571
  error(USAGE$1);
385
572
  return 1;
386
573
  }
387
- let nameFilter;
388
- if (projectName !== void 0) {
389
- const project = (options.projects ?? (() => loadConfig().projects))().find((p) => p.name === projectName);
390
- if (!project) {
391
- error(`未找到项目: ${projectName}(请检查 ~/.deploy-cli/config.json 的 projects 注册表)`);
392
- return 1;
393
- }
394
- nameFilter = project.name;
395
- }
396
- const listOpts = {};
397
- if (all) listOpts.all = true;
398
- if (nameFilter) listOpts.name = nameFilter;
399
574
  const docker = options.docker ?? createDocker();
400
575
  try {
576
+ let nameFilter;
577
+ if (projectName !== void 0) {
578
+ const project = (options.projects ?? (() => loadConfig().projects))().find((p) => p.name === projectName);
579
+ if (!project) {
580
+ error(`未找到项目: ${projectName}(请检查 ~/.deploy-cli/config.json 的 projects 注册表)`);
581
+ return 1;
582
+ }
583
+ nameFilter = project.name;
584
+ }
585
+ const listOpts = {};
586
+ if (all) listOpts.all = true;
587
+ if (nameFilter) listOpts.name = nameFilter;
401
588
  const containers = await docker.listContainers(listOpts);
402
589
  log("NAME IMAGE STATE PORTS");
403
590
  for (const container of containers) log(`${container.name}\t${container.image}\t${container.state}\t${container.ports}`);
@@ -414,7 +601,8 @@ const USAGE = `用法:
414
601
  deploy-cli tui 启动交互式 TUI
415
602
  deploy-cli doctor 环境自检
416
603
  deploy-cli ps 列出容器(--help 看示例)
417
- deploy-cli logs 查看容器日志(--help 看示例)`;
604
+ deploy-cli logs 查看容器日志(--help 看示例)
605
+ deploy-cli project 项目注册表:list / add / remove(--help 看示例)`;
418
606
  /** CLI 子命令分发,返回退出码 */
419
607
  async function main(argv) {
420
608
  const [command] = argv;
@@ -422,6 +610,7 @@ async function main(argv) {
422
610
  case "doctor": return await doctor();
423
611
  case "ps": return await ps(argv.slice(1));
424
612
  case "logs": return await logs(argv.slice(1));
613
+ case "project": return await project(argv.slice(1));
425
614
  case "help":
426
615
  case "--help":
427
616
  case "-h":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dongnanyang/deploy-cli",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Local CLI TUI for SSH tunnels & deploy scripts",
5
5
  "type": "module",
6
6
  "bin": {
@@ -26,7 +26,8 @@
26
26
  "@vue-tui/runtime": "^0.3.0",
27
27
  "@vue-tui/use": "^0.3.0",
28
28
  "ssh2": "^1.16.0",
29
- "vue": "^3.5.0"
29
+ "vue": "^3.5.0",
30
+ "yaml": "^2.9.1"
30
31
  },
31
32
  "devDependencies": {
32
33
  "@vitejs/plugin-vue-jsx": "5.1.5",