@infly/libs 2.1.4 → 2.1.6

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
@@ -89,7 +89,7 @@ infly-libs build:docker --local --cicd-url="https://deploy.example.com/projects/
89
89
  infly-libs build:docker --local --cicd-url "https://deploy.example.com/projects/admin/"
90
90
  ```
91
91
 
92
- 未传 `--cicd-url` 时保持原有输出。参数值必须是合法的 HTTP/HTTPS URL,且不能包含用户名、密码或控制字符。输出时“发布系统:”与 URL 分为两行,URL 独占一行,便于终端识别并通过 Ctrl + 鼠标左键打开。
92
+ 未传 `--cicd-url` 时保持原有输出。参数值必须是合法的 HTTP/HTTPS URL,且不能包含用户名、密码或控制字符。配置和显示文本可以使用中文;输出时“发布系统:”与 URL 分为两行,URL 独占一行,并通过 OSC 8 显式超链接把百分号编码后的标准 URL 作为点击目标,避免依赖终端对中文或空格的自动识别。在支持 OSC 8 的终端中可通过 Ctrl + 鼠标左键打开完整地址。
93
93
 
94
94
  需要在推送前验证镜像自身的 Docker `HEALTHCHECK` 时,可启用可选健康门禁:
95
95
 
@@ -342,7 +342,7 @@ infly-libs docker:bundle --target demo --dry-run
342
342
 
343
343
  `--platform` 是 `--target` 的兼容别名;`--config` 读取 JSON 文件,其顶层直接为 docker 配置。CLI 参数优先于项目配置;`--backend-dir` 仅覆盖名为 backend 的组件源码目录。
344
344
 
345
- 构建成功后逐组件输出完整提交版本、`组件=仓库:<12位提交版本>` 和 `组件_latest=仓库:latest` 机器行;每个组件的一次构建同时生成不可变提交标签与 `latest`,启用推送时先推送不可变标签,再更新 `latest`。全部构建结束后打印 `✅ 完成` 汇总块,明确区分“不可变提交版本”和“最新构建镜像”。可选 `--cicd-url`(支持等号和空格两种传参形式)在推送成功后于汇总块尾输出蓝色发布系统链接;“发布系统:”与 URL 分为两行,URL 独占一行。不传时保持原输出。链接必须是合法 HTTP/HTTPS URL,不含用户名或密码。
345
+ 构建成功后逐组件输出完整提交版本、`组件=仓库:<12位提交版本>` 和 `组件_latest=仓库:latest` 机器行;每个组件的一次构建同时生成不可变提交标签与 `latest`,启用推送时先推送不可变标签,再更新 `latest`。全部构建结束后打印 `✅ 完成` 汇总块,明确区分“不可变提交版本”和“最新构建镜像”。可选 `--cicd-url`(支持等号和空格两种传参形式)在推送成功后于汇总块尾输出蓝色发布系统链接;“发布系统:”与中文 URL 分为两行,URL 独占一行,OSC 8 显式超链接使用百分号编码后的标准 URL 作为点击目标,确保支持该协议的终端可通过 Ctrl + 鼠标左键完整打开。不传时保持原输出。链接必须是合法 HTTP/HTTPS URL,不含用户名或密码。
346
346
 
347
347
  `docker:bundle` 只生成本地脚本包,不自动部署;输出须是项目内尚不存在的独立子目录。包内包含 config.sh、Compose 覆盖文件和公共 shell 脚本。Jenkins 固定公共包版本后生成,使用既有凭据,调用 `deploy.sh component=repository:commit` 传输并执行。包内仍保留 `promote-image.sh` 供既有 qc 晋升流程兼容使用;低频定制平台直接在本地推送不可变提交标签与 `latest`,Jenkins 不再调用晋升脚本。运行部署脚本会操作服务器,必须由开发者手动触发既定任务。
348
348
 
@@ -15,6 +15,7 @@ trap cleanup EXIT
15
15
  current_images=()
16
16
  next_images=()
17
17
  selected_indexes=()
18
+ selected_components=()
18
19
  selected_services=()
19
20
  for i in "${!component_names[@]}"; do
20
21
  key=${image_keys[$i]}
@@ -37,6 +38,7 @@ for pair in "$@"; do
37
38
  [[ "$suffix" != "$image" && "$suffix" =~ ^[0-9a-f]{12}$ ]] || { echo "只接受本组件正式 commit 镜像: $name" >&2; exit 1; }
38
39
  next_images[$i]=$image
39
40
  selected_indexes+=("$i")
41
+ selected_components+=("$name")
40
42
  read -r -a group <<< "${service_groups[$i]}"
41
43
  selected_services+=("${group[@]}")
42
44
  done
@@ -65,10 +67,13 @@ rollback() {
65
67
  echo "发布失败,恢复基线: $previous_env" >&2
66
68
  cp -- "$previous_env" "$release_env_file"
67
69
  chmod 600 "$release_env_file"
68
- if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}" || ! bash "$bundle_dir/health-check.sh"; then
70
+ if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}" \
71
+ || ! run_post_deploy "${selected_components[@]}" \
72
+ || ! bash "$bundle_dir/health-check.sh"; then
69
73
  echo "自动回滚未通过验证,需人工恢复: $project_dir/$previous_env" >&2
70
74
  fi
71
75
  }
72
76
  if ! "${compose[@]}" up -d --no-deps "${selected_services[@]}"; then rollback; exit 1; fi
77
+ if ! run_post_deploy "${selected_components[@]}"; then rollback; exit 1; fi
73
78
  if ! bash "$bundle_dir/health-check.sh"; then rollback; exit 1; fi
74
79
  echo "发布成功: $target; 上一版本记录: $project_dir/$previous_env"
@@ -14,7 +14,7 @@ function renderDeploymentConfig(config, target) {
14
14
  if (!deploy || deploy.ready !== true) throw new Error(`平台 ${target} 尚未完成人工服务器核验,禁止生产发布`);
15
15
  const components = Object.entries(profile.components || {});
16
16
  if (!components.length) throw new Error("部署必须配置 components");
17
- const repositories = [], keys = [], serviceGroups = [];
17
+ const repositories = [], keys = [], serviceGroups = [], postDeployByComponent = [];
18
18
  for (const [name, component] of components) {
19
19
  required(name, "component", /^[a-z][a-z0-9_-]*$/);
20
20
  const repository = required(component.repository, `${name}.repository`, /^[a-z0-9][a-z0-9.:/_-]+$/);
@@ -23,10 +23,20 @@ function renderDeploymentConfig(config, target) {
23
23
  keys.push(required(component.imageEnv, `${name}.imageEnv`, /^[A-Z][A-Z0-9_]*$/));
24
24
  if (!Array.isArray(component.services) || !component.services.length) throw new Error(`${name} 缺少 services`);
25
25
  serviceGroups.push(component.services.map((service) => required(service, "service", /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/)).join(" "));
26
+ if (component.postDeploy != null && !Array.isArray(component.postDeploy)) throw new Error(`${name}.postDeploy 必须是数组`);
27
+ postDeployByComponent.push(component.postDeploy || []);
26
28
  }
27
29
  if (new Set(keys).size !== keys.length) throw new Error("imageEnv 不得重复");
28
30
  const allServices = serviceGroups.flatMap((group) => group.split(" "));
29
31
  if (new Set(allServices).size !== allServices.length) throw new Error("组件不能重复拥有同一个服务");
32
+ for (let i = 0; i < postDeployByComponent.length; i++) {
33
+ for (const action of postDeployByComponent[i]) {
34
+ if (!action || !allServices.includes(action.service) || !Array.isArray(action.command) || !action.command.length
35
+ || !action.command.every((arg) => typeof arg === "string" && arg && !arg.includes("\0"))) {
36
+ throw new Error(`${components[i][0]}.postDeploy 必须绑定已配置服务和非空命令数组`);
37
+ }
38
+ }
39
+ }
30
40
  const lines = ["# 由 @infly/libs 根据项目配置生成;只含非敏感参数。",
31
41
  `target=${quote(required(target, "target", /^[a-zA-Z0-9_-]+$/))}`,
32
42
  `registry=${quote(required(config.registry, "registry", /^[a-zA-Z0-9.:-]+$/))}`,
@@ -42,6 +52,15 @@ function renderDeploymentConfig(config, target) {
42
52
  array("component_names", components.map(([name]) => name)), array("repositories", repositories),
43
53
  array("image_keys", keys), array("service_groups", serviceGroups), array("health_services", allServices),
44
54
  ];
55
+ lines.push("run_post_deploy() {", " local component", " for component in \"$@\"; do", " case \"$component\" in");
56
+ for (let i = 0; i < components.length; i++) {
57
+ lines.push(` ${quote(components[i][0])})`);
58
+ for (const action of postDeployByComponent[i]) {
59
+ lines.push(` \"\${compose[@]}\" exec -T ${quote(action.service)} ${action.command.map(quote).join(" ")} || return $?`);
60
+ }
61
+ lines.push(" ;;");
62
+ }
63
+ lines.push(" *) echo \"未知发布组件: $component\" >&2; return 2 ;;", " esac", " done", "}");
45
64
  const health = deploy.health || {};
