Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ export function extractListFormat(format: string): BaseVariableFormat {
throw new Error(`Invalid list format value. Received "${listValue}"`);
}
}

/**
* Extracts the format wrapped in `enum()` or throws an error if it's an invalid format
* @param format a format string wrapped with `enum()`
*/
export function extractEnumFormat(format: string): BaseVariableFormat {
let enumValue = removePrefix('enum(', format);
enumValue = enumValue.substr(0, enumValue.length - 1).trim();
const enumValues = enumValue.split(',');
if (enumValues.length > 0) {
return enumValue as BaseVariableFormat;
} else {
throw new Error(`Invalid enum format value. Received "${enumValue}"`);
}
}

/**
* Extracts the formats wrapped in `map()` or throws an error if one is an invalid format
Expand Down Expand Up @@ -173,6 +188,10 @@ export function textToFormat(text: string): VariableFormat {
// file format
const fileValueFormat = extractFileFormat(sanitizedText);
return `file(${fileValueFormat})`;
} else if (sanitizedText.startsWith('enum(') && sanitizedText.endsWith(')')) {
// file format
const enumValueFormat = extractEnumFormat(sanitizedText);
return `enum(${enumValueFormat})`;
}

throw new Error(`Invalid format. Received: "${text}"`);
Expand Down
33 changes: 30 additions & 3 deletions src/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import prompts, { Answers, PromptObject, PromptType } from 'prompts';
import prompts, { Answers, Choice, PromptObject, PromptType } from 'prompts';
import { WriteStream } from 'tty';
import { ParseResult, VariableFormat } from './parser';
import { extractEnumFormat, ParseResult, VariableFormat } from './parser';
import { getValidator } from './validators';

export function getPromptType(format: VariableFormat): PromptType {
if(format.startsWith('enum(')){
return 'select';
}
switch (format) {
case 'secret':
return 'invisible';
Expand All @@ -14,6 +17,29 @@ export function getPromptType(format: VariableFormat): PromptType {
return 'text';
}
}

export function getChoices(format: VariableFormat): Choice[] | undefined {
if(format.startsWith('enum(')){
const enumValues = extractEnumFormat(format);
return enumValues.split(',').map(possibleValue => ({
title: possibleValue,
value: possibleValue
}))
}
}

export function getInitial(format: VariableFormat, initialValue: string | null): any {
if(format.startsWith('enum(')){
const enumValues = extractEnumFormat(format);
const values = enumValues.split(',');
for(let [index, value] of values.entries()) {
if(value === initialValue) {
return index;
}
}
}
return initialValue;
}

export function createQuestions(
parsedExample: ParseResult,
Expand All @@ -26,10 +52,11 @@ export function createQuestions(
type: getPromptType(entry.format),
name: entry.key,
message: entry.description || `Please enter a value for ${entry.key}`,
initial: entry.default || undefined,
initial: getInitial(entry.format, entry.default),
validate: getValidator(entry.format),
stdout: promptStream,
float: entry.format === 'number' ? true : undefined,
choices: getChoices(entry.format),
};
});
}
Expand Down