UNPKG

3.51 kBPlain TextView Raw
1import inquirer from 'inquirer';
2
3import { CommandTestRunner } from '../test-runner/command-test-runner.js';
4
5import { ChoiceType } from './choice-type.js';
6import { Preset } from './presets/preset.js';
7import { PromptOption } from './prompt-option.js';
8
9export interface PromptResult {
10 additionalNpmDependencies: string[];
11 additionalConfig: Record<string, unknown>;
12}
13
14export class StrykerInquirer {
15 public async promptPresets(options: Preset[]): Promise<Preset | undefined> {
16 const choices: ChoiceType[] = options.map((_) => _.name);
17 choices.push(new inquirer.Separator());
18 choices.push('None/other');
19 const answers = await inquirer.prompt<{ preset: string }>({
20 choices,
21 message: 'Are you using one of these frameworks? Then select a preset configuration.',
22 name: 'preset',
23 type: 'list',
24 });
25 return options.find((_) => _.name === answers.preset);
26 }
27
28 public async promptTestRunners(options: PromptOption[]): Promise<PromptOption> {
29 if (options.length) {
30 const choices: ChoiceType[] = options.map((_) => _.name);
31 choices.push(new inquirer.Separator());
32 choices.push(CommandTestRunner.runnerName);
33 const answers = await inquirer.prompt<{ testRunner: string }>({
34 choices,
35 message:
36 'Which test runner do you want to use? If your test runner isn\'t listed here, you can choose "command" (it uses your `npm test` command, but will come with a big performance penalty)',
37 name: 'testRunner',
38 type: 'list',
39 });
40 return options.filter((_) => _.name === answers.testRunner)[0] ?? { name: CommandTestRunner.runnerName, pkg: null };
41 } else {
42 return { name: CommandTestRunner.runnerName, pkg: null };
43 }
44 }
45
46 public async promptBuildCommand(skip: boolean): Promise<PromptOption> {
47 const { buildCommand } = await inquirer.prompt<{ buildCommand: string }>({
48 message:
49 'What build command should be executed just before running your tests? For example: "npm run build" or "tsc -b" (leave empty when this is not needed).',
50 name: 'buildCommand',
51 default: 'none',
52 when: !skip,
53 });
54
55 return { name: buildCommand !== 'none' ? buildCommand : '', pkg: null };
56 }
57
58 public async promptReporters(options: PromptOption[]): Promise<PromptOption[]> {
59 const answers = await inquirer.prompt<{ reporters: string[] }>({
60 choices: options.map((_) => _.name),
61 default: ['html', 'clear-text', 'progress'],
62 message: 'Which reporter(s) do you want to use?',
63 name: 'reporters',
64 type: 'checkbox',
65 });
66 return options.filter((option) => answers.reporters.some((reporterName) => option.name === reporterName));
67 }
68
69 public async promptPackageManager(options: PromptOption[]): Promise<PromptOption> {
70 const answers = await inquirer.prompt<{ packageManager: string }>({
71 choices: options.map((_) => _.name),
72 default: ['npm'],
73 message: 'Which package manager do you want to use?',
74 name: 'packageManager',
75 type: 'list',
76 });
77 return options.filter((_) => _.name === answers.packageManager)[0];
78 }
79
80 public async promptJsonConfigType(): Promise<boolean> {
81 const json = 'JSON';
82
83 const answers = await inquirer.prompt<{ configType: string }>({
84 choices: [json, 'JavaScript'],
85 default: json,
86 message: 'What file type do you want for your config file?',
87 name: 'configType',
88 type: 'list',
89 });
90 return answers.configType === json;
91 }
92}