Skip to content

Commit ed2ca3e

Browse files
fix: fallback to tpi when looking for existing envs on init (#11137)
* fix: fallback to tpi when looking for existing envs on init * fix: use process.cwd as project dir when loading tpi on init * Update packages/amplify-cli/src/init-steps/s0-analyzeProject.ts Co-authored-by: John Hockett <jhockett@users.noreply.github.com> Co-authored-by: John Hockett <jhockett@users.noreply.github.com>
1 parent 03177a9 commit ed2ca3e

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

packages/amplify-cli/src/__tests__/init-steps/s0-analyzeProject.test.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ jest.mock('amplify-cli-core');
99
const stateManagerMock = stateManager as jest.Mocked<typeof stateManager>;
1010
stateManagerMock.getLocalAWSInfo.mockReturnValue({ envA: 'test', envB: 'test' });
1111
stateManagerMock.getLocalEnvInfo.mockReturnValue({ defaultEditor: 'Visual Studio Code' });
12+
stateManagerMock.getTeamProviderInfo.mockReturnValue({});
1213

1314
describe('analyzeProject', () => {
14-
it('recognizes the default editor', async () => {
15+
let mockContext;
16+
beforeEach(() => {
1517
const mockPluginPlatform = constructMockPluginPlatform();
1618
const mockProcessArgv = [
1719
'/Users/userName/.nvm/versions/node/v12.16.1/bin/node',
@@ -20,7 +22,7 @@ describe('analyzeProject', () => {
2022
'-y',
2123
];
2224
const mockInput = new Input(mockProcessArgv);
23-
const mockContext = constructContext(mockPluginPlatform, mockInput) as unknown as $TSContext;
25+
mockContext = constructContext(mockPluginPlatform, mockInput) as unknown as $TSContext;
2426
const frontendPlugins = [
2527
{
2628
name: 'amplify-frontend-javascript',
@@ -42,9 +44,36 @@ describe('analyzeProject', () => {
4244
},
4345
};
4446
mockContext.runtime.plugins.push(...frontendPlugins);
47+
});
48+
49+
it('recognizes the default editor', async () => {
4550
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => { /* noop */ });
4651
const result = await analyzeProject(mockContext);
4752
expect(result.exeInfo.localEnvInfo.defaultEditor).toStrictEqual('Visual Studio Code');
4853
consoleLogSpy.mockClear();
4954
});
55+
56+
it('sets isNewEnv false in context when env exists in tpi file', async () => {
57+
mockContext.exeInfo.inputParams = {
58+
amplify: {
59+
envName: 'test',
60+
},
61+
};
62+
stateManagerMock.getLocalAWSInfo.mockReturnValue({});
63+
stateManagerMock.getTeamProviderInfo.mockReturnValue({ test: {}, other: {} });
64+
await analyzeProject(mockContext);
65+
expect(mockContext.exeInfo.isNewEnv).toBe(false);
66+
});
67+
68+
it('sets isNewEnv true in context when env does not exists in tpi file', async () => {
69+
mockContext.exeInfo.inputParams = {
70+
amplify: {
71+
envName: 'new',
72+
},
73+
};
74+
stateManagerMock.getLocalAWSInfo.mockReturnValue({});
75+
stateManagerMock.getTeamProviderInfo.mockReturnValue({ test: {}, other: {} });
76+
await analyzeProject(mockContext);
77+
expect(mockContext.exeInfo.isNewEnv).toBe(true);
78+
});
5079
});

packages/amplify-cli/src/init-steps/s0-analyzeProject.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,27 @@ const getEnvName = async (context: $TSContext): Promise<string> => {
292292
return envName;
293293
};
294294

295-
const isNewEnv = (envName: string): boolean => !Object.keys(
296-
stateManager.getLocalAWSInfo(process.cwd(), { throwIfNotExist: false, default: {} }),
297-
).includes(envName);
295+
/**
296+
* TODO this currently checks both local-aws-info and team-provider-info for environments
297+
*
298+
* We need to remove the check from team-provider-info and instead use a service call
299+
* but this is a breaking change because it means that some init flows will now require additional arguments to correctly
300+
* attach to existing environments.
301+
* Specifically we need the appId, region and AWS credentials to make a service call to get existing environments
302+
*
303+
* Most likely we should make a breaking change for this where init can no longer be use to pull existing projects and instead customers
304+
* can only use pull for this use case
305+
* @param envName the envName to check
306+
* @returns whether the env already exists
307+
*/
308+
const isNewEnv = (envName: string): boolean => {
309+
const cwd = process.cwd();
310+
const readOptions = { throwIfNotExist: false, default: {} };
311+
const localAwsInfoEnvs = Object.keys(stateManager.getLocalAWSInfo(cwd, readOptions));
312+
const tpiEnvs = Object.keys(stateManager.getTeamProviderInfo(cwd, readOptions));
313+
const allEnvs = Array.from(new Set([...localAwsInfoEnvs, ...tpiEnvs]));
314+
return !allEnvs.includes(envName);
315+
};
298316

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

0 commit comments

Comments
 (0)