{"size": 1569, "ext": "js", "max_stars_repo_path": "scripts/android/afterPluginInstall.js", "max_stars_repo_name": "mkobilev/cordova-plugin-android-fragmentactivity", "max_stars_repo_stars_event_min_datetime": "2018-01-15T14:44:42.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-03T10:12:14.000Z", "max_issues_repo_path": "scripts/android/afterPluginInstall.js", "max_issues_repo_name": "mkobilev/cordova-plugin-android-fragmentactivity", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "scripts/android/afterPluginInstall.js", "max_forks_repo_name": "mkobilev/cordova-plugin-android-fragmentactivity", "max_forks_repo_forks_event_min_datetime": "2018-08-10T09:07:12.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-03T10:12:17.000Z"}
{"max_stars_count": 9, "max_issues_count": null, "max_forks_count": 13, "avg_line_length": 32.6875, "max_line_length": 133, "alphanum_fraction": 0.6934353091}
#!/usr/bin/env node

'use strict';

const fs = require('fs');
const _ = require('lodash');
const xml2js = require('xml2js');
const utilities = require("../lib/utilities");

String.prototype.replaceAll = function(search, replacement) {
  var target = this;
  return target.replace(new RegExp(search, 'g'), replacement);
};

module.exports = function(context) {

  var androidManifestPath = utilities.getAndroidManifestPath(context);

  if (androidManifestPath !== null) {
    const parseString = xml2js.parseString;
    const builder = new xml2js.Builder();
    const manifestPath = androidManifestPath + '/AndroidManifest.xml';
    const androidManifest = fs.readFileSync(manifestPath).toString();

    const activityPath = context.opts.projectRoot + '/plugins/cordova-plugin-android-fragmentactivity/src/android/MainActivity.java';
    const activity = fs.readFileSync(activityPath).toString();

    let manifestRoot, packageName, newActivity, newActivityPath;

    if (androidManifest) {
      parseString(androidManifest, (err, manifest) => {
        if (err) return console.error(err);

        manifestRoot = manifest['manifest'];
        packageName = manifestRoot['$']['package'];

        newActivity = activity.replaceAll(/\$\{mypackage\}/g, packageName);
        newActivityPath = utilities.getAndroidSourcePath(context) + "/" + packageName.replaceAll("\\.", "/") + '/MainActivity.java';

        console.log(newActivityPath);

        fs.writeFileSync(newActivityPath, newActivity);
        console.log("New MainActivity generated.");
      });
    }
  }
};
