cordova plugin installation issue when using node

2019-07-04 21:04发布

问题:

Cordova 3.4 hooks is not installing mentioned plugin's properly in iOS . I am adding install_plugins.js into the folder project/project_root/hooks/after_platform_add which has the following code in it:

#!/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);
});

so i when i add platform with command cordova platform add ios, all plugins installed correctly .

After build the project using the command cordova build ios am getting log as ** BUILD SUCCEEDED **

But when i run my project in Xcode am getting following error

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",
  [

  ]
]

Please help me to resolve this issue

回答1:

This issue bit me in the butt too, but I did finally find a solution; here's the deal. The problem is that node.js executes commands asynchronously by default. Great for web servers, not so much for shell scripts! So as you're issuing the "cordova plugin add [your plugin]" commands one after the other, you're ending up with a bunch of them executing simultaneously and they are stepping on each other while iterating over the list of installed plugins and rewriting the file (cordova_plugins.js). You can actually watch that happen if you add the "--verbose" switch to your command (so "cordova plugin add [your plugin] --verbose". Node.js did not get the capability to execute commands synchronously until 0.12 (it's the execSync command), and as of this writing the latest stable version is 0.10. If you're working with Cordova in 2015 you've most likely got 0.10, so you will need to install a package like shelljs or exec-sync to get that functionality. So using shelljs, on the command line you would:

[sudo] npm install shelljs

And in your hook script replace all of this:

// 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);
});

With this:

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


回答2:

Now that execSync is available in the latest stable nodejs (v0.12.4 as of writing), you can do this in your hook:

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