使用npm + package.json管理Cordova插件 [英] Manage cordova plugins with npm + package.json

查看:245
本文介绍了使用npm + package.json管理Cordova插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Angular + Ionic + Cordova项目,其中有多个开发人员,我们希望为其管理Cordova插件依赖项。我们正在使用 Cordova CLI 5+ ,并在手动运行安装命令时(例如, cordova插件添加cordova-plugin-camera ),新行将添加到部分c $ c> package.json 文件。成品如下所示:

  cordovaPlugins:[
cordova-plugin-camera,
cordova-plugin-console,
cordova-plugin-contacts,
cordova-plugin-device,
cordova-plugin-dialogs,
cordova-plugin-file,
cordova-plugin-geolocation,
cordova-plugin-media,
cordova-plugin-media-capture,
cordova-plugin-network-information,
cordova-plugin-splashscreen,
cordova-plugin-statusbar,
cordova-plugin-vibration,
com.ionic.keyboard
]

这一切都很棒,除了我们可以找不到任何方法让开发人员2到npm安装这些插件-相反,他必须单独运行命令,然后在 package.json 中添加重复的行,资料库。我们确定必须有安装这些的命令,但找不到。谁能阐明一些想法?

解决方案

导致我们问题的原因



我们最初使用此 Ionic + Cordova + Grunt种子项目来生成我们的初始应用程序。 项目包括许多 Cordova钩子除其他功能外,还可以从相关的 cordovaPlatforms 中添加和删除平台和插件。运行相应的命令(即<$ c $)时,在 package.json 中的code>和 cordovaPlugins 部分中c> cordova插件添加cordova-plugin-media cordovaPlugins )添加一行。



<为了更好地支持本地测试(例如,尝试使用新版本的插件)并防止跨开发依赖问题,我们禁用了种子项目挂钩,现在手工制作了 package.json



正确管理Cordova插件



事实证明,离子CLI 使用 package.json 进行管理Cordova应用程序在平台和插件方面的状态(自版本1.3.19 而言)



填充有两个部分的 package.json cordovaPlatforms cordovaPlugins 使我们能够进行简单的离子状态还原来使Cordova环境处于良好的仿真状态,建筑物等。



指定版本



进一步锁定我们的应用的状态和开发环境,我们还通过添加版本号来指定了所使用的Cordova平台和插件的目标版本。我们使用的是:

  {
...
cordovaPlatforms:[
android@4.0.2,
ios@3.8.0
],
cordovaPlugins:[
cordova-plugin-camera@1.1.0,
cordova-plugin-contacts@1.1.0,
cordova-plugin-device@1.0.1,
cordova-plugin-file@2.1.0,
cordova-plugin-media@1.0.1,
cordova-plugin-media-capture@1.0.1,
cordova-plugin-network-information@1.0.1,
cordova-plugin-splashscreen@2.1.0,
cordova-plugin-statusbar@1.0.1,
cordova-plugin-vibration@1.2.0,
com.ionic.keyboard@1.0.5
]
}

tl; dr



一旦您在 package.json 中包含以上内容,然后可以通过 ionic state restore (v1.3.19 +)确保本地环境处于正确的状态,该版本将通过 package.json进行咀嚼并根据需要安装平台和插件


We have an Angular + Ionic + Cordova project with multiple devs that we'd like to manage cordova plugin dependencies for. We are using Cordova CLI 5+, and when manually running the install commands (e.g. cordova plugin add cordova-plugin-camera), a new line gets added to the cordovaPlugins section of the package.json file. Here's what the finished product looks like:

"cordovaPlugins": [
  "cordova-plugin-camera",
  "cordova-plugin-console",
  "cordova-plugin-contacts",
  "cordova-plugin-device",
  "cordova-plugin-dialogs",
  "cordova-plugin-file",
  "cordova-plugin-geolocation",
  "cordova-plugin-media",
  "cordova-plugin-media-capture",
  "cordova-plugin-network-information",
  "cordova-plugin-splashscreen",
  "cordova-plugin-statusbar",
  "cordova-plugin-vibration",
  "com.ionic.keyboard"
]

That's all great, except we can't find any way for dev #2 to npm install those plugins - instead, he has to run the commands individually, which then adds a duplicate line to package.json, dirtying the repository. We are sure there must be a command to install these, but can't find it. Can anyone shed some light?

解决方案

What Caused Our Issue

We had originally used this Ionic + Cordova + Grunt seed project to spawn our initial app. The project includes a number of Cordova hooks that, among other things, add and remove platforms and plugins from the relevant cordovaPlatforms and cordovaPlugins sections in package.json when you run the corresponding command (i.e. cordova plugin add cordova-plugin-media adds a line to cordovaPlugins).

To better support local testing (trying new versions of a plugin, for example), and to prevent cross-dev dependency issues, we disabled the seed project hook and now hand-craft the package.json as needed.

Properly Managing Cordova Plugins

In turns out, the Ionic CLI uses package.json to manage Cordova app state in terms of platforms and plugins (as of version 1.3.19, it appears).

Populating package.json with two sections, cordovaPlatforms and cordovaPlugins has allowed us to do a simple ionic state restore to get the Cordova environment in shape for emulation, building, etc.

Specifying Versions

To further lock-down our app's state and development environment, we've also specified the target version of the Cordova platforms and plugins that we are using by adding the version number. Here's what we use:

{
  ...
  "cordovaPlatforms": [
    "android@4.0.2",
    "ios@3.8.0"
  ],
  "cordovaPlugins": [
    "cordova-plugin-camera@1.1.0",
    "cordova-plugin-contacts@1.1.0",
    "cordova-plugin-device@1.0.1",
    "cordova-plugin-file@2.1.0",
    "cordova-plugin-media@1.0.1",
    "cordova-plugin-media-capture@1.0.1",
    "cordova-plugin-network-information@1.0.1",
    "cordova-plugin-splashscreen@2.1.0",
    "cordova-plugin-statusbar@1.0.1",
    "cordova-plugin-vibration@1.2.0",
    "com.ionic.keyboard@1.0.5"
  ]
}

tl;dr

Once you have the above in your package.json, you can then ensure that your local environment is in the proper state via ionic state restore (v1.3.19+) which will chew through package.json and install platforms and plugins as needed.

这篇关于使用npm + package.json管理Cordova插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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