@infly/libs 2.2.12 → 2.2.14

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
@@ -165,6 +165,24 @@ const {
165
165
 
166
166
  `build/build-utils/*` 是包内实现目录,不作为新的跨仓调用入口。应用选择、产品线、环境和构建顺序仍由调用方负责;该入口不改变 `infly-libs beforeBuild`、`afterBuild` 的现有行为。
167
167
 
168
+ Vite 应用需要在最终 `index.html` 中记录构建时间时,使用显式入口 `@infly/libs/build/vite`:
169
+
170
+ ```ts
171
+ import { createBuildTimestampPlugin } from "@infly/libs/build/vite";
172
+
173
+ export default defineConfig({
174
+ plugins: [createBuildTimestampPlugin()],
175
+ });
176
+ ```
177
+
178
+ 插件只在 `vite build` 时生效,默认向 `<head>` 注入 UTC ISO 8601 时间:
179
+
180
+ ```html
181
+ <meta name="build-timestamp" content="2026-09-30T03:25:38.000Z">
182
+ ```
183
+
184
+ 可通过 `timestamp` 固定时间以支持可复现测试,通过 `metaName` 调整字段名。未显式引入该插件的既有项目行为不变。
185
+
168
186
  通用 workspace 与 Git 原语通过稳定子路径使用:
169
187
 
170
188
  ```javascript
@@ -260,7 +278,7 @@ Windows 本地构建连接 `dockerDesktopLinuxEngine` 失败时,入口尝试
260
278
 
261
279
  维护归属:公共构建工具;以 `bin/cli.js`、`build/docker/target-release.js` 和 `deployment-bundle.js` 为实现契约。新增配置字段时同步此节及测试。
262
280
 
263
- `infly-libs docker:release` 先单选 target,再多选该 target 的 components。构建在执行命令的机器进行。默认不推送;`--push` 构建、验证后先推送不可变 `<完整40位提交版本>`,再更新 `latest`;`--dry-run` 只解析配置,不联网、不运行 Git/Docker、不创建目录。
281
+ `infly-libs docker:release` 先单选 target,再多选该 target 的 components。构建在执行命令的机器进行。默认不推送;`--push` 构建、验证后先推送不可变 `<完整40位提交版本>`,再更新 `latest`;组件显式设置 `mutableTags: false` 时只生成、推送不可变提交标签。`--dry-run` 只解析配置,不联网、不运行 Git/Docker、不创建目录。未使用下述新增字段的既有 target 保持原有构建参数、commit + latest 标签和推送顺序。
264
282
 
265
283
  当 Docker 配置较大时,推荐在 `package.json` 只保留配置入口和真正需要被 Jenkins 直接读取的部署差异:
266
284
 
@@ -379,11 +397,16 @@ targets:
379
397
  - `config`:`infly.docker` 中的外部配置路径,支持 JSON、YAML、JS;外部配置为基础,package 中的同名对象递归覆盖。循环引用和未知格式会直接失败。
380
398
  - `compose`:组件构建配置来源,必须指定 `file` 和 `service`。工具从 Compose 服务读取镜像仓库、上下文、Dockerfile、stage 与构建参数;组件显式字段仍可覆盖解析结果。
381
399
  - `target` / `buildArgs`:分别映射到 `docker buildx build --target` 和 `--build-arg`;使用 `compose` 时通常无需重复填写。
400
+ - `platform`:可选 Docker 构建平台,例如 `linux/amd64`;映射到 `docker buildx build --platform`。这里的组件字段不同于 CLI 的 `--platform` target 兼容别名。
401
+ - `mutableTags`:默认 `true`,保持 commit + latest 双标签;设为 `false` 时只构建和推送完整 40 位 commit 标签。
382
402
  - `requiredFiles`:构建前必须存在的源码仓相对路径,例如前端产物的 `index.html`;缺失时停止构建。
403
+ - `pushBranch` / `pushRef`:target 级可选推送门禁,必须成对配置,例如 `develop` 与 `origin/develop`。仅 `--push` 时要求当前分支匹配、主动 fetch 对应 remote/branch,并要求本地 HEAD 与远端提交相同;`--no-push` 仍允许在其他分支本地验证镜像。
404
+ - `requirements`:target 级可选本机工具最低版本,目前只支持 `node`、`pnpm` 和 `>=x.y.z` 格式。
405
+ - `preBuild`:target 级可选命令参数数组列表,例如 `[["pnpm", "install", "--frozen-lockfile"], ["pnpm", "run", "build:test"]]`。命令不经过 shell,在干净工作树和推送门禁之后、Docker 检查与 `requiredFiles` 校验之前执行;配置具有代码执行权限,必须与源码一样评审。
383
406
  - `source`:可选独立源码仓;`directory` 支持单个路径或按优先级排列的候选路径数组,路径均从项目目录解析,采用第一个包含 `.git` 的目录。fetch 后使用指定分支的临时 detached worktree,不切换当前开发分支。交互终端找不到候选仓库时会列出已检查路径、展示路径示例并等待手动输入;取消或非交互环境仍明确说明未执行构建或推送,打印编号化的 clone、fetch 和重跑步骤,全部构建停止,CLI 返回 2。
384
407
  - `targetPresets` 与 `targets.*.extends`:可选的具名公共目标配置。preset 可以继续 `extends` 另一个 preset;目标只继承显式指定的 preset,未声明 `extends` 的历史目标不受影响。同名普通对象递归合并;数组、字符串、数字、布尔值和 `null` 由下层整项替换,不按数组下标混合。未知 preset 或循环继承直接失败。本地构建、Compose 发布和部署包生成共用同一解析规则。
385
408
  - `releaseScripts`:可选的组件重跑脚本覆盖。默认从项目 scripts 自动识别严格匹配 `release:<组件>:docker` 的脚本,例如 `release:backend:docker`;源码仓缺失时提示 `pnpm run <脚本> -- --platform ...`。只有脚本名称不遵循约定时才需要显式配置;无匹配时提示通用 `pnpm exec infly-libs docker:release` 命令。
386
- - 无 `source` 时构建当前项目,要求干净工作树且 HEAD 等于 `expectedRef`。有 `source` 时校验临时工作树对应的 origin 分支。
409
+ - 无 `source` 时构建当前项目,要求干净工作树;通常配置组件 `expectedRef` 并要求 HEAD 与其相同。target 已配置 `pushBranch` / `pushRef` 时可以省略组件 `expectedRef`,推送同步由 target 门禁负责。有 `source` 时校验临时工作树对应的 origin 分支。
387
410
  - `check`:可选镜像内校验,使用 `entrypoint`、`args` 数组及 `addHosts` 数组;失败不推送。配置命令具有代码执行权限,应与源码一样评审,不能来自不可信输入。
388
411
  - `deployment`:仅用于部署包生成;`ready=false` 会阻止生成。可设置 `lockName`,默认 `.infly-release.lock`。服务归属和 imageEnv 必须唯一。
389
412
  - `runtimeEnv`:可选的服务器运行时环境文件白名单。`sourceFile` 必须是部署目录内已存在的总环境文件,公共脚本不会创建它,并在校验通过后收紧为 600 权限;`targetFile` 是自动生成的应用专用文件。`variables` 默认必需且按 `name` 输出,可用 `sources` 声明按顺序读取的候选源变量,用 `optional=true` 声明可选项。源文件不存在、不是普通文件、必需变量缺失/为空/重复时,在拉取或重建容器前失败;生成使用临时文件原子替换并设置 600 权限。失败时恢复已有专用文件;若这是首次生成且容器已经开始重建,则保留新文件,保证旧镜像可继续通过同一挂载路径回滚。脚本不执行 `source`,也不输出变量值。
@@ -401,7 +424,7 @@ infly-libs docker:bundle --target demo --dry-run
401
424
 
402
425
  `--platform` 是 `--target` 的兼容别名;`--config` 读取 JSON、YAML 或 JS 文件,其顶层直接为 docker 配置。`docker:config` 输出合并和继承后的单行 JSON,便于不安装 YAML 插件的 Jenkins 使用。CLI 参数优先于项目配置;`--backend-dir` 仅覆盖名为 backend 的组件源码目录,并覆盖整个 `source.directory` 候选数组。
403
426
 
404
- 构建成功后逐组件输出完整提交版本、`组件=仓库:<完整40位提交版本>` 和 `组件_latest=仓库:latest` 机器行;每个组件的一次构建同时生成不可变提交标签与 `latest`,启用推送时先推送不可变标签,再更新 `latest`。全部构建结束后打印 `✅ 完成` 汇总块,明确区分“不可变提交版本”和“最新推送镜像(所选平台 registry 的 latest)”。可选 `--cicd-url`(支持等号和空格两种传参形式)只在目标配置 `deployment.ready=true` 时于汇总块尾输出蓝色发布系统链接;未开放部署的平台只提示镜像已推送,不展示可能造成误解的部署入口。“发布系统:”与中文 URL 分为两行,URL 独占一行,OSC 8 显式超链接使用百分号编码后的标准 URL 作为点击目标,确保支持该协议的终端可通过 Ctrl + 鼠标左键完整打开。不传时保持原输出。链接必须是合法 HTTP/HTTPS URL,不含用户名或密码。单组件推送成功且 `--cicd-url` 指向以 `/build` 结尾的 Jenkins 构建地址时,链接会预填“平台”与“镜像版本”参数(`平台 / 完整40位提交版本 / 北京时间`),打开构建页默认即为本次推送镜像;多组件推送或其他地址保持原链接。
427
+ 构建成功后逐组件输出完整提交版本和 `组件=仓库:<完整40位提交版本>` 机器行;默认还输出 `组件_latest=仓库:latest`,并在推送时先推不可变标签、再更新 `latest`。配置 `mutableTags: false` 的组件不会生成或输出 latest。全部构建结束后打印 `✅ 完成` 汇总块,明确区分不可变提交版本与可选的最新推送镜像。可选 `--cicd-url`(支持等号和空格两种传参形式)只在目标配置 `deployment.ready=true` 时于汇总块尾输出蓝色发布系统链接;未开放部署的平台只提示镜像已推送,不展示可能造成误解的部署入口。“发布系统:”与中文 URL 分为两行,URL 独占一行,OSC 8 显式超链接使用百分号编码后的标准 URL 作为点击目标,确保支持该协议的终端可通过 Ctrl + 鼠标左键完整打开。不传时保持原输出。链接必须是合法 HTTP/HTTPS URL,不含用户名或密码。单组件推送成功且 `--cicd-url` 指向以 `/build` 结尾的 Jenkins 构建地址时,链接会预填“平台”与“镜像版本”参数(`平台 / 完整40位提交版本 / 北京时间`),打开构建页默认即为本次推送镜像;多组件推送或其他地址保持原链接。
405
428
 
406
429
  `docker:bundle` 只生成本地脚本包,不自动部署;输出须是项目内尚不存在的独立子目录。包内包含 config.sh、Compose 覆盖文件和公共 shell 脚本。Jenkins 可直接传入部署项目 `package.json` 声明的精确版本或 caret 范围获取公共包;`docker:bundle` 会在执行时输出实际解析到的 `@infly/libs` 精确版本,供 Jenkins 日志追溯。镜像不需要 Infly 专用版本标签,因此独立项目使用普通 `docker build` 生成的镜像仍可部署。Jenkins 使用既有凭据,调用 `deploy.sh component=repository:commit` 传输并执行。包内仍保留 `promote-image.sh` 供既有 qc 晋升流程兼容使用;低频定制平台直接在本地推送不可变提交标签与 `latest`,Jenkins 不再调用晋升脚本。运行部署脚本会操作服务器,必须由开发者手动触发既定任务。
407
430
 
@@ -107,12 +107,102 @@ function trimPathInput(value) {
107
107
  : trimmed;
108
108
  }
109
109
 
110
- function validateComponent(component, name) {
110
+ const SAFE_GIT_NAME = /^[A-Za-z0-9][A-Za-z0-9._/-]*$/;
111
+ const SAFE_DOCKER_PLATFORM = /^[a-z0-9]+\/[a-z0-9]+(?:\/[a-z0-9._-]+)?$/;
112
+ const SAFE_VERSION_REQUIREMENT = /^>=(\d+)(?:\.(\d+))?(?:\.(\d+))?$/;
113
+
114
+ function validateReleaseProfile(profile, target) {
115
+ if (profile.pushBranch != null && (typeof profile.pushBranch !== "string"
116
+ || !SAFE_GIT_NAME.test(profile.pushBranch) || profile.pushBranch.startsWith("-")
117
+ || profile.pushBranch.includes(".."))) {
118
+ throw new Error(`${target} pushBranch 必须是合法 Git 分支名`);
119
+ }
120
+ if (profile.pushRef != null && (typeof profile.pushRef !== "string"
121
+ || !SAFE_GIT_NAME.test(profile.pushRef) || profile.pushRef.startsWith("-")
122
+ || profile.pushRef.includes("..") || !profile.pushRef.includes("/"))) {
123
+ throw new Error(`${target} pushRef 必须使用 remote/branch 格式`);
124
+ }
125
+ if ((profile.pushBranch == null) !== (profile.pushRef == null)) {
126
+ throw new Error(`${target} pushBranch 和 pushRef 必须同时配置`);
127
+ }
128
+ if (profile.requirements != null) {
129
+ if (!profile.requirements || typeof profile.requirements !== "object"
130
+ || Array.isArray(profile.requirements)) throw new Error(`${target} requirements 必须是对象`);
131
+ for (const [tool, requirement] of Object.entries(profile.requirements)) {
132
+ if (!["node", "pnpm"].includes(tool) || typeof requirement !== "string"
133
+ || !SAFE_VERSION_REQUIREMENT.test(requirement)) {
134
+ throw new Error(`${target} requirements 只支持 node/pnpm 的 >=x.y.z 版本约束`);
135
+ }
136
+ }
137
+ }
138
+ if (profile.preBuild != null && (!Array.isArray(profile.preBuild) || profile.preBuild.length === 0
139
+ || profile.preBuild.some((command) => !Array.isArray(command) || command.length === 0
140
+ || command.some((arg) => typeof arg !== "string" || !arg || /[\r\n\0]/.test(arg))))) {
141
+ throw new Error(`${target} preBuild 必须是非空命令参数数组列表`);
142
+ }
143
+ }
144
+
145
+ function compareVersions(actual, required) {
146
+ const actualMatch = String(actual).trim().replace(/^v/, "").match(/^(\d+)(?:\.(\d+))?(?:\.(\d+))?/);
147
+ const requiredMatch = String(required).match(SAFE_VERSION_REQUIREMENT);
148
+ if (!actualMatch || !requiredMatch) return false;
149
+ for (let index = 1; index <= 3; index++) {
150
+ const left = Number(actualMatch[index] || 0);
151
+ const right = Number(requiredMatch[index] || 0);
152
+ if (left !== right) return left > right;
153
+ }
154
+ return true;
155
+ }
156
+
157
+ function windowsExecutable(command, platform = process.platform) {
158
+ return platform === "win32" && ["npm", "npx", "pnpm", "yarn"].includes(command)
159
+ ? `${command}.cmd`
160
+ : command;
161
+ }
162
+
163
+ function runReleasePreparation(profile, { run, capture, projectDir, push, platform = process.platform, log = console.log }) {
164
+ validateReleaseProfile(profile, profile.label || "Docker target");
165
+ const git = (...args) => capture("git", ["-C", projectDir, ...args]);
166
+ if (git("status", "--porcelain", "--untracked-files=all")) {
167
+ throw new Error("当前项目存在未提交改动,不能构建发布镜像");
168
+ }
169
+ if (push && profile.pushBranch) {
170
+ const branch = git("branch", "--show-current");
171
+ if (branch !== profile.pushBranch) {
172
+ throw new Error(`镜像只允许从 ${profile.pushBranch} 推送;当前分支: ${branch || "detached HEAD"}`);
173
+ }
174
+ const slash = profile.pushRef.indexOf("/");
175
+ const remote = profile.pushRef.slice(0, slash);
176
+ const remoteBranch = profile.pushRef.slice(slash + 1);
177
+ run("git", ["-C", projectDir, "fetch", remote, remoteBranch], { cwd: projectDir });
178
+ const commit = git("rev-parse", "HEAD").toLowerCase();
179
+ const remoteCommit = git("rev-parse", `${profile.pushRef}^{commit}`).toLowerCase();
180
+ if (commit !== remoteCommit) {
181
+ throw new Error(`${profile.pushBranch} 必须已推送并与 ${profile.pushRef} 同步`);
182
+ }
183
+ }
184
+ for (const [tool, requirement] of Object.entries(profile.requirements || {})) {
185
+ const command = windowsExecutable(tool, platform);
186
+ const args = tool === "node" ? ["--version"] : ["--version"];
187
+ const actual = capture(command, args);
188
+ if (!compareVersions(actual, requirement)) {
189
+ throw new Error(`${tool} 必须满足 ${requirement},当前版本: ${actual || "无法识别"}`);
190
+ }
191
+ }
192
+ for (const [command, ...args] of profile.preBuild || []) {
193
+ log(`执行发布准备: ${command} ${args.join(" ")}`);
194
+ run(windowsExecutable(command, platform), args, { cwd: projectDir });
195
+ }
196
+ }
197
+
198
+ function validateComponent(component, name, guardedByProfile = false) {
111
199
  if (!component || !/^[a-z0-9][a-z0-9._:/-]*$/.test(component.repository || "") || component.repository.includes("@")) {
112
200
  throw new Error(`${name} 必须配置有效的镜像 repository(不含标签)`);
113
201
  }
114
202
  if (component.repository.split("/").at(-1).includes(":")) throw new Error(`${name} repository 不得包含标签`);
115
- if (!component.expectedRef && !component.source?.branch) throw new Error(`${name} 必须配置 expectedRef 或 source.branch`);
203
+ if (!component.expectedRef && !component.source?.branch && !guardedByProfile) {
204
+ throw new Error(`${name} 必须配置 expectedRef、source.branch 或 target pushRef`);
205
+ }
116
206
  if (component.source) {
117
207
  normalizeSourceDirectories(component.source.directory, name);
118
208
  if (!component.source.branch || component.source.branch.startsWith("-")) {
@@ -124,6 +214,13 @@ function validateComponent(component, name) {
124
214
  if (component.target != null && (typeof component.target !== "string" || !/^[a-zA-Z0-9_.-]+$/.test(component.target))) {
125
215
  throw new Error(`${name} target 必须是合法 Docker stage`);
126
216
  }
217
+ if (component.platform != null && (typeof component.platform !== "string"
218
+ || !SAFE_DOCKER_PLATFORM.test(component.platform))) {
219
+ throw new Error(`${name} platform 必须是 linux/amd64 等合法 Docker 平台`);
220
+ }
221
+ if (component.mutableTags != null && typeof component.mutableTags !== "boolean") {
222
+ throw new Error(`${name} mutableTags 必须是布尔值`);
223
+ }
127
224
  if (component.buildArgs != null && (!component.buildArgs || typeof component.buildArgs !== "object"
128
225
  || Array.isArray(component.buildArgs) || Object.entries(component.buildArgs).some(([key, value]) =>
129
226
  !/^[A-Z_][A-Z0-9_]*$/.test(key) || !["string", "number", "boolean"].includes(typeof value)))) {
@@ -178,6 +275,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
178
275
  bump: "none", push: Boolean(options.push), dryRun: Boolean(options.dryRun) });
179
276
  }
180
277
  if (!profile?.components) throw new Error(`平台 ${target} 未配置 components`);
278
+ validateReleaseProfile(profile, target);
181
279
  const components = options.components || await (dependencies.selectOptions || selectOptions)("请选择构建范围",
182
280
  Object.entries(profile.components).map(([value, item]) => ({ value, label: item.label || value })));
183
281
  if (components === EXIT_SELECTION) return { cancelled: true };
@@ -188,7 +286,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
188
286
  const plan = components.map((name) => {
189
287
  if (!Object.hasOwn(profile.components, name)) throw new Error(`不支持的构建范围: ${name}`);
190
288
  const component = profile.components[name];
191
- validateComponent(component, name);
289
+ validateComponent(component, name, Boolean(profile.pushRef));
192
290
  if (!component.source) return { name, ...component };
193
291
  const configuredDirectory = name === "backend" && options.backendDir ? options.backendDir : component.source.directory;
194
292
  const directoryCandidates = resolveSourceDirectories(projectDir, configuredDirectory, name);
@@ -197,8 +295,6 @@ async function runTargetRelease(options = {}, dependencies = {}) {
197
295
  });
198
296
  // dry-run 只解析配置,不联网、不调用 Git/Docker、不创建临时目录。
199
297
  if (options.dryRun) { log(JSON.stringify({ target, push: Boolean(options.push), plan }, null, 2)); return { target, plan }; }
200
- const run = dependencies.run || execute;
201
- const capture = (command, args) => String(run(command, args, { cwd: projectDir, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }) || "").trim();
202
298
  // 全部组件先预检,数组按顺序采用第一个 Git 仓;交互终端允许补录机器本地路径。
203
299
  const stdin = dependencies.stdin || process.stdin;
204
300
  const stdout = dependencies.stdout || process.stdout;
@@ -252,6 +348,12 @@ async function runTargetRelease(options = {}, dependencies = {}) {
252
348
  }
253
349
  return { target, missingSources: missing.map((item) => item.name) };
254
350
  }
351
+ const run = dependencies.run || execute;
352
+ const capture = (command, args) => String(run(command, args, { cwd: projectDir, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }) || "").trim();
353
+ if (profile.pushRef || profile.requirements || profile.preBuild) {
354
+ runReleasePreparation(profile, { run, capture, projectDir, push: Boolean(options.push),
355
+ platform: dependencies.platform, log });
356
+ }
255
357
  await checkDocker(run, projectDir, true, dependencies);
256
358
  const results = [];
257
359
  for (const item of plan) {
@@ -271,7 +373,8 @@ async function runTargetRelease(options = {}, dependencies = {}) {
271
373
  if (git("status", "--porcelain", "--untracked-files=all")) throw new Error(`${item.name} 仓库存在未提交改动`);
272
374
  const commit = git("rev-parse", "HEAD").toLowerCase();
273
375
  const expectedRef = item.source ? `origin/${item.source.branch}` : item.expectedRef;
274
- if (!/^[a-f0-9]{40}$/.test(commit) || commit !== git("rev-parse", `${expectedRef}^{commit}`).toLowerCase()) {
376
+ if (!/^[a-f0-9]{40}$/.test(commit) || (expectedRef
377
+ && commit !== git("rev-parse", `${expectedRef}^{commit}`).toLowerCase())) {
275
378
  throw new Error(`${item.name} HEAD 与 ${expectedRef} 不一致,请先核对并 fetch 对应仓库`);
276
379
  }
277
380
  if (item.source) {
@@ -298,16 +401,18 @@ async function runTargetRelease(options = {}, dependencies = {}) {
298
401
  }
299
402
  if (!io.existsSync(dockerfile)) throw new Error(`缺少 Dockerfile: ${dockerfile}`);
300
403
  const image = `${item.repository}:${commit}`;
301
- const latestImage = `${item.repository}:latest`;
404
+ const latestImage = item.mutableTags === false ? null : `${item.repository}:latest`;
302
405
  // --load 只把当前构建节点平台的镜像载入本地镜像存储;发布流程直接使用单平台镜像。
303
406
  const buildOptions = ["buildx", "build", "--load", "--file", dockerfile];
407
+ if (item.platform) buildOptions.push("--platform", item.platform);
304
408
  if (item.target) buildOptions.push("--target", item.target);
305
409
  for (const [key, value] of Object.entries(item.buildArgs || {})) {
306
410
  buildOptions.push("--build-arg", `${key}=${value}`);
307
411
  }
308
412
  run("docker", [...buildOptions,
309
413
  "--label", `org.opencontainers.image.revision=${commit}`, "--label", `org.opencontainers.image.version=${commit}`,
310
- "--label", `com.infly.platform=${target}`, "--tag", image, "--tag", latestImage, context], { cwd: projectDir });
414
+ "--label", `com.infly.platform=${target}`, "--tag", image,
415
+ ...(latestImage ? ["--tag", latestImage] : []), context], { cwd: projectDir });
311
416
  if (capture("docker", ["image", "inspect", "--format", '{{ index .Config.Labels "org.opencontainers.image.revision" }}', image]) !== commit) {
312
417
  throw new Error(`${item.name} 镜像 revision 校验失败`);
313
418
  }
@@ -317,11 +422,11 @@ async function runTargetRelease(options = {}, dependencies = {}) {
317
422
  }
318
423
  if (options.push) {
319
424
  run("docker", ["push", image], { cwd: projectDir });
320
- run("docker", ["push", latestImage], { cwd: projectDir });
425
+ if (latestImage) run("docker", ["push", latestImage], { cwd: projectDir });
321
426
  }
322
427
  log(`${item.name}_commit=${commit}`);
323
428
  log(`${item.name}=${image}`);
324
- log(`${item.name}_latest=${latestImage}`);
429
+ if (latestImage) log(`${item.name}_latest=${latestImage}`);
325
430
  results.push({ component: item.name, commit, image, latestImage, pushed: Boolean(options.push) });
326
431
  } finally {
327
432
  // 只移除本次创建的隔离 worktree;清理失败保留目录供人工恢复,绝不清空开发工作区。
@@ -334,10 +439,15 @@ async function runTargetRelease(options = {}, dependencies = {}) {
334
439
  // 构建时已逐组件打印 frontend_commit= 等机器行,供失败前人工留档;这里汇总为可直接复制到
335
440
  // 输出同时面向本地核对与 Jenkins 下拉部署,避免再手工复制提交参数。
336
441
  if (results.length) {
442
+ const usesMutableTags = results.some((item) => item.latestImage);
337
443
  const pushNote = options.push
338
444
  ? profile.deployment?.ready === true
339
- ? "已推送不可变提交标签和 latest 最新推送标签,可直接在 Jenkins 选择部署"
340
- : "已推送不可变提交标签和 latest 最新推送标签;该平台尚未开放 Jenkins 部署"
445
+ ? usesMutableTags
446
+ ? "已推送不可变提交标签和 latest 最新推送标签,可直接在 Jenkins 选择部署"
447
+ : "已推送不可变提交标签,可直接在 Jenkins 选择部署"
448
+ : usesMutableTags
449
+ ? "已推送不可变提交标签和 latest 最新推送标签;该平台尚未开放 Jenkins 部署"
450
+ : "已推送不可变提交标签;该平台尚未开放 Jenkins 部署"
341
451
  : "仅本地生成,未推送镜像";
342
452
  log("");
343
453
  log("\x1b[0;34m==========================================");
@@ -346,7 +456,7 @@ async function runTargetRelease(options = {}, dependencies = {}) {
346
456
  log(`可用标签(${pushNote}):`);
347
457
  for (const r of results) {
348
458
  log(` ${r.component}=${r.image} (不可变提交版本)`);
349
- log(` ${r.component}_latest=${r.latestImage} (最新推送镜像,即所选平台 registry 的 latest)`);
459
+ if (r.latestImage) log(` ${r.component}_latest=${r.latestImage} (最新推送镜像,即所选平台 registry 的 latest)`);
350
460
  }
351
461
  if (cicdLink && profile.deployment?.ready === true) {
352
462
  log("");
@@ -364,4 +474,5 @@ async function runTargetRelease(options = {}, dependencies = {}) {
364
474
  return { target, results };
365
475
  }
366
476
 
367
- module.exports = { parseReleaseArgs, runTargetRelease };
477
+ module.exports = { compareVersions, parseReleaseArgs, runReleasePreparation, runTargetRelease,
478
+ validateReleaseProfile, windowsExecutable };
@@ -0,0 +1,30 @@
1
+ export const BUILD_TIMESTAMP_META_NAME: "build-timestamp";
2
+
3
+ export interface BuildTimestampPluginOptions {
4
+ /** 固定构建时间;默认使用创建插件时的当前时间,并统一输出为 UTC ISO 8601。 */
5
+ timestamp?: string | Date;
6
+ /** 注入的 meta name;默认是 build-timestamp。 */
7
+ metaName?: string;
8
+ }
9
+
10
+ export interface BuildTimestampHtmlTag {
11
+ tag: "meta";
12
+ attrs: {
13
+ name: string;
14
+ content: string;
15
+ };
16
+ injectTo: "head";
17
+ }
18
+
19
+ /**
20
+ * 使用最小结构化契约,避免把 @infly/libs 自身的 Vite 小版本类型泄漏给消费方。
21
+ */
22
+ export interface BuildTimestampVitePlugin {
23
+ name: "infly-build-timestamp";
24
+ apply: "build";
25
+ transformIndexHtml(): BuildTimestampHtmlTag[];
26
+ }
27
+
28
+ export function createBuildTimestampPlugin(
29
+ options?: BuildTimestampPluginOptions,
30
+ ): BuildTimestampVitePlugin;
@@ -0,0 +1,50 @@
1
+ const BUILD_TIMESTAMP_META_NAME = "build-timestamp";
2
+
3
+ function normalizeBuildTimestamp(value) {
4
+ const date = value === undefined ? new Date() : value instanceof Date ? value : new Date(value);
5
+ if (Number.isNaN(date.getTime())) {
6
+ throw new TypeError("build timestamp must be a valid Date or date string");
7
+ }
8
+ return date.toISOString();
9
+ }
10
+
11
+ function normalizeMetaName(value) {
12
+ const metaName = value === undefined ? BUILD_TIMESTAMP_META_NAME : String(value).trim();
13
+ if (!/^[A-Za-z][A-Za-z0-9._:-]*$/.test(metaName)) {
14
+ throw new TypeError("build timestamp metaName must be a valid HTML meta name");
15
+ }
16
+ return metaName;
17
+ }
18
+
19
+ /**
20
+ * 创建一个只在 Vite build 时生效的插件,把 UTC 构建时间写入最终 index.html。
21
+ *
22
+ * @param {{ timestamp?: string | Date, metaName?: string }} [options]
23
+ * @returns {object}
24
+ */
25
+ function createBuildTimestampPlugin(options = {}) {
26
+ const buildTimestamp = normalizeBuildTimestamp(options.timestamp);
27
+ const metaName = normalizeMetaName(options.metaName);
28
+
29
+ return {
30
+ name: "infly-build-timestamp",
31
+ apply: "build",
32
+ transformIndexHtml() {
33
+ return [
34
+ {
35
+ tag: "meta",
36
+ attrs: {
37
+ name: metaName,
38
+ content: buildTimestamp,
39
+ },
40
+ injectTo: "head",
41
+ },
42
+ ];
43
+ },
44
+ };
45
+ }
46
+
47
+ module.exports = {
48
+ BUILD_TIMESTAMP_META_NAME,
49
+ createBuildTimestampPlugin,
50
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infly/libs",
3
- "version": "2.2.12",
3
+ "version": "2.2.14",
4
4
  "description": "不受前端框架限制的独立工具库",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -29,6 +29,11 @@
29
29
  "exports": {
30
30
  ".": "./index.js",
31
31
  "./build": "./build/index.js",
32
+ "./build/vite": {
33
+ "types": "./build/vite/index.d.ts",
34
+ "require": "./build/vite/index.js",
35
+ "default": "./build/vite/index.js"
36
+ },
32
37
  "./concurrency": "./core/async/concurrency.js",
33
38
  "./progress": "./cli/progress.js",
34
39
  "./npm-release": "./release/npm-plan.js",