Skip to content

Commit

Permalink
fix: truncate long app names on pull (#6741)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhockett authored Feb 26, 2021
1 parent ca66f3e commit 748b252
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
27 changes: 27 additions & 0 deletions packages/amplify-cli/src/__tests__/attach-backend-steps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { analyzeProject } from '../attach-backend-steps/a20-analyzeProject';
import { $TSContext, stateManager } from 'amplify-cli-core';

jest.mock('amplify-cli-core');
const stateManager_mock = stateManager as jest.Mocked<typeof stateManager>;

const contextStub = {
exeInfo: {
projectConfig: {
projectName: 'this-is-a-possible-name-from-the-console!',
version: '3.0',
},
localEnvInfo: {},
},
} as $TSContext;

describe('a20-analyzeProject', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('long console name', async () => {
stateManager_mock.getLocalEnvInfo.mockReturnValueOnce({ defaultEditor: 'gedit' });
await analyzeProject(contextStub);
expect(contextStub.exeInfo.projectConfig.projectName.length).toBe(20);
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { normalizeEditor, editorSelection } from '../extensions/amplify-helpers/editor-selection';
import { amplifyCLIConstants } from '../extensions/amplify-helpers/constants';
import { stateManager } from 'amplify-cli-core';
import { $TSContext, stateManager } from 'amplify-cli-core';
import { normalizeProjectName } from '../extensions/amplify-helpers/project-name-validation';

export async function analyzeProject(context) {
export async function analyzeProject(context: $TSContext) {
let defaultEditor = getDefaultEditor();

if (!defaultEditor) {
defaultEditor = await getEditor(context);
}

context.exeInfo.projectConfig.projectName = normalizeProjectName(context.exeInfo.projectConfig.projectName);

context.exeInfo.forcePush = !!context?.parameters?.options?.forcePush;

context.exeInfo.projectConfig.version = amplifyCLIConstants.PROJECT_CONFIG_VERSION;
Expand All @@ -17,10 +20,7 @@ export async function analyzeProject(context) {
return context;
}

/* End getProjectName */

/* Begin getEditor */
async function getEditor(context) {
async function getEditor(context: $TSContext) {
let editor;
if (context.exeInfo.inputParams.amplify && context.exeInfo.inputParams.amplify.defaultEditor) {
editor = normalizeEditor(context.exeInfo.inputParams.amplify.defaultEditor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { makeId } from './make-id';
export const validAlphanumericRegex = /^[a-zA-Z0-9]+$/;
export const invalidAlphanumericRegex = /[^a-zA-Z0-9]/g;

export function isProjectNameValid(projectName) {
return projectName && projectName.length >= 3 && projectName.length <= 20 && validAlphanumericRegex.test(projectName);
export function isProjectNameValid(projectName: string) {
return !!projectName && projectName.length >= 3 && projectName.length <= 20 && validAlphanumericRegex.test(projectName);
}

export function normalizeProjectName(projectName) {
export function normalizeProjectName(projectName: string) {
if (!projectName) {
projectName = `amplify${makeId(5)}`;
}
Expand Down

0 comments on commit 748b252

Please sign in to comment.