Search code examples
cordovaionic-frameworkcordova-plugins

Plugins not installing during platform add with ionic/cordova on ios


When adding a platform in ionic with plugins already defined in package.json a random set of plugins is installed and referenced in plugins/ios.json.

As a concrete example, my package.json contains this:

cordovaPlugins: [
    "com.danielcwilson.plugins.googleanalytics",
    "com.ionic.keyboard",
    "cordova-plugin-console",
    "cordova-plugin-device",
    "uk.co.whiteoctober.cordova.appversion",
    "com.paypal.cordova.mobilesdk",
    "nl.x-services.plugins.flashlight",
    "cordova-plugin-inappbrowser",
    "https://github.com/wildabeast/BarcodeScanner"
]

On the first run, the plugins/ios.json contains:

com.danielcwilson.plugins.googleanalytics
com.paypal.cordova.mobilesdk
com.phonegap.plugins.barcodescanner
cordova-plugin-console
nl.x-services.plugins.flashlight
uk.co.whiteoctober.cordova.appversion

If I clear the platforms and plugins folder and re-add the platform, then on second run I get:

com.ionic.keyboard
com.paypal.cordova.mobilesdk
cordova-plugin-console
cordova-plugin-device
cordova-plugin-inappbrowser
nl.x-services.plugins.flashlight
uk.co.whiteoctober.cordova.appversion

and so on.

During the ionic platform add ios step I do see all the plugins being added; however occasionally the Running command: /path/to/project/hooks/after_platform_add/010_install_plugins.js step will not run.

Is there a reliable way to install the plugins?

I am using:

| What    | Version |
| :------ | :------ |
| node    | 0.10.39 |
| cordova | 5.1.1   |
| ionic   | 1.6.1   |

contents of 010_install_plugins.sh as requested:

#!/usr/bin/env node

/**
 * Install all plugins listed in package.json
 * https://raw.githubusercontent.com/diegonetto/generator-ionic/master/templates/hooks/after_platform_add/install_plugins.js
 */
var exec = require('child_process').exec;
var path = require('path');
var sys = require('sys');

var packageJSON = null;

try {
  packageJSON = require('../../package.json');
} catch(ex) {
  console.log('\nThere was an error fetching your package.json file.')
  console.log('\nPlease ensure a valid package.json is in the root of this project\n')
  return;
}

var cmd = process.platform === 'win32' ? 'cordova.cmd' : 'cordova';
// var script = path.resolve(__dirname, '../../node_modules/cordova/bin', cmd);

packageJSON.cordovaPlugins = packageJSON.cordovaPlugins || [];
packageJSON.cordovaPlugins.forEach(function (plugin) {
  exec('cordova plugin add ' + plugin, function (error, stdout, stderr) {
    sys.puts(stdout);
  });
});

Which is the default with ionic.


Solution

  • It turns out that the bug was in Ionic itself.

    The hook file was running all plugin installs asyncronously, meaning that files were being written to concurrently so changes were being lost.

    I've submitted a pull request to ionic-cli to fix the issue here. The change was to replace:

     packageJSON.cordovaPlugins.forEach(function (plugin) {
       exec('cordova plugin add ' + plugin, function (error, stdout, stderr) {
         sys.puts(stdout);
       });
    });
    

    with:

    function installNextPlugin() {
        var curPlugin = packageJSON.cordovaPlugins.shift();
        if (curPlugin) {
            exec('cordova plugin add ' + curPlugin, function(err, stdout, stderr) {
                sys.puts(stdout);
                sys.puts(stderr);
            })
                .on("exit", function(code) {
                    if (code) {
                        console.log("'cordova plugin add " + curPlugin + "' failed with code '" + code + "'");
                        process.exit(code);
                    } else {
                        installNextPlugin();
                    }
                });
        }
    }
    
    installNextPlugin();
    

    in 010_install_plugins.js.