Skip to content

Commit

Permalink
feat: "convertType" for input of type promptString
Browse files Browse the repository at this point in the history
  • Loading branch information
usernamehw committed Aug 26, 2023
1 parent 9553d3a commit e15cd33
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
7 changes: 7 additions & 0 deletions data/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@
"description": {
"type": "string"
},
"convertType": {
"type": "string",
"enum": [
"boolean",
"number"
]
},
"default": {
"type": "string",
"description": "Default value that will be used if the user doesn't enter something else."
Expand Down
33 changes: 29 additions & 4 deletions src/substituteVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,34 @@ const monthNamesShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
*
* TODO: throw errors (window.showMessage) when variable exists but can't resolve
*/
export async function substituteVariables(strArg: string, inputs: Inputs | undefined): Promise<string> {
export async function substituteVariables(strArg: string, inputs: Inputs | undefined): Promise<boolean | number | string> {
if (isSingleVariable(strArg)) {
return replaceSingleVariable(strArg.slice(2, -1), inputs);
}
const replacedString = await utils.replaceAsync(strArg, /\$\{[^}]+\}/giu, async match => {
const variableName = match.slice(2, -1);// Remove `${` and `}` from match
return replaceSingleVariable(variableName, inputs);
return String(await replaceSingleVariable(variableName, inputs));
});

return replacedString;
}
/**
* Return true when string contains only 1 varialbe.
*/
function isSingleVariable(text: string): boolean {
if (text.startsWith('${') && text.endsWith('}')) {
const variableText = text.slice(2, -1);
if (variableText.includes('$') ||
variableText.includes('{') ||
variableText.includes('}')) {
return false;
}
return true;
}
return false;
}

async function replaceSingleVariable(variableName: string, inputs: Inputs | undefined): Promise<string> {
async function replaceSingleVariable(variableName: string, inputs: Inputs | undefined): Promise<boolean | number | string> {
const activeTextEditor = window.activeTextEditor;
const workspaceFolderFsPath = workspace.workspaceFolders?.[0].uri.fsPath;

Expand Down Expand Up @@ -295,7 +313,7 @@ async function replaceCommandVariable(commandId: string, args: unknown): Promise

return String(commandReturnValue);
}
async function replaceInputVariable(inputName: string, inputs: Inputs | undefined): Promise<string | undefined> {
async function replaceInputVariable(inputName: string, inputs: Inputs | undefined): Promise<boolean | number | string | undefined> {
if (!inputs) {
window.showErrorMessage(`Missing "inputs" property to resolve "${inputName}" variable.`);
return extUtils.wrapVariable(inputName);
Expand All @@ -317,6 +335,13 @@ async function replaceInputVariable(inputName: string, inputs: Inputs | undefine
title: foundInput.description,
password: foundInput.password,
}), foundInput.default);

if (foundInput?.convertType === 'boolean') {
return Boolean(inputResult);
} else if (foundInput?.convertType === 'number') {
return Number(inputResult);
}

return inputResult;
} else if (foundInput.type === 'command') {
return replaceCommandVariable(foundInput.command, foundInput.args);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ interface InputPromptString {
description?: string;
default?: string;
password?: boolean;
convertType?: 'boolean' | 'number';
}
interface InputPickString {
id: string;
Expand Down

0 comments on commit e15cd33

Please sign in to comment.