如何在源更改时重建 node.js 插件 [英] How to rebuild node.js addon when source changes

查看:58
本文介绍了如何在源更改时重建 node.js 插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 node.js 应用程序并且我创建了一个本地插件.(如果您需要背景信息,可以创建 node.js 的原生插件像这样).

I have a node.js app and I have created a native addon. (In case you need background info, native addons for node.js can be created like this).

在我的开发环境中,我希望它查看源文件(在本例中为 C++ 源文件)并在 C++ 源文件更改时自动重建我的插件,并在构建完成后自动重新启动节点应用程序.

In my development environment I want it to watch source files (these are C++ source files, in this case) and automatically rebuild my addon when the C++ source files change, and also automatically restart the node application after the build completes.

我确信有不止一种方法可以实现这一点,但我还是尝试了 nodemon.但是我不知道如何让 nodemon 在重新启动应用程序之前等待构建完成.

I'm certain there's more than one way to accomplish this, but I went down the road of trying nodemon. But I couldn't figure out how to get nodemon to wait for the build to finish before restarting the application.

我认为 npm 也可以使用脚本本身来完成此操作,也许使用某种监视包.因此,如果有更简单的方法,我愿意接受其他方法.

I figure npm can probably do this itself too with a script, perhaps with some kind of a watch package. So I'm open to alternative approaches if there's something easier.

因此,当我的任何源文件发生更改时,我真正需要的只是让 node-gyp build 在重新启动工作流中的正确点运行(停止节点、重新编译、重新启动节点).现在它会重新构建插件并重新启动应用程序无需等待构建完成,这是不可取的.

So when any of my source files change, really all I need is for node-gyp build to run at the right point in the restart workflow (stop node, recompile, restart node). Right now it rebuilds the addon and restarts the application without waiting for the build to complete, and that's undesirable.

这是我的 nodemon.json 文件:

Here's my nodemon.json file:

{
  "watch": [
    "addon/"
  ],
  "ignore": [
    "addon/build/"
  ],
  "events": {
    "restart": "cd addon && node-gyp build"
  },
  "ext": "js,json,cc,h"
}

这不起作用,因为重启"就像一个事件,因为我的运行 node-gyp 的脚本会触发并执行,但在构建完成之前它不会阻止重启.

This doesn't work because the "restart" is like an event, in that my script that runs node-gyp fires and executes, but it doesn't block restarting until the build is finished.

我希望它更像中间件,它会在应用程序终止后但在重新启动应用程序之前运行构建脚本.我还咨询了 这个相关的 nodemon 问题,但没有关于如何做到这一点的具体建议发生.

I was hoping it would work more like middleware, where it would run the build script after the app is terminated, but before it restarts the app. I also consulted this relevant nodemon issue, but there's no concrete suggestion there about how to make that happen.

我需要更改一些内容,以便在源更改时自动进行重建(node-gyp 步骤),然后重新启动.

I need to change something so that the rebuild (node-gyp step) will happen automatically when the source changes and then restart nicely.

注意:稍后我将对其进行重构,以便仅在相关 C++ 源文件更改时重建插件,并在 .js 文件更改时重新启动应用程序而不重建 C++ 插件,但首先我需要找出在正确的时间执行 node-gyp 构建步骤.

推荐答案

我通过让 nodemon 使用 -x 选项运行 npm.然后 npm 脚本可以依次执行 buildrun 脚本.

I solved this by having nodemon use the -x option to run npm. Then the npm script can execute the build and run scripts sequentially.

这是我的 package.json 的相关部分:

Here's the relevant part of my package.json:

"scripts": {
  "dev": "nodemon -x \"npm run buildrun\"",
  "buildrun": "npm run build && npm run server",
  "build": "echo Building Addon && cd addon && node-gyp build",
  "server": "nodemon server.js",
}

您可以通过运行 npm run dev 来启动它.

You kickstart this by running npm run dev.

dev 使用 -x 选项运行 nodemon,命令是 npm run buildrun.然后 nodemon 每次代码更改时都会重新启动 npm buildrun 脚本.这是我无法弄清楚的主要部分.

dev runs nodemon with the -x option and the command is npm run buildrun. Then nodemon restarts the npm buildrun script every time code changes. This was the main part I had trouble figuring out.

为了解释其余部分,buildrun 脚本背靠背运行两个脚本.第一个 (build) 构建插件,第二个 (server) 运行服务器.

To explain the rest, the buildrun script runs two scripts back to back. The first one (build) builds the addon and the second one (server) runs the server.

服务器脚本实际上也运行 nodemon:它运行另一个 nodemon 来运行带有 nodemon server.js 的服务器脚本.

The server script actually runs nodemon as well: it runs another nodemon to run the server script with nodemon server.js.

从技术上讲,通过让 npm 再次调用 nodemon(使用正确的配置文件、不同的参数和上下文)nodemon 可以观察代码库的不同部分并重新启动不同的部分系统的(例如,只看服务器代码而不重建整个插件,这就是我的实际代码所做的.)

通过使用 -x 选项调用 nodemon 并在代码更改时重新启动整个构建和运行脚本,我能够对操作进行排序以使 nodemon 首先构建插件,等待构建完成,然后运行服务器.

By calling nodemon with the -x option and having the entire build-and-run script get restarted when code changes, I was able to sequence the actions to get nodemon to first build the addon, wait for the build to complete, and then run the server.

这个 nodemon -x \"npm ...\" 技术起初对我来说并不明显,实际上我花了半年时间才想出这个解决方案.所以,我分享这个是因为其他人可能会发现这个技巧很有用.

This nodemon -x \"npm ...\" technique wasn't obvious to me at first and it actually took me half a year to come up with this solution. So, I'm sharing this because other people may find this technique useful.

这篇关于如何在源更改时重建 node.js 插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