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: add better error messaging when initializing an already initialized project #13166

Merged
merged 11 commits into from
Sep 20, 2023
Merged
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { $TSContext, stateManager } from '@aws-amplify/amplify-cli-core';
import { $TSContext, AmplifyError, stateManager } from '@aws-amplify/amplify-cli-core';
import { analyzeProject } from '../../init-steps/s0-analyzeProject';
import { constructMockPluginPlatform } from '../extensions/amplify-helpers/mock-plugin-platform';
import { CLIInput as CommandLineInput } from '../../domain/command-input';
import { constructContext } from '../../context-manager';
import * as fs from 'fs-extra';

jest.spyOn(stateManager, 'getLocalAWSInfo').mockReturnValue({ envA: 'test', envB: 'test' });
jest.spyOn(stateManager, 'getLocalEnvInfo').mockReturnValue({ defaultEditor: 'Visual Studio Code' });
Expand Down Expand Up @@ -75,4 +76,35 @@ describe('analyzeProject', () => {
await analyzeProject(mockContext);
expect(mockContext.exeInfo.isNewEnv).toBe(true);
});

it('throws helpful error message when running subsequent init -y commands', async () => {
const emitErrorMock = jest.fn();

mockContext.amplify = {
confirmPrompt: jest.fn(),
getAllEnvs: jest.fn().mockReturnValue(['dev']),
pathManager: {
getProjectConfigFilePath: jest.fn().mockReturnValue('mockDirPath'),
},
};
mockContext.exeInfo = {
isNewProject: false,
};
mockContext.exeInfo.inputParams = {
amplify: {},
yes: true,
};
mockContext.usageData = {
emitError: emitErrorMock,
};

jest.spyOn(fs, 'existsSync').mockReturnValue(true);
rtpascual marked this conversation as resolved.
Show resolved Hide resolved

const amplifyError = new AmplifyError('ProjectInitError', {
message: `Amplify project is already initialized with environment name: dev`,
resolution: `To create a new environment run \`amplify add env\``,
});

await expect(analyzeProject(mockContext)).rejects.toThrow(amplifyError);
});
});
11 changes: 7 additions & 4 deletions packages/amplify-cli/src/init-steps/s0-analyzeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,11 @@ const getDefaultEnv = (context: $TSContext): string | undefined => {
resolution: INVALID_ENV_NAME_MSG,
});
}

if (isNewProject(context) || !context.amplify.getAllEnvs().includes(defaultEnv)) {
if (
isNewProject(context) ||
!context.amplify.getAllEnvs().includes(defaultEnv) ||
(context.exeInfo.inputParams && context.exeInfo.inputParams.yes)
rtpascual marked this conversation as resolved.
Show resolved Hide resolved
) {
return defaultEnv;
}
return undefined;
Expand All @@ -241,8 +244,8 @@ const getEnvName = async (context: $TSContext): Promise<string> => {
});
} else if (context.exeInfo.inputParams && context.exeInfo.inputParams.yes) {
throw new AmplifyError('ProjectInitError', {
message: `Invalid environment name: ${context.exeInfo.inputParams.amplify.envName}`,
resolution: INVALID_ENV_NAME_MSG,
message: `Amplify project is already initialized with environment name: ${getDefaultEnv(context)}`,
resolution: `To create a new environment run \`amplify add env\``,
});
}
rtpascual marked this conversation as resolved.
Show resolved Hide resolved

Expand Down