本文介绍了安装Cordova插件的节点依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Cordova插件,它对其中一个钩子脚本具有节点依赖性.理想情况是在安装我的插件时:

I'm writing a Cordova plugin, it has a node dependency for one of the hook scripts. Ideally when my plugin is installed:

$ cordova plugin add my-cordova-plugin

如果package.json列出了依赖项,我希望它运行npm install.

I would like it to run npm install if package.json has dependencies listed.

Cordova是否以某种方式支持此功能?我错过了什么吗?

Does Cordova support this feature in some way? Have I missed something?

我当前的解决方案是另一个运行after_plugin_install的钩子:

My current solution is another hook that runs after_plugin_install:

module.exports = function (context) {
    var shell = context.requireCordovaModule('shelljs');

    shell.cd(context.opts.plugin.dir);
    shell.exec('npm install');
};

推荐答案

我正在寻找将npm模块添加到Cordova项目中,不需要插件,只需使用简单的钩子触发了before_prepare.

I you're looking for to add npm modules to your Cordova project, you don't need a plugin, juste use a simple hook triggered before_prepare.

此钩子将运行每个cordova prepare所需的所有npm安装(也包括cordova runcordova compile等.).

This hook will run all the npm install you need for each cordova prepare (also for cordova run, cordova compile, etc..).

您不必为钩子创建JS文件,Linux shell脚本就足够了(尽管它的移植性较差).当我仅需要执行"npm install"或类似的操作时,我更喜欢使用juste .sh文件.

You don't have to make a JS file for a hook, a linux shell script is enough (although it is less portable). I prefer to use juste .sh file when my only need is to do "npm install" or stuff like that.

这篇关于安装Cordova插件的节点依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:34