Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fallback to tpi when looking for existing envs on init #11137

Merged
merged 3 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ jest.mock('amplify-cli-core');
const stateManagerMock = stateManager as jest.Mocked<typeof stateManager>;
stateManagerMock.getLocalAWSInfo.mockReturnValue({ envA: 'test', envB: 'test' });
stateManagerMock.getLocalEnvInfo.mockReturnValue({ defaultEditor: 'Visual Studio Code' });
stateManagerMock.getTeamProviderInfo.mockReturnValue({});

describe('analyzeProject', () => {
it('recognizes the default editor', async () => {
let mockContext;
beforeEach(() => {
const mockPluginPlatform = constructMockPluginPlatform();
const mockProcessArgv = [
'/Users/userName/.nvm/versions/node/v12.16.1/bin/node',
Expand All @@ -20,7 +22,7 @@ describe('analyzeProject', () => {
'-y',
];
const mockInput = new Input(mockProcessArgv);
const mockContext = constructContext(mockPluginPlatform, mockInput) as unknown as $TSContext;
mockContext = constructContext(mockPluginPlatform, mockInput) as unknown as $TSContext;
const frontendPlugins = [
{
name: 'amplify-frontend-javascript',
Expand All @@ -42,9 +44,36 @@ describe('analyzeProject', () => {
},
};
mockContext.runtime.plugins.push(...frontendPlugins);
});

it('recognizes the default editor', async () => {
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => { /* noop */ });
const result = await analyzeProject(mockContext);
expect(result.exeInfo.localEnvInfo.defaultEditor).toStrictEqual('Visual Studio Code');
consoleLogSpy.mockClear();
});

it('sets isNewEnv false in context when env exists in tpi file', async () => {
mockContext.exeInfo.inputParams = {
amplify: {
envName: 'test',
},
};
stateManagerMock.getLocalAWSInfo.mockReturnValue({});
stateManagerMock.getTeamProviderInfo.mockReturnValue({ test: {}, other: {} });
await analyzeProject(mockContext);
expect(mockContext.exeInfo.isNewEnv).toBe(false);
});

it('sets isNewEnv true in context when env does not exists in tpi file', async () => {
mockContext.exeInfo.inputParams = {
amplify: {
envName: 'new',
},
};
stateManagerMock.getLocalAWSInfo.mockReturnValue({});
stateManagerMock.getTeamProviderInfo.mockReturnValue({ test: {}, other: {} });
await analyzeProject(mockContext);
expect(mockContext.exeInfo.isNewEnv).toBe(true);
});
});
22 changes: 19 additions & 3 deletions packages/amplify-cli/src/init-steps/s0-analyzeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,25 @@ const getEnvName = async (context: $TSContext): Promise<string> => {
return envName;
};

const isNewEnv = (envName: string): boolean => !Object.keys(
stateManager.getLocalAWSInfo(process.cwd(), { throwIfNotExist: false, default: {} }),
).includes(envName);
/**
* TODO this currently checks both local-aws-info and team-provider-info for environments
*
* We need to remove the check from team-provider-info and instead use a service call
* but this is a breaking change because it means that some init flows will now require additional arguments to correctly
* attach to existing environments.
* Specifically we need the appId, region and AWS credentials to make a service call to get existing environments
*
* Most likely we should make a breaking change for this where init can no longer be use to pull existing projects and instead customers
* can only use pull for this use case
* @param envName the envName to check
* @returns whether the env already exists
*/
const isNewEnv = (envName: string): boolean => {
const localAwsInfoEnvs = Object.keys(stateManager.getLocalAWSInfo(process.cwd(), { throwIfNotExist: false, default: {} }));
const tpiEnvs = Object.keys(stateManager.getTeamProviderInfo(process.cwd(), { throwIfNotExist: false, default: {} }));
edwardfoyle marked this conversation as resolved.
Show resolved Hide resolved
const allEnvs = Array.from(new Set([...localAwsInfoEnvs, ...tpiEnvs]));
return !allEnvs.includes(envName);
};

const isNewProject = (context: $TSContext): boolean => {
let newProject = true;
Expand Down