mirror of
https://github.com/YuleBest/FontMM.git
synced 2026-12-04 05:44:50 +08:00
原 dev/*.sh 依赖 zip/unzip 等系统命令, 在 Windows、精简容器与部分 CI 镜像中缺失或行为不一致。全部改写为 Node 脚本, 只需 Node 即可构建。 - dev/pack.mjs: 打包主流程 (取代 cd.sh), 含产物回读校验 - dev/webzip.mjs / gen-sha256.mjs / build-wght.mjs / ci.mjs - dev/empty-font.mjs / sync-fonts-xml.mjs (开发辅助) - dev/lib/: zip 封装 (@zip.js/zip.js) / 路径 / 日志 / 命令 / Go 构建 - package.json 与 3 个 workflow 改用 node/pnpm 入口 - README 开发章节重写 顺带修复: - Go 构建加 -trimpath -buildvcs=false: 原先会把源码绝对路径与 vcs.modified 嵌进二进制, 导致同一份源码在不同目录构建、或工作树有 未提交改动时产物哈希不同, SHA256SUMS 随构建环境漂移 - zip 权限位与控制流: fontmm-wght 保持 0755, 目录条目与派生配置排除 均与原 zip 行为一致; SHA256SUMS 权限固定 0644 - shfmt 参数 -w -d 相互矛盾 (一个改写一个只 diff), 拆为 shfmt 与 shfmt:fix - .gitignore 补充派生字体配置, 避免 dev/sync-fonts-xml.mjs 的产物被误提交 产物验证: 与迁移前对比条目名/权限位/内容哈希一致; 模拟干净克隆 CI 流程构建, 内容与权限位完全一致, 设 SOURCE_DATE_EPOCH 后 zip 逐字节相同。
54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
// dev/lib/exec.mjs — 外部命令调用封装 (取代 shell 里的直接调用)
|
|
// 统一处理: 命令缺失的友好提示、退出码检查、stdout 透传
|
|
import { spawnSync } from 'node:child_process';
|
|
import { log } from './log.mjs';
|
|
|
|
/** 命令是否存在于 PATH (只探测可执行性, 不关心退出码) */
|
|
export function hasCommand(cmd) {
|
|
const probe = spawnSync(cmd, [], { stdio: 'ignore', shell: false });
|
|
// ENOENT 表示命令不存在; 其余情况 (包括参数缺失导致的非 0 退出) 都说明命令可用
|
|
return !(probe.error && probe.error.code === 'ENOENT');
|
|
}
|
|
|
|
/**
|
|
* 运行命令, 失败即抛错 (由调用方捕获或用 runOrDie)。
|
|
* @param {string} cmd
|
|
* @param {string[]} args
|
|
* @param {{cwd?: string, env?: Record<string,string>, quiet?: boolean}} [opts]
|
|
*/
|
|
export function run(cmd, args, opts = {}) {
|
|
const { cwd, env, quiet = false } = opts;
|
|
const res = spawnSync(cmd, args, {
|
|
cwd,
|
|
env: env ? { ...process.env, ...env } : process.env,
|
|
stdio: quiet ? ['ignore', 'pipe', 'pipe'] : ['ignore', 'inherit', 'inherit'],
|
|
encoding: 'utf8',
|
|
shell: false,
|
|
});
|
|
if (res.error) {
|
|
if (res.error.code === 'ENOENT') throw new Error(`未找到命令: ${cmd} (请先安装)`);
|
|
throw new Error(`${cmd} 执行失败: ${res.error.message}`);
|
|
}
|
|
if (res.status !== 0) {
|
|
const detail = quiet ? (res.stderr || res.stdout || '').trim() : '';
|
|
throw new Error(`${cmd} 退出码 ${res.status}${detail ? `: ${detail}` : ''}`);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/** 运行命令并返回 stdout (不打印) */
|
|
export function capture(cmd, args, opts = {}) {
|
|
const res = run(cmd, args, { ...opts, quiet: true });
|
|
return res.stdout ?? '';
|
|
}
|
|
|
|
/** 运行命令失败即终止脚本 (用于构建流程中的必需步骤) */
|
|
export function runOrDie(cmd, args, opts = {}) {
|
|
try {
|
|
return run(cmd, args, opts);
|
|
} catch (e) {
|
|
log.fail(e.message);
|
|
process.exit(1);
|
|
}
|
|
}
|