Skip to content

Commit

Permalink
fix: add a required input prompt for use in region input (#2292)
Browse files Browse the repository at this point in the history
* fix: add a required input prompt for use in region input

* PR Updates

* api update
  • Loading branch information
Amplifiyer authored Dec 3, 2024
1 parent 1f56a5f commit 0cf5c26
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/shaggy-pens-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@aws-amplify/cli-core': patch
'@aws-amplify/backend-cli': patch
---

add a required input prompt for use in region input
5 changes: 5 additions & 0 deletions packages/cli-core/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import { WriteStream } from 'node:tty';
export class AmplifyPrompter {
static input: (options: {
message: string;
required?: never;
defaultValue?: string;
} | {
message: string;
required: true;
defaultValue?: never;
}) => Promise<string>;
static secretValue: (promptMessage?: string) => Promise<string>;
static yesOrNo: (options: {
Expand Down
28 changes: 22 additions & 6 deletions packages/cli-core/src/prompter/amplify_prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,32 @@ export class AmplifyPrompter {
* @param options for displaying the prompt
* @param options.message display for the prompt
* @param options.defaultValue if user submits without typing anything. Default: "."
* @param options.required if the user input is required, incompatible with options.defaultValue
* @returns Promise<string> the user input
*/
static input = async (options: {
message: string;
defaultValue?: string;
}): Promise<string> => {
const response = await input({
static input = async (
options:
| {
message: string;
required?: never;
defaultValue?: string;
}
| {
message: string;
required: true;
defaultValue?: never;
}
): Promise<string> => {
if (options.required) {
return input({
message: options.message,
validate: (val: string) =>
val && val.length > 0 ? true : 'Cannot be empty',
});
}
return input({
message: options.message,
default: options.defaultValue ?? '',
});
return response;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void describe('configure command', () => {
emitSuccessMock.mock.resetCalls();
});

void it('configures a profile with an IAM user', async (contextual) => {
void it('fails to configure a profile with a name that already has a profile', async (contextual) => {
const mockProfileExists = mock.method(
profileController,
'profileExists',
Expand All @@ -65,7 +65,7 @@ void describe('configure command', () => {
});
});

void it('configures a profile with an IAM user', async (contextual) => {
void it('configures a profile with an existing IAM user credentials', async (contextual) => {
const mockProfileExists = mock.method(
profileController,
'profileExists',
Expand All @@ -84,10 +84,10 @@ void describe('configure command', () => {
}
);

const mockInput = contextual.mock.method(
const mockRequiredInput = contextual.mock.method(
AmplifyPrompter,
'input',
(options: { message: string; defaultValue?: string }) => {
(options: { message: string; required: true }) => {
if (options.message.includes('Enter the AWS region to use')) {
return Promise.resolve(testRegion);
}
Expand All @@ -105,7 +105,7 @@ void describe('configure command', () => {

assert.equal(mockProfileExists.mock.callCount(), 1);
assert.equal(mockSecretValue.mock.callCount(), 2);
assert.equal(mockInput.mock.callCount(), 1);
assert.equal(mockRequiredInput.mock.callCount(), 1);
assert.equal(mockHasIAMUser.mock.callCount(), 1);
assert.equal(mockAppendAWSFiles.mock.callCount(), 1);
assert.deepStrictEqual(mockAppendAWSFiles.mock.calls[0].arguments[0], {
Expand All @@ -121,7 +121,7 @@ void describe('configure command', () => {
});
});

void it('configures a profile without an IAM user', async (contextual) => {
void it('configures a profile without an existing IAM user', async (contextual) => {
const mockProfileExists = mock.method(
profileController,
'profileExists',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class ConfigureProfileCommand

const region = await AmplifyPrompter.input({
message: `Enter the AWS region to use with the '${profileName}' profile (eg us-east-1, us-west-2, etc):`,
required: true,
});

await this.profileController.createOrAppendAWSFiles({
Expand Down

0 comments on commit 0cf5c26

Please sign in to comment.