钩子文件夹中使用节点脚本时的cordova插件安装问题

cordova3.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项目获取日志为** 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", [ ] ] 

请帮我解决这个问题

这个问题也困扰我,但我终于find了解决办法; 这是交易。 问题是node.js默认情况下是asynchronous执行命令的。 非常适合web服务器,而不是shell脚本! 因此,当你发出“cordova plugin add [你的插件]”命令一个接一个的时候,你会得到一堆同时执行的代码,它们在迭代已安装的插件列表的同时,重写文件(cordova_plugins.js)。 如果你在你的命令中添加了“–verbose”开关,你实际上可以观察到这种情况(所以“cordova plugin add [your plugin] –verbose”。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"); }); 

现在,execSync在最新的稳定nodejs中可用(v0.12.4刚刚起步),你可以在你的hook中做到这一点:

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