probe.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { readFile, readdir } from "node:fs/promises";
  2. import { delimiter, dirname, join } from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. import { execFile } from "node:child_process";
  5. import { promisify } from "node:util";
  6. const exec = promisify(execFile);
  7. const windows = process.platform === "win32";
  8. const directory = dirname(fileURLToPath(import.meta.url));
  9. const config = JSON.parse(await readFile(join(directory, "config.json"), "utf8"));
  10. const original = process.env.PATH.split(delimiter);
  11. const baseEnv = Object.fromEntries(
  12. // Windows 环境变量不区分大小写;留着原来的 Path 会与下面的 PATH 冲突。
  13. Object.entries(process.env).filter(([name]) => !windows || name.toUpperCase() !== "PATH"),
  14. );
  15. const env = {
  16. ...baseEnv,
  17. PATH: [original[0], join(directory, "system-bin"), ...original.slice(1)].join(delimiter),
  18. };
  19. // Agent 的 Bash 在 Windows 上是 Git Bash;command -v 给出 POSIX 路径,转回 Windows 形式再比较。
  20. const resolved = (
  21. await exec(
  22. windows ? "sh.exe" : "/bin/sh",
  23. [
  24. "-c",
  25. windows
  26. ? 'cygpath -w "$(command -v qpdf)"; p="$(command -v zcode-soffice)" && cygpath -w "$p" || true'
  27. : "command -v qpdf; command -v zcode-soffice || true",
  28. ],
  29. { env },
  30. )
  31. ).stdout
  32. .trim()
  33. .split(/\r?\n/);
  34. const qpdf = windows
  35. ? await exec(process.env.ComSpec, ["/d", "/c", "qpdf"], { env })
  36. : await exec("qpdf", [], { env });
  37. const cache = join(config.storageRoot, "cli/plugins/cache/zcode-plugins-official/documents");
  38. const versions = await readdir(cache);
  39. if (versions.length !== 1) throw new Error("Expected one distributed documents plugin");
  40. const helper = join(cache, versions[0], "scripts/zcode_libreoffice.py");
  41. const office = await exec(config.python, [helper, "check"], { env, timeout: 30000 });
  42. console.log(
  43. JSON.stringify({
  44. pathFirst: original[0],
  45. qpdfPath: resolved[0],
  46. officePath: resolved[1] ?? null,
  47. qpdf: qpdf.stdout + qpdf.stderr,
  48. office: JSON.parse(office.stdout),
  49. officeNotice: office.stderr,
  50. }),
  51. );