<commit_before>import * as inquirer from 'inquirer';

import Config from '../common/config';
import Connection from '../common/connection';
import { IConnection } from '../common/interfaces';
import { PathChoices } from './eums';
import { InitOptions } from './interfaces';

export default class Init {

  /**
   * Invoke action.
   *
   * @param options CLI options.
   */
  public invoke(options: InitOptions): void {
    const webConfigConns: Connection[] = Config.getConnectionsFromWebConfig(options.webconfig);
    const conn: Connection = new Connection();

    if (!options.force && Config.doesDefaultExist()) {
      // don't overwrite existing config file
      return console.error('Config file already exists!');
    }

    if (webConfigConns) {
      // use options from web config
      conn.loadFromObject(webConfigConns[0]);
    }

    if (options.skip) {
      // skip prompts and create with defaults
      Config.write({ connections: options.webconfig || [conn] });
      return;
    }

    inquirer.prompt(this.getQuestions(conn, !!webConfigConns))
      .then(answers => this.writeFiles(answers));
  }

  /**
   * Get all applicable questions.
   *
   * @param conn Connection object to use for default values.
   */
  private getQuestions(conn: Connection, showWebConfig: boolean): inquirer.Questions {
    return [
      {
        name: 'path',
        message: 'Where would you like to store connections?',
        type: 'list',
        choices: () => {
          const choices: object[] = [
            {
              name: 'Main configuration file.',
              value: PathChoices.SscConfig
            },
            {
              name: 'Separate connections configuration file.',
              value: PathChoices.ConnsConfig
            }
          ];

          if (showWebConfig) {
            choices.push({
              name: 'Web.config file with connection strings.',
              value: PathChoices.WebConfig
            });
          }

          return choices;
        }
      },
      {
        name: 'server',
        message: 'Server URL.',
        default: (conn.server || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'port',
        message: 'Server port.',
        default: (conn.port || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'database',
        message: 'Database name.',
        default: (conn.database || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'user',
        message: 'Login username.',
        default: (conn.user || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'password',
        message: 'Login password.',
        type: 'password',
        default: (conn.password || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'name',
        message: 'Connection name.',
        default: 'dev',
        when: answers => (answers.path !== PathChoices.WebConfig)
      }
    ];
  }

  /**
   * From configuration files(s) based on answers.
   *
   * @param answers Answers from questions.
   */
  private writeFiles(answers: inquirer.Answers): void {
    const conn: IConnection = {
      name: answers.name,
      server: answers.server,
      port: answers.port,
      database: answers.database,
      user: answers.user,
      password: <PASSWORD>
    };

    if (answers.path === PathChoices.WebConfig) {
      Config.write({ connections: Config.defaultWebConfigFile });
    } else if (answers.path === PathChoices.ConnsConfig) {
      Config.write({ connections: Config.defaultConnectionsJsonFile });
      Config.write({ connections: [conn] }, Config.defaultConnectionsJsonFile);
    } else {
      Config.write({ connections: [conn] });
    }
  }
}<commit_msg>chore: move path choices to own method<commit_after>import * as inquirer from 'inquirer';

import Config from '../common/config';
import Connection from '../common/connection';
import { IConnection } from '../common/interfaces';
import { PathChoices } from './eums';
import { InitOptions } from './interfaces';

export default class Init {

  /**
   * Invoke action.
   *
   * @param options CLI options.
   */
  public invoke(options: InitOptions): void {
    const webConfigConns: Connection[] = Config.getConnectionsFromWebConfig(options.webconfig);
    const conn: Connection = new Connection();

    if (!options.force && Config.doesDefaultExist()) {
      // don't overwrite existing config file
      return console.error('Config file already exists!');
    }

    if (webConfigConns) {
      // use options from web config
      conn.loadFromObject(webConfigConns[0]);
    }

    if (options.skip) {
      // skip prompts and create with defaults
      Config.write({ connections: options.webconfig || [conn] });
      return;
    }

    inquirer.prompt(this.getQuestions(conn, !!webConfigConns))
      .then(answers => this.writeFiles(answers));
  }

  /**
   * Get all applicable questions.
   *
   * @param conn Connection object to use for default values.
   */
  private getQuestions(conn: Connection, showWebConfig: boolean): inquirer.Questions {
    return [
      {
        name: 'path',
        message: 'Where would you like to store connections?',
        type: 'list',
        choices: () => this.getPathChoices(showWebConfig)
      },
      {
        name: 'server',
        message: 'Server URL.',
        default: (conn.server || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'port',
        message: 'Server port.',
        default: (conn.port || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'database',
        message: 'Database name.',
        default: (conn.database || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'user',
        message: 'Login username.',
        default: (conn.user || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'password',
        message: 'Login password.',
        type: 'password',
        default: (conn.password || undefined),
        when: answers => (answers.path !== PathChoices.WebConfig)
      },
      {
        name: 'name',
        message: 'Connection name.',
        default: 'dev',
        when: answers => (answers.path !== PathChoices.WebConfig)
      }
    ];
  }

  /**
   * Get all available configuration file path choices.
   *
   * @param showWebConfig Indicates if Web.config choice should be available.
   */
  private getPathChoices(showWebConfig: boolean): inquirer.ChoiceType[] {
    const choices: inquirer.ChoiceType[] = [
      {
        name: 'Main configuration file.',
        value: PathChoices.SscConfig
      },
      {
        name: 'Separate connections configuration file.',
        value: PathChoices.ConnsConfig
      }
    ];

    if (showWebConfig) {
      choices.push({
        name: 'Web.config file with connection strings.',
        value: PathChoices.WebConfig
      });
    }

    return choices;
  }

  /**
   * From configuration files(s) based on answers.
   *
   * @param answers Answers from questions.
   */
  private writeFiles(answers: inquirer.Answers): void {
    const conn: IConnection = {
      name: answers.name,
      server: answers.server,
      port: answers.port,
      database: answers.database,
      user: answers.user,
      password: <PASSWORD>
    };

    if (answers.path === PathChoices.WebConfig) {
      Config.write({ connections: Config.defaultWebConfigFile });
    } else if (answers.path === PathChoices.ConnsConfig) {
      Config.write({ connections: Config.defaultConnectionsJsonFile });
      Config.write({ connections: [conn] }, Config.defaultConnectionsJsonFile);
    } else {
      Config.write({ connections: [conn] });
    }
  }
}
{'fraction_non_alphanumeric': 0.12971902463176135, 'fraction_numerical': 0.00024755539051862855, 'mean_word_length': 2.3087633087633086, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '2184783', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2215, 'n_words': 571}