resolve-pnpm-invocation.mjs 1.3 KB

1234567891011121314151617181920212223242526
  1. /**
  2. * 解析"怎么调用 pnpm"这一个决策,独立成模块只为可测:bump 脚本本体是 top-level await,
  3. * import 它就会真的执行一次 bump,没法在单测里覆盖这段逻辑。
  4. */
  5. /**
  6. * @param {{ npmExecpath?: string | undefined, platform: string }} input
  7. * @returns {{ command: string, args: string[], options: { shell?: true } }}
  8. */
  9. export function resolvePnpmInvocation(input) {
  10. const { npmExecpath, platform } = input;
  11. const args = ["install", "--prefer-offline"];
  12. // 首选让当前 Node 直接跑 pnpm 的 JS 入口:不经 shell、不碰 `.cmd`,三平台同一条码路。
  13. // pnpm 运行 package script 时会注入 npm_execpath。
  14. const entry = typeof npmExecpath === "string" ? npmExecpath.trim() : "";
  15. if (entry) {
  16. return { command: process.execPath, args: [entry, ...args], options: {} };
  17. }
  18. // 回落。Windows 上 pnpm 是 `pnpm.cmd`:execFile 不走 shell 时不补 PATHEXT(ENOENT),
  19. // 而 Node 自 18.20/20.12 起(CVE-2024-27980)又禁止不带 shell spawn `.cmd`(EINVAL)。
  20. // 两条都堵死,所以这里只能借 shell;参数是硬编码字面量,没有拼接也没有外部输入。
  21. if (platform === "win32") {
  22. return { command: "pnpm", args, options: { shell: true } };
  23. }
  24. return { command: "pnpm", args, options: {} };
  25. }