在hooks文件夹中使用节点脚本时出现cordova插件安装问题

时间:2014-07-22 06:18:01

标签: ios cordova phonegap-plugins cordova-plugins

Cordova 3.4挂钩未在iOS中正确安装提到的插件。我将install_plugins.js添加到文件夹project/project_root/hooks/after_platform_add中,其中包含以下代码:

#!/usr/bin/env node

//this hook installs all your plugins

// add your plugins to this list--either the identifier, the filesystem location or the URL
// It can also be git url like "https://github.com/chrisekelley/AppPreferences/"
var pluginlist = [
    "org.apache.cordova.camera",
    "org.apache.cordova.console",
    "org.apache.cordova.contacts",
    "org.apache.cordova.device",
    "org.apache.cordova.dialogs",
    "org.apache.cordova.file",
    "org.apache.cordova.file-transfer",
    "org.apache.cordova.geolocation",
    "org.apache.cordova.globalization",
    "org.apache.cordova.media",
    "org.apache.cordova.media-capture",
    "org.apache.cordova.network-information",
    "org.apache.cordova.splashscreen",
    "org.apache.cordova.statusbar"
];

// no need to configure below

var fs = require('fs');
var path = require('path');
var sys = require('sys')
var exec = require('child_process').exec;

function puts(error, stdout, stderr) {
    sys.puts(stdout)
}

pluginlist.forEach(function(plug) {
    exec("cordova plugin add " + plug, puts);
});

所以当我使用命令cordova platform add ios添加平台时,所有插件都正确安装。

使用命令cordova build ios构建项目后,我的日志为** BUILD SUCCEEDED **

但是当我在Xcode中运行我的项目时出现以下错误

2014-07-22 11:42:00.960 Totter[2788:90b] CDVPlugin class CDVDevice (pluginName: Device) does not exist.
2014-07-22 11:42:00.961 Totter[2788:90b] ERROR: Plugin 'Device' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.
2014-07-22 11:42:00.963 Totter[2788:90b] -[CDVCommandQueue executePending] [Line 158] FAILED pluginJSON = [
  "Device1460086973",
  "Device",
  "getDeviceInfo",
  [

  ]
]
2014-07-22 11:42:00.964 Totter[2788:90b] CDVPlugin class CDVConnection (pluginName: NetworkStatus) does not exist.
2014-07-22 11:42:00.965 Totter[2788:90b] ERROR: Plugin 'NetworkStatus' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.
2014-07-22 11:42:00.965 Totter[2788:90b] -[CDVCommandQueue executePending] [Line 158] FAILED pluginJSON = [
  "NetworkStatus1460086974",
  "NetworkStatus",
  "getConnectionInfo",
  [

  ]
]

请帮我解决此问题

2 个答案:

答案 0 :(得分:2)

这个问题也让我陷入困境,但我终于找到了解决方案;这是合约。问题是node.js默认情况下异步执行命令。非常适合Web服务器,而不是shell脚本!因此,当您发布" cordova插件时,添加[您的插件]"一个接一个地命令,你最终会同时执行一堆并且他们互相踩踏,同时迭代已安装的插件列表并重写文件(cordova_plugins.js)。如果你添加" - verbose"你可以实际观察到这种情况。切换到你的命令(所以" cordova插件添加[你的插件] --verbose&#34 ;.Node.js没有同步执行命令的能力,直到0.12(它是execSync命令),并且在撰写本文时,最新的稳定版本为0.10。如果您在2015年与Cordova合作,您很可能获得0.10,因此您需要安装shelljs或exec-sync等软件包才能获得该功能所以使用shelljs,在命令行中你会:

[sudo] npm install shelljs

在你的钩子脚本中替换所有这些:

// no need to configure below

var fs = require('fs');
var path = require('path');
var sys = require('sys')
var exec = require('child_process').exec;

function puts(error, stdout, stderr) {
    sys.puts(stdout)
}

pluginlist.forEach(function(plug) {
    exec("cordova plugin add " + plug, puts);
});

有了这个:

var execSync = require("shelljs").exec;
pluginlist.forEach(function(plugin) {
    execSync("cordova plugin add " + plugin + " --verbose");
});

答案 1 :(得分:1)

既然execSync在最新的稳定nodejs(写作时为v0.12.4)中可用,你可以在你的钩子中执行此操作:

var execSync = require('child_process').execSync;
execSync("cordova plugin add " + plugin)