{"size": 1376, "ext": "js", "max_stars_repo_path": "examples/radish34/deploy/src/utils/SettingsSaver.js", "max_stars_repo_name": "ShipChain/baseline", "max_stars_repo_stars_event_min_datetime": "2020-03-19T16:04:37.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-28T11:55:25.000Z", "max_issues_repo_path": "examples/radish34/deploy/src/utils/SettingsSaver.js", "max_issues_repo_name": "ShipChain/baseline", "max_issues_repo_issues_event_min_datetime": "2020-03-19T19:16:32.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-23T17:39:12.000Z", "max_forks_repo_path": "examples/radish34/deploy/src/utils/SettingsSaver.js", "max_forks_repo_name": "ShipChain/baseline", "max_forks_repo_forks_event_min_datetime": "2020-03-19T16:06:40.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-27T19:57:11.000Z"}
{"max_stars_count": 533, "max_issues_count": 220, "max_forks_count": 216, "avg_line_length": 26.9803921569, "max_line_length": 136, "alphanum_fraction": 0.6911337209}
const fs = require('fs');

/**
 * Radish specific class for saving settings to configuration file
 */
class SettingsSaver {

  constructor(configDir, rpcProvider) {
    if (typeof configDir === 'undefined') {
      throw new Error('Organisational config directory not supplied');
    }
    this.configDir = configDir;
    this.rpcProvider = rpcProvider;
  }

  async updateSettings(organisationName, organisationAddress, messagingEndpoint, messagingKey, zkpPublicKey, zkpPrivateKey, addresses) {
    const organisationConfigPath = `${this.configDir}/config-${organisationName}.json`;
    const {
      organization
    } = JSON.parse(fs.readFileSync(organisationConfigPath));

    const newOrganization = {
      ...organization,
      zkpPublicKey,
      zkpPrivateKey,
      address: organisationAddress,
      messagingEndpoint: messagingEndpoint,
      messagingKey: messagingKey,
    }

    const settings = {
      rpcProvider: this.rpcProvider,
      organization: newOrganization,
      addresses,
    }

    try {
      fs.writeFileSync(organisationConfigPath, JSON.stringify(settings, null, 2));
      console.log(`Updated settings in file ${organisationConfigPath}`);
    } catch (err) {
      console.log(`Error writing to settings file ${organisationConfigPath}`);
      return process.exit(1);
    }

    return settings;
  }

}

module.exports = SettingsSaver;
