Search code examples
node.jscordovahookcordova-plugins

Get cordova package name from javascript hook


I am writing a cordova plugin with a node hook to run after_prepare .

This is for Android only.

From within this hook I need to get the cordova package name, so I can copy a file to the src/com/example/myproject folder (if the package is com.example.myproject).

If I know the package name I can make this path. I have it working hardcoded now but I need this to work with two different package names.

Is there a way to get the package name from within this code in the plugin hook?

module.exports = function(ctx){
   var fs = ctx.requireCordovaModule('fs');
   var path = ctx.requireCordovaModule('path');
   var deferral = ctx.requireCordovaModule('q').defer();

   //get package name here

   //do other stuff
}

I have done a lot of research but have not been able to find this.

Thanks.


Solution

  • It doesn't look like it is available off of the context object, but you could try to parse the config.xml.

    module.exports = function(context) {
        var fs = require('fs');
        var path = require('path');
        var config_xml = path.join(context.opts.projectRoot, 'config.xml');
        var et = context.requireCordovaModule('elementtree');
    
        var data = fs.readFileSync(config_xml).toString();
        var etree = et.parse(data);
    
        console.log(etree.getroot().attrib.id);
    };
    

    The local-webserver plugin uses a similar strategy for reading config properties.