<commit_before>import fs from 'fs';
import path from 'path';

export default class {

  constructor(hostname, port, ssl) {
    this.hostname = hostname;
    this.port = port;
    this.ssl = ssl;

    this.mutableProperties = {
      "workDir": 'string',
      "port": 'integer',
      "hostname": 'string',
      "ssl": 'boolean',
      "sslkey": "string",
      "sslcert": "string"
    };
  }

  listConfig(configLocation) {
    console.log('Config location: ' + configLocation);
    fs.readFile(configLocation, (err, configData) => {
      console.log("Config configuration:\n", JSON.parse(configData));
    });
  }

  updateProp(property, value) {
    let configPath = path.join(__dirname, '../..', 'config.json');
    let configString = fs.readFileSync(configPath);
    let config = JSON.parse(configString);

    if (config.hasOwnProperty(property) !== true) {
      console.log("Error, property does not exist");
      return;
    } else if (this.mutableProperties.hasOwnProperty(property) !== true) {
      console.log("Error, cannot change this property from cli\n" +
        "changing all properties should be done using a config file");
      return;
    }

    // Convert values
    if (value === 'true') {
      value = true;
    } else if (value === 'false') {
      value = false;
    }

    if (typeof value !== this.mutableProperties[property]) {
      console.log("Value is incorrect, aborting\n");
      return;
    }

    if (property === 'ssl') {
      config.ssl.enabled = value;
    }
    else if (property === 'sslkey') {
      config.ssl.sslkey = value;
    }
    else if(property === 'sslcert') {
      config.ssl.sslcert = value;
    }

    else {
      config[property] = value;
    }

    fs.writeFile(configPath, JSON.stringify(config, null, 2), () => {
      console.log("Property: '"+ property + "' has been updated: " + value);
    });

  }

}<commit_msg>New feature: set any setting in config using ap-npm config<commit_after>import fs from 'fs';
import path from 'path';

export default class {

  constructor(hostname, port, ssl) {
    this.hostname = hostname;
    this.port = port;
    this.ssl = ssl;
    this.configPath = path.join(__dirname, '../..', 'config.json');
  }

  listConfig(configLocation) {
    console.log('Config location: ' + configLocation);
    fs.readFile(configLocation, (err, configData) => {
      console.log("Config:\n", JSON.parse(configData));
    });
  }

  updateProp(property, value) {
    let propArgs = property.split('.');

    fs.readFile(this.configPath, (err, file) => {
      if (err) {
        console.log("Could not update config:" + err);
        return 1;
      }

      let config = JSON.parse(file);
      switch (propArgs.length) {
        case 1:
          if (config.hasOwnProperty(propArgs[0])) {
            config[propArgs[0]] = this.convertValue(value);
            break;
          }
          console.log("Unknown property: " + property);
          return 2;
        case 2:
          if (config.hasOwnProperty(propArgs[0])) {
            if (config[propArgs[0]]
              .hasOwnProperty(propArgs[1])
            ) {
              config[propArgs[0]][propArgs[1]] = this.convertValue(value);
              break;
            }
          }
          console.log("Unknown property: " + property);
          return 2;
        case 3:
          if (config.hasOwnProperty(propArgs[0])) {
            if (config[propArgs[0]]
              .hasOwnProperty(propArgs[1])
            ) {
              if (config[propArgs[0]][propArgs[1]]
                .hasOwnProperty(propArgs[2])
              ) {
                config[propArgs[0]][propArgs[1]][propArgs[2]] = this.convertValue(value);
                break;
              }
            }
          }
          console.log("Unknown property: " + property);
          return 2;
        default:
          console.log("Unknown property: " + property);
          return 2;
      }

      fs.writeFile(this.configPath, JSON.stringify(config, null, 2), () => {
        console.log("Property: '" + property + "' has been updated: " + value);
      });
    });
  }

  convertValue(value) {
    if (value === 'true') {
      return true;
    } else if (value === 'false') {
      return false;
    }
    return value;
  }

}
{'fraction_non_alphanumeric': 0.14940098661028894, 'fraction_numerical': 0.00610758750293634, 'mean_word_length': 2.1705137751303054, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6777949', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1269, 'n_words': 276}