在npm安装期间检测node-webkit [英] Detecting node-webkit during npm install

查看:93
本文介绍了在npm安装期间检测node-webkit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为主要作为本机模块构建的node.js库工作.因此,当人们尝试将其包含在node-webkit项目中时,他们必须使用nw-gyp而不是node-gyp重新编译它.我知道我们可以在构建的代码运行时使用以下特定方式检测到node-webkit:

I'm working on a library for node.js that is build primarily as a native module. As such, when people try to include it in node-webkit projects, they have to recompile it using nw-gyp instead of node-gyp. I know we can detect node-webkit specifically when our built code runs using something like this:

try { isNodeWebkit = (typeof require('nw.gui') !== "undefined"); } catch(e) { isNodeWebkit = false; }

try { isNodeWebkit = (typeof require('nw.gui') !== "undefined"); } catch(e) { isNodeWebkit = false; }

但是,我想在我们的安装脚本(由npm install运行)中检测到此错误.或者,我们查看自己的package.json,但是也许有办法查看根项目的package.json吗?这样,我们至少可以查看某些属性,也许是engine之类的东西?

However, I would like to detect this inside of our install script (run by npm install). Alternatively, we look in our own package.json, but is there maybe a way to look at the root project's package.json? That way we could at least look at some property, maybe engine or something?

推荐答案

我最终编写了一个模块来执行此操作: https://github.com/maxkorp/which-native-nodish (还支持atom-shell和重命名的nw.js)

I ended up writing a module to do this: https://github.com/maxkorp/which-native-nodish (also supports atom-shell and the renamed nw.js)

要点是,只要您是node_modules文件夹的子级(该文件夹是具有package.json的文件夹的子级)的子目录,便从该模块的父目录开始,并继续向上移动.进入根目录级别后,请检查package.json中的engines属性以获取atom-shell,node-webkit或nwjs属性.不能保证能正常工作(最远的祖先项目必须指定是否以这种方式使用节点式引擎),但是总比没有好,而且是我见过的唯一的即用型解决方案.

The gist is that you start at the parent directory to the module, and keep going up as long as you are a child of a node_modules folder which is a child of a folder with a package.json. Once at the root level, check the engines property in the package.json for an atom-shell, node-webkit or nwjs property. Not guaranteed to work (The farthest ancestor project must specify if its using a node-ish engine in this way), but it's better than nothing, and the only out of the box solution I've seen.

这篇关于在npm安装期间检测node-webkit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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