首页 > 解决方案 > 如何在 Electron 环境中使用 nodegit

问题描述

我很难在环境nodegit中运行的Angular 9应用程序中使用electron。这不是安装问题,也不是编译问题,因为我可以让它以某种方式工作......

当我在主进程中执行所有调用时nodegit(通过 IPC 消息),我的调用者需要访问完整的类,例如 Repository。当我们通过 IPC 发送响应时,它会被序列化。所以,在线路的另一端,我得到的只是一个空对象。

ipcMain.on(`GitService:clone`, (event, arg) => {
        const url = arg[0];
        const localPath = arg[1];
        let cloneOptions = arg[2];
        if (cloneOptions === undefined) {
            cloneOptions = {};
        }
        cloneOptions.fetchOpts = cloneOptions.fetchOpts || {};
        cloneOptions.fetchOpts.callbacks = cloneOptions.fetchOpts.callbacks || {};
        cloneOptions.fetchOpts.callbacks.credentials = () => this.credentialsCallback();

        console.log(`Cloning repo ${url}`, cloneOptions);
        const repo = await git.Clone.clone(url, localPath, cloneOptions).catch((err) => {
            console.log(err);
            return git.Repository.open(localPath);
        });
        event.sender.send(`GitService:clone`, repo);
    });

[...]

public credentialsCallback() {
   const creds = git.Cred.userpassPlaintextNew('mysuperusername', "myawsomepassword");
   return creds;
}

这工作正常。它克隆了 repo,但调用者没有收到实际的 Repository 类,因为它正在被序列化。

我能做些什么?

我尝试nodegit直接在渲染器中使用,但它需要一个回调来进行身份验证,看起来回调在渲染器中没有被正确调用......

这是我尝试移至渲染器进程的代码

public method() {
    const git = electron.remote.require('nodegit');
    const clonePath: string = 'the-path-to-clone-to';

    let cloneOptions = {};
    cloneOptions.fetchOpts = cloneOptions.fetchOpts || {};
    cloneOptions.fetchOpts.callbacks = cloneOptions.fetchOpts.callbacks || {};
    cloneOptions.fetchOpts.callbacks.credentials = function() {
        console.log('credentialsCallback');
        const creds = git.Cred.userpassPlaintextNew('mysuperuser', "myawsomepassword");
        return creds;
    };

    console.log(`Cloning repo`);
    const repo = await git.Clone('my-git-url', clonePath, cloneOptions);

    return repo;
}

克隆过程开始(因为我可以看到创建的项目文件夹),我的凭据回调被调用(因为我在控制台中看到'credentialsCallback'),但我收到此错误:

错误:需要远程身份验证但未设置回调

尽管我的回调被调用。

我正在使用const git = electron.remote.require('nodegit');,因为如果我require('nodegit')直接使用,我会在主进程控制台中收到此错误

./node_modules/promisify-node/index.js 中的警告 107:14-27 关键依赖项:依赖项的请求是一个表达式

./node_modules/nodegit/build/Release/nodegit.node 中的警告 1:0 模块解析失败:意外字符 '�' (1:0) 您可能需要适当的加载器来处理此文件类型,目前没有加载器配置为处理这个文件。请参阅https://webpack.js.org/concepts#loaders (此二进制文件省略了源代码)

那么我在这里做错了什么?

我正在使用电子 6.0.7

我正在使用nodegit 0.26.4

标签: electronnodegit

解决方案


推荐阅读