Skip to content

Commit

Permalink
refactor: extract BackendIdentifierResolver (#270)
Browse files Browse the repository at this point in the history
* refactor: extract BackendIdentifierResolver
  • Loading branch information
sdstolworthy authored Sep 22, 2023
1 parent 314677b commit 813cdfb
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-fans-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': patch
---

Extract BackendIdentifierResolver into its own class
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { BackendIdentifierResolver } from './backend_identifier_resolver.js';

describe('BackendIdentifierResolver', () => {
it('returns an App Name and Branch identifier', async () => {
const backendIdResolver = new BackendIdentifierResolver({
resolve: () => Promise.resolve('testAppName'),
});
assert.deepEqual(await backendIdResolver.resolve({ branch: 'test' }), {
appName: 'testAppName',
branchName: 'test',
});
});
it('returns a App Id identifier', async () => {
const backendIdResolver = new BackendIdentifierResolver({
resolve: () => Promise.resolve('testAppName'),
});
assert.deepEqual(
await backendIdResolver.resolve({ appId: 'my-id', branch: 'my-branch' }),
{
backendId: 'my-id',
branchName: 'my-branch',
}
);
});
it('returns a Stack name identifier', async () => {
const backendIdResolver = new BackendIdentifierResolver({
resolve: () => Promise.resolve('testAppName'),
});
assert.deepEqual(await backendIdResolver.resolve({ stack: 'my-stack' }), {
stackName: 'my-stack',
});
});
});
35 changes: 35 additions & 0 deletions packages/cli/src/backend-identifier/backend_identifier_resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BackendIdentifier } from '@aws-amplify/deployed-backend-client';
import { AppNameResolver } from './local_app_name_resolver.js';

type BackendIdentifierParameters = {
stack?: string;
appId?: string;
branch?: string;
};
/**
* Translates args to BackendIdentifier.
* Throws if translation can't be made (this should never happen if command validation works correctly).
*/
export class BackendIdentifierResolver {
/**
* Instantiates BackendIdentifierResolver
*/
constructor(private appNameResolver: AppNameResolver) {}
resolve = async (
args: BackendIdentifierParameters
): Promise<BackendIdentifier> => {
if (args.stack) {
return { stackName: args.stack };
} else if (args.appId && args.branch) {
return { backendId: args.appId, branchName: args.branch };
} else if (args.branch) {
return {
appName: await this.appNameResolver.resolve(),
branchName: args.branch,
};
}
throw new Error(
'Unable to resolve BackendIdentifier with provided parameters'
);
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CwdPackageJsonLoader } from './cwd_package_json_loader.js';
import { CwdPackageJsonLoader } from '../cwd_package_json_loader.js';

export type AppNameResolver = {
resolve: () => Promise<string>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../../test-utils/command_runner.js';
import assert from 'node:assert';
import { ClientConfigGeneratorAdapter } from './client_config_generator_adapter.js';
import { BackendIdentifierResolver } from '../../../backend-identifier/backend_identifier_resolver.js';

describe('generate config command', () => {
const clientConfigGeneratorAdapter = new ClientConfigGeneratorAdapter(
Expand All @@ -21,9 +22,12 @@ describe('generate config command', () => {
() => Promise.resolve()
);

const backendIdResolver = new BackendIdentifierResolver({
resolve: () => Promise.resolve('testAppName'),
});
const generateConfigCommand = new GenerateConfigCommand(
clientConfigGeneratorAdapter,
{ resolve: () => Promise.resolve('testAppName') }
backendIdResolver
);
const parser = yargs().command(
generateConfigCommand as unknown as CommandModule
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ArgumentsCamelCase, Argv, CommandModule } from 'yargs';
import { ClientConfigFormat } from '@aws-amplify/client-config';
import { AppNameResolver } from '../../../local_app_name_resolver.js';
import { ClientConfigGeneratorAdapter } from './client_config_generator_adapter.js';
import { BackendIdentifier } from '@aws-amplify/deployed-backend-client';
import { BackendIdentifierResolver } from '../../../backend-identifier/backend_identifier_resolver.js';

export type GenerateConfigCommandOptions = {
stack: string | undefined;
Expand Down Expand Up @@ -37,7 +36,7 @@ export class GenerateConfigCommand
*/
constructor(
private readonly clientConfigGenerator: ClientConfigGeneratorAdapter,
private readonly appNameResolver: AppNameResolver
private readonly backendIdentifierResolver: BackendIdentifierResolver
) {
this.command = 'config';
this.describe = 'Generates client config';
Expand All @@ -49,7 +48,9 @@ export class GenerateConfigCommand
handler = async (
args: ArgumentsCamelCase<GenerateConfigCommandOptions>
): Promise<void> => {
const backendIdentifier = await this.getBackendIdentifier(args);
const backendIdentifier = await this.backendIdentifierResolver.resolve(
args
);

await this.clientConfigGenerator.generateClientConfigToFile(
backendIdentifier,
Expand All @@ -58,26 +59,6 @@ export class GenerateConfigCommand
);
};

/**
* Translates args to BackendIdentifier.
* Throws if translation can't be made (this should never happen if command validation works correctly).
*/
private getBackendIdentifier = async (
args: ArgumentsCamelCase<GenerateConfigCommandOptions>
): Promise<BackendIdentifier> => {
if (args.stack) {
return { stackName: args.stack };
} else if (args.appId && args.branch) {
return { backendId: args.appId, branchName: args.branch };
} else if (args.branch) {
return {
appName: await this.appNameResolver.resolve(),
branchName: args.branch,
};
}
throw this.missingArgsError;
};

/**
* @inheritDoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { GenerateCommand } from './generate_command.js';
import { GenerateConfigCommand } from './config/generate_config_command.js';
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
import { ClientConfigGeneratorAdapter } from './config/client_config_generator_adapter.js';
import { LocalAppNameResolver } from '../../local_app_name_resolver.js';
import { CwdPackageJsonLoader } from '../../cwd_package_json_loader.js';
import { LocalAppNameResolver } from '../../backend-identifier/local_app_name_resolver.js';
import { BackendIdentifierResolver } from '../../backend-identifier/backend_identifier_resolver.js';

/**
* Creates wired generate command.
Expand All @@ -18,9 +19,13 @@ export const createGenerateCommand = (): CommandModule => {
new CwdPackageJsonLoader()
);

const backendIdentifierResolver = new BackendIdentifierResolver(
localAppNameResolver
);

const generateConfigCommand = new GenerateConfigCommand(
clientConfigGenerator,
localAppNameResolver
backendIdentifierResolver
);

return new GenerateCommand(generateConfigCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { CommandModule } from 'yargs';

import { SandboxCommand, SandboxCommandOptions } from './sandbox_command.js';
import { SandboxSingletonFactory } from '@aws-amplify/sandbox';
import { LocalAppNameResolver } from '../../local_app_name_resolver.js';
import { SandboxDeleteCommand } from './sandbox-delete/sandbox_delete_command.js';
import { SandboxIdResolver } from './sandbox_id_resolver.js';
import { CwdPackageJsonLoader } from '../../cwd_package_json_loader.js';
import { LocalAppNameResolver } from '../../backend-identifier/local_app_name_resolver.js';

/**
* Creates wired sandbox command.
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/sandbox/sandbox_id_resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppNameResolver } from '../../local_app_name_resolver.js';
import { userInfo as _userInfo } from 'os';
import { AppNameResolver } from '../../backend-identifier/local_app_name_resolver.js';

/**
* Resolves an ID that can be used to uniquely identify sandbox environments
Expand Down

0 comments on commit 813cdfb

Please sign in to comment.