46
65
  if (!Array.isArray(health.urls) || !health.urls.length) throw new Error("必须配置 health.urls");
47
66
  for (const url of health.urls) if (!/^https?:\/\//.test(url)) throw new Error("健康检查 URL 必须是 HTTP(S)");
@@ -52,8 +52,10 @@ function formatCicdLink(value) {
52
52
  throw new Error("--cicd-url 不能包含用户名或密码凭据");
53
53
  }
54
54
 
55
- // URL 独占一行,便于终端完整识别并支持 Ctrl + 鼠标左键打开。
56
- return `发布系统:\n\x1b[34m${decodeURI(parsedUrl.href)}\x1b[0m`;
55
+ // 显示中文地址,同时把标准化 URL 放进 OSC 8 点击目标,避免依赖终端的文本自动识别。
56
+ const hyperlinkStart = `\x1b]8;;${parsedUrl.href}\x1b\\`;
57
+ const hyperlinkEnd = "\x1b]8;;\x1b\\";
58
+ return `发布系统:\n${hyperlinkStart}\x1b[34m${decodeURI(parsedUrl.href)}\x1b[0m${hyperlinkEnd}`;
57
59
  }
58
60
 
59
61
  function detectMode({ standalone = false } = {}) {
@@ -87,8 +87,27 @@ async function selectOptions(message, choices, dependencies = {}) {
87
87
  }
88
88
  }
89
89
 
90
+ async function inputText(message, dependencies = {}) {
91
+ const stdin = dependencies.stdin || process.stdin;
92
+ const stdout = dependencies.stdout || process.stdout;
93
+ if (!stdin.isTTY || !stdout.isTTY) {
94
+ throw new Error(`${message}: interactive input requires a TTY; pass an explicit option.`);
95
+ }
96
+
97
+ const Input = dependencies.Input || require("enquirer").Input;
98
+ const prompt = new Input({ name: "value", message, stdin, stdout });
99
+ prompt.once("start", makePromptCancellationSafe);
100
+ try {
101
+ return await prompt.run();
102
+ } catch (error) {
103
+ if (!error || error.name === "CancelPromptError" || isReadlineClosedError(error)) return EXIT_SELECTION;
104
+ throw error;
105
+ }
106
+ }
107
+
90
108
  module.exports = {
91
109
  EXIT_SELECTION,
110
+ inputText,
92
111
  makePromptCancellationSafe,
93
112
  selectOption,
94
113
  selectOptions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infly/libs",
3
- "version": "2.1.4",
3
+ "version": "2.1.6",
4
4
  "description": "不受前端框架限制的独立工具库",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",