【问题标题】:monkey patch node module猴子补丁节点模块
【发布时间】:2018-03-07 10:57:42
【问题描述】:

我正在尝试在 electron 应用程序中修补 this node module。我想更改capture 方法的输入参数。

到目前为止,我的代码如下所示:

if (process.platform == "darwin") {
      let refSCapt = screenshot.capture;
      console.log("Outside");
      screenshot.capture = function(output: string, callback: any) {
        console.log("Inside");
        // Override output path of a temp .png file
        let tempOutput = output.split("/")[-1];
        refSCapt(this.screenshotsPath + tempOutput, callback);
      };
    }

问题在于补丁没有反映,并且调用了原始方法,就好像什么都没有改变一样。 Outside 已记录,但 Inside 从未被调用。

那么我怎样才能修补这个模块方法呢?

【问题讨论】:

标签: node.js typescript electron


【解决方案1】:

那么我该如何修补这个模块方法呢?

你所拥有的可以正常工作......但只有如果你的代码在其他地方使用之前运行。对于可靠的修补程序,我推荐 https://github.com/ds300/patch-package 安装时修补模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2013-04-14
    • 2016-09-01
    • 2012-09-16
    • 2012-12-18
    • 2020-01-09
    相关资源
    最近更新 更多