All files / license index.ts

100% Statements 22/22
100% Branches 2/2
100% Functions 3/3
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 597x 7x   7x 7x 7x 7x   7x       16x           16x 15x 15x 14x           158x           14x   1x 1x           16x 14x 14x 13x       1x     14x            
import chalk from 'chalk';
import Generator, { Answers } from 'yeoman-generator';
 
import { Filenames } from '../lib/enums/filenames';
import { Messages } from '../lib/enums/messages';
import { Names } from '../lib/enums/names';
import { GithubClient } from '../lib/helpers/github.client';
 
module.exports = class extends Generator {
  answers!: Answers;
 
  async prompting(): Promise<void> {
    this.answers = await this.prompt({
      type: 'confirm',
      name: Names.WITH_LICENSE,
      message: Messages.WITH_LICENSE,
      default: true,
    });
    if (this.answers[Names.WITH_LICENSE]) {
      try {
        const licenses = await GithubClient.getLicenses();
        const licenseAnswer = await this.prompt([
          {
            type: 'list',
            name: Names.LICENSE,
            message: Messages.LICENSE,
            default: 'mit',
            choices: licenses.map((l: any) => ({
              name: l.name,
              value: l.key,
            })),
          },
        ]);
        this.answers[Names.LICENSE] = licenseAnswer[Names.LICENSE];
      } catch (error) {
        this.answers[Names.WITH_LICENSE] = false;
        chalk.yellow('Error while fetching licenses, skipping...');
      }
    }
  }
 
  async writing() {
    if (this.answers[Names.WITH_LICENSE]) {
      try {
        const licenseContent = await GithubClient.getLicense(this.answers[Names.LICENSE]);
        this.fs.copyTpl(this.templatePath(Filenames.LICENSE), this.destinationPath(Filenames.LICENSE), {
          license: licenseContent,
        });
      } catch (error) {
        chalk.yellow('Error while fetching license, skipping...');
      }
 
      this.fs.extendJSON(this.destinationPath(Filenames.PACKAGE_JSON), {
        license: this.answers[Names.LICENSE],
      });
    }
  }
};