代码之家  ›  专栏  ›  技术社区  ›  Alex

如何去除出口上所有的电子膨胀器?

  •  1
  • Alex  · 技术社区  · 5 年前

    我试着在app quit上删除整个目录,但它抛出了EBUSY错误。很明显文件被锁上了,好像有人不想让我们把浮肿去掉?

    const fs = require('fs-extra');
    
    const clearBloat = async () => fs.remove(path.join(app.getPath('appData'), app.name));
    
    app.on('window-all-closed', async () => {
      await clearBloat();
    });
    
    0 回复  |  直到 5 年前
        1
  •  3
  •   reZach    5 年前

    在做了一些测试之后,我发现你必须删除这些文件 之后 您的electron进程已结束(尝试在 退出 将退出 app events 不会删除文件/文件夹;它们会立即被重新创建。 例如,electron(很可能是Chromium)希望这些文件/文件夹在应用程序运行时存在,而要想弄清楚如何连接到应用程序中需要做很多工作)。

    对我有效的方法是从shell中派生一个分离的cmd,等待3秒钟,然后删除给定应用程序文件夹中的所有文件/文件夹。读者的练习是隐藏 ping 命令(或隐藏窗口,但是 mixed success 或选择其他命令。我找到了 timeout 工作,但是 sleep choice like this )做

    以下是您需要补充的内容:

    const { app } = require("electron");
    const { spawn } = require("child_process");
    const path = require("path");
    
    ...
    
    app.on("will-quit", async (event) => {
      const folder = path.join(app.getPath("appData"), app.name);
    
      // Wait 3 seconds, navigate into your app folder and delete all files/folders
      const cmd = `ping localhost -n 3 > nul 2>&1 && pushd "${folder}" && (rd /s /q "${folder}" 2>nul & popd)`;
      
      // shell = true prevents EONENT errors
      // stdio = ignore allows the pipes to continue processing w/o handling command output
      // detached = true allows the command to run once your app is [completely] shut down
      const process = spawn(cmd, { shell: true, stdio: "ignore", detached: true });
    
      // Prevents the parent process from waiting for the child (this) process to finish
      process.unref();
    });
    

    a method 可在您的 electron session 这是一个本机API,用于清除所有这些文件/文件夹。但是,它返回了一个承诺,我不知道如何在这些应用程序事件中同步执行。

    Reference #1