prompting()

in templates/typescript/generator-bot-virtualassistant/generators/app/index.js [124:238]


  prompting() {
    this.log(bigBot);
    // Validate language option
    if (this.options.assistantLang) {
      this.options.assistantLang = this.options.assistantLang
        .replace(/\s/g, "")
        .split(",");
      if (
        !this.options.assistantLang.every(language => {
          return languages.includes(language);
        })
      ) {
        this.log.error(
          "ERROR: One of the languages is not recognized, please check your language value\n\t"
        );
        process.exit(1);
      }
    } else {
      this.log.error(
        "ERROR: Language must be selected from the list:\n\t" +
          languages.map(l => `${l.value} -> ${l.name}`).join("\n\t") +
          "\nDefault value: en-us"
      );
      process.exit(1);
    }

    // Generate the main prompts
    const prompts = [
      // Name of the assistant
      {
        type: `input`,
        name: `assistantName`,
        message: `What's the name of your assistant?`,
        default: this.options.assistantName
          ? this.options.assistantName
          : `sample-assistant`
      },
      // Description of the assistant
      {
        type: `input`,
        name: `assistantDesc`,
        message: `What's the description of your assistant?`,
        default: this.options.assistantDesc ? this.options.assistantDesc : ``
      },
      // Language of the assistant
      {
        type: "checkbox",
        name: "assistantLang",
        message: "Which languages will your assistant use?",
        choices: languagesChoice
      },
      // Path of the assistant
      {
        type: `confirm`,
        name: `pathConfirmation`,
        message: `Do you want to change the new assistant's location?`,
        default: false
      },
      {
        when: function(response) {
          return response.pathConfirmation === true;
        },
        type: `input`,
        name: `assistantGenerationPath`,
        message: `Where do you want to generate the assistant?`,
        default: this.options.assistantGenerationPath
          ? this.options.assistantGenerationPath
          : process.cwd(),
        validate: path => {
          if (fs.existsSync(path)) {
            return true;
          }

          this.log(
            chalk.red(
              `\n`,
              `ERROR: This is not a valid path. Please try again.`
            )
          );
        }
      },
      // Final confirmation of the assistant
      {
        type: `confirm`,
        name: `finalConfirmation`,
        message: `Looking good. Shall I go ahead and create your new assistant?`,
        default: true
      }
    ];

    // Check that if it was generated with CLI commands
    if (this.options.noPrompt) {
      this.props = _pick(this.options, [
        `assistantName`,
        `assistantDesc`,
        `assistantLang`,
        `assistantGenerationPath`
      ]);

      // Validate we have what we need, or we'll need to throw
      if (!this.props.assistantName) {
        this.log.error(
          `ERROR: Must specify a name for your assistant when using --noPrompt argument. Use --assistantName or -n option.`
        );
        process.exit(1);
      }

      this.props.finalConfirmation = true;
      return;
    }

    return this.prompt(prompts).then(props => {
      this.props = props;
    });
  }