nativeExe.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { createLog } from "@/utils/logger";
  2. export default function async(exeName, cb) {
  3. if (typeof nodeRequire == "undefined") {
  4. console.log("nodeRequire failed");
  5. window._hmt.push([
  6. "_trackEvent",
  7. window.location.pathname.replace(/\d+/g, ""),
  8. "不在Electron中,调用 " + exeName + " 失败",
  9. ]);
  10. createLog({
  11. page: window.location.pathname.replace(/\d+/g, ""),
  12. action: "不在Electron中,调用 " + exeName + " 失败",
  13. });
  14. throw new Error("不在Electron中,调用 " + exeName + " 失败");
  15. }
  16. return new Promise(resolve => {
  17. window.nodeRequire("node-cmd").get(exeName, async (err, data, stderr) => {
  18. console.log("exe", err, data, stderr); // 未免过多日志,此处后续可以关闭
  19. createLog({
  20. page: window.location.pathname.replace(/\d+/g, ""),
  21. error: JSON.stringify(err),
  22. data,
  23. stderr: JSON.stringify(stderr),
  24. });
  25. await new Promise(resolve2 => setTimeout(() => resolve2(), 1000));
  26. await cb();
  27. resolve();
  28. });
  29. });
  30. }
  31. export function fileExists(file) {
  32. if (typeof nodeRequire == "undefined") {
  33. console.log("nodeRequire failed");
  34. window._hmt.push([
  35. "_trackEvent",
  36. window.location.pathname.replace(/\d+/g, ""),
  37. "不在Electron中,调用 fs 失败",
  38. ]);
  39. createLog({
  40. page: window.location.pathname.replace(/\d+/g, ""),
  41. action: "不在Electron中,调用 fs 失败",
  42. });
  43. throw new Error("不在Electron中,调用 fs 失败");
  44. }
  45. return window.nodeRequire("fs").existsSync(file);
  46. }